← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands-website/pybb_attachments into lp:widelands-website

 

kaputtnik has proposed merging lp:~widelands-dev/widelands-website/pybb_attachments into lp:widelands-website.

Commit message:
Allow attachments in the forum

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #964452 in Widelands Website: "Suggestion: Allow uploads to the site"
  https://bugs.launchpad.net/widelands-website/+bug/964452
  Bug #1833658 in Widelands Website: "Create a possibility to upload tournament replay"
  https://bugs.launchpad.net/widelands-website/+bug/1833658

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands-website/pybb_attachments/+merge/370342

Made attachments python3 compatible: https://bazaar.launchpad.net/~widelands-dev/widelands-website/pybb_attachments/revision/545

Set allowed file size to 5 Mb (as we have for nginx)

Delete attachment when deleting a post: https://bazaar.launchpad.net/~widelands-dev/widelands-website/pybb_attachments/revision/548

Added template for showing attachments.

Style tweaks, example: https://bugs.launchpad.net/widelands-website/+bug/964452/+attachment/5277903/+files/attachments_1.png

Autoreload css for the users if this got merged, so the users don't have to hit CTRL+F5

I'll do some explanations in the wiki and announce this change. Probably with the option to remove it again if we get too many problematic attachments.
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands-website/pybb_attachments into lp:widelands-website.
=== modified file 'mainpage/settings.py'
--- mainpage/settings.py	2019-04-11 16:11:21 +0000
+++ mainpage/settings.py	2019-07-18 20:25:31 +0000
@@ -191,7 +191,8 @@
 # Pybb Configuration #
 ######################
 # See also pybb defaults in pybb.settings.py
-PYBB_ATTACHMENT_ENABLE = False  # disable gzip middleware when enabling attachments
+PYBB_ATTACHMENT_ENABLE = True
+PYBB_ATTACHMENT_SIZE_LIMIT = 1024*1024*5  #5MB
 PYBB_DEFAULT_MARKUP = 'markdown'
 INTERNAL_PERM='pybb.can_access_internal' # The permission string derived from pybb.models.category
 

=== modified file 'pybb/forms.py'
--- pybb/forms.py	2019-03-20 21:32:32 +0000
+++ pybb/forms.py	2019-07-18 20:25:31 +0000
@@ -1,6 +1,6 @@
 import re
 from datetime import datetime
-import os.path
+import os
 
 from django import forms
 from django.conf import settings
@@ -69,9 +69,15 @@
                              name=memfile.name, post=post)
             dir = os.path.join(settings.MEDIA_ROOT,
                                pybb_settings.ATTACHMENT_UPLOAD_TO)
-            fname = '%d.0' % post.id
+            if not os.path.exists(dir):
+                os.makedirs(dir)
+
+            fname = '{}.0'.format(post.id)
             path = os.path.join(dir, fname)
-            file(path, 'w').write(memfile.read())
+
+            with open(path, 'wb') as f:
+                f.write(memfile.read())
+
             obj.path = fname
             obj.save()
 

=== modified file 'pybb/models.py'
--- pybb/models.py	2019-04-11 15:20:33 +0000
+++ pybb/models.py	2019-07-18 20:25:31 +0000
@@ -345,6 +345,11 @@
     def delete(self, *args, **kwargs):
         self_id = self.id
         head_post_id = self.topic.posts.order_by('created')[0].id
+
+        if self.attachments.all():
+            for attach in self.attachments.all():
+                attach.delete()
+
         super(Post, self).delete(*args, **kwargs)
 
         self.topic.save()
@@ -398,7 +403,7 @@
         super(Attachment, self).save(*args, **kwargs)
         if not self.hash:
             self.hash = hashlib.sha1(
-                str(self.id) + settings.SECRET_KEY).hexdigest()
+                str(self.id).encode('utf-8') + settings.SECRET_KEY.encode('utf-8')).hexdigest()
         super(Attachment, self).save(*args, **kwargs)
 
     def __str__(self):
@@ -407,19 +412,13 @@
     def get_absolute_url(self):
         return reverse('pybb_attachment', args=[self.hash])
 
-    def size_display(self):
-        size = self.size
-        if size < 1024:
-            return '%b' % size
-        elif size < 1024 * 1024:
-            return '%dKb' % int(size / 1024)
-        else:
-            return '%.2fMb' % (size / float(1024 * 1024))
-
     def get_absolute_path(self):
         return os.path.join(settings.MEDIA_ROOT, pybb_settings.ATTACHMENT_UPLOAD_TO,
                             self.path)
 
+    def delete(self, *args, **kwargs):
+        os.remove(self.get_absolute_path())
+        super(Attachment, self).delete(*args, **kwargs)
 
 if notification is not None:
     signals.post_save.connect(notification.handle_observations, sender=Post)

=== modified file 'pybb/static/css/forum.css'
--- pybb/static/css/forum.css	2019-02-28 16:09:16 +0000
+++ pybb/static/css/forum.css	2019-07-18 20:25:31 +0000
@@ -132,6 +132,10 @@
 	border: 1px solid #474444;
 }
 
+.attachment {
+	display: inline-block
+}
+
 /*****************/
 /* Edit/add Post */
 /*   related     */

=== modified file 'pybb/templates/pybb/base.html'
--- pybb/templates/pybb/base.html	2019-01-24 18:03:54 +0000
+++ pybb/templates/pybb/base.html	2019-07-18 20:25:31 +0000
@@ -7,7 +7,7 @@
 {% endblock %}
 
 {% block extra_head %}
-<link rel="stylesheet" type="text/css" media="all" href="{% static 'css/forum.css' %}" />
+<link rel="stylesheet" type="text/css" media="all" href="{% static 'css/forum.css' %}?v1" />
 
 <link rel="alternate" type="application/atom+xml" title="Latest Posts on all forums" href="{% url 'pybb_feed_posts' %}" />
 <link rel="alternate" type="application/atom+xml" title="Latest Topics on all forums" href="{% url 'pybb_feed_topics' %}" />

=== modified file 'pybb/templates/pybb/delete_post.html'
--- pybb/templates/pybb/delete_post.html	2018-10-14 13:24:15 +0000
+++ pybb/templates/pybb/delete_post.html	2019-07-18 20:25:31 +0000
@@ -15,13 +15,18 @@
 	<div class="preview-box">
 		<div class="content post">
 		{{ post.body_html|safe }}
+
+		{% if post.attachments.all %}
+			{% include 'pybb/inlines/attachment.html' %}
+		{% endif %}
 		</div>
+		<br />
 	</div>
 	<form method="post">
-		<div class="info_line">
+		<p class="info_line">
 			<input type="submit" value="{% trans "Yes, I am sure." %}" />
 			<input type="button" value="{% trans "Cancel" %}" onclick="history.back();" />
-		</div>
+		</p>
 		{% csrf_token %}
 	</form>
 </div>

=== added file 'pybb/templates/pybb/inlines/attachment.html'
--- pybb/templates/pybb/inlines/attachment.html	1970-01-01 00:00:00 +0000
+++ pybb/templates/pybb/inlines/attachment.html	2019-07-18 20:25:31 +0000
@@ -0,0 +1,9 @@
+{% load i18n %}
+
+{% for attach in post.attachments.all %}
+	<div class="attachment">
+		<hr />
+		{% trans "Attachment" %}: <a href="{{ attach.get_absolute_url }}">{{ attach.name }}</a> ({{ attach.size|filesizeformat }})
+	</div>
+{% endfor %}
+

=== modified file 'pybb/templates/pybb/inlines/post.html'
--- pybb/templates/pybb/inlines/post.html	2018-12-21 12:50:32 +0000
+++ pybb/templates/pybb/inlines/post.html	2019-07-18 20:25:31 +0000
@@ -29,19 +29,16 @@
 	<a id="post-{{ post.id }}" href="{{post.get_absolute_url}}" title="{% trans "Permalink" %}" class="posRight small permalink">&nbsp;</a>
 	<span class="small">Posted at: {{ post.created|custom_date:user}}</span>
 	<hr />
-	<div class="post">
 		{{ post.body_html|safe }}
-	</div>
+
+	{% if post.updated %}
+		<div class="small">{% trans "Edited" %}: {{ post.updated|custom_date:user|title}}</div>
+	{% endif %}
 
 	{% if post.attachment_cache %}
-		{% for attach in post.attachment_cache %}
-			{% trans "Attachment" %}: <a href="{{ attach.get_absolute_url }}">{{ attach.name }}</a> ({{ attach.size_display }})
-		{% endfor %}
+		{% include 'pybb/inlines/attachment.html' %}
 	{% endif %}
 
-	{% if post.updated %}
-		<span class="small">{% trans "Edited" %}: {{ post.updated|custom_date:user|title}}</span>
-	{% endif %}
 	<hr />
 	{% if user.is_authenticated %}
 		{% ifequal user.wlprofile.show_signatures 1 %}
@@ -54,7 +51,7 @@
 			{{ post.user.wlprofile.signature|urlize|linebreaks }}
 		{% endif %}
 	{% endif %}
-
+	</div>
 	<a class="button posRight" href="#top">
 		<img src="{% static 'forum/img/top.png' %}" alt ="" class="middle" />
 		<span class="middle">{% trans "Top" %}</span>

=== modified file 'pybb/util.py'
--- pybb/util.py	2019-04-11 15:20:33 +0000
+++ pybb/util.py	2019-07-18 20:25:31 +0000
@@ -31,7 +31,6 @@
             output = func(request, *args, **kwargs)
             if not isinstance(output, dict):
                 return output
-            
 
             # TODO(Franku): 'MIME_TYPE' is never in output as i can see for now.
             # But if, this should maybe 'content_type' instead

=== modified file 'pybb/views.py'
--- pybb/views.py	2019-05-27 18:18:14 +0000
+++ pybb/views.py	2019-07-18 20:25:31 +0000
@@ -297,7 +297,7 @@
     if not allowed:
         return HttpResponseRedirect(post.get_absolute_url())
 
-    if 'POST' == request.method:
+    if request.method == 'POST':
         topic = post.topic
         forum = post.topic.forum
         post.delete()
@@ -352,12 +352,12 @@
     return HttpResponseRedirect(reverse('pybb_topic', args=[topic.id]))
 
 
-@login_required
 def show_attachment(request, hash):
     attachment = get_object_or_404(Attachment, hash=hash)
-    file_obj = file(attachment.get_absolute_path())
-    return HttpResponse(file_obj, content_type=attachment.content_type)
-
+    
+    with open(attachment.get_absolute_path(), 'rb') as file_obj:
+        return HttpResponse(file_obj, content_type=attachment.content_type)
+    return HTTP404
 
 @login_required
 @ajax


Follow ups