← Back to team overview

widelands-dev team mailing list archive

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

 

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

Commit message:
Allow ordering of screenshots by applying a new database column.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #1823634 in Widelands Website: "Allow reordering of screenshots"
  https://bugs.launchpad.net/widelands-website/+bug/1823634

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

Add a new database column 'position' to the model of wlscreens_screenshot. This makes it possible to reorder the screenshots after uploading.

In the admin page a list of screenshots is added to a category. E.g. clicking on Category 'Build 20' all screenshots for 'Build 20' will be listet. One can edit (upload, change) the screenshots in this list. So one don't have to edit each screenshot by its own.

Removed: url for displaying screenshots by one category, which leads only into a 404.

Changed: Replaced the view function with a django class based view.

I thought also to have the screenshot list paginated, to speed up loading time, but i didn't found a good solution to display the pagination bar. This can be done later on.

To get this in:
merge the branch
run: ./manage.py migrate
restart the website
-- 
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands-website/screshot_ordering into lp:widelands-website.
=== modified file 'wlscreens/admin.py'
--- wlscreens/admin.py	2016-12-13 18:28:51 +0000
+++ wlscreens/admin.py	2019-04-09 18:52:10 +0000
@@ -4,11 +4,14 @@
 from models import Category, Screenshot
 from django.contrib import admin
 
-
+class ScreenshotsInline(admin.TabularInline):
+    model = Screenshot
+    
 class CategoryAdmin(admin.ModelAdmin):
     prepopulated_fields = {'slug': ('name',)}
     search_fields = ['name']
     list_display = ['name']
+    inlines = [ScreenshotsInline,]
 
 admin.site.register(Category, CategoryAdmin)
 

=== added file 'wlscreens/migrations/0002_auto_20190409_0924.py'
--- wlscreens/migrations/0002_auto_20190409_0924.py	1970-01-01 00:00:00 +0000
+++ wlscreens/migrations/0002_auto_20190409_0924.py	2019-04-09 18:52:10 +0000
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.20 on 2019-04-09 09:24
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('wlscreens', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name='screenshot',
+            options={'ordering': ['position']},
+        ),
+        migrations.AddField(
+            model_name='screenshot',
+            name='position',
+            field=models.IntegerField(blank=True, default=0, help_text=b'The position inside the category', null=True),
+        ),
+    ]

=== modified file 'wlscreens/models.py'
--- wlscreens/models.py	2019-03-31 11:08:21 +0000
+++ wlscreens/models.py	2019-04-09 18:52:10 +0000
@@ -35,9 +35,6 @@
 
         return super(Category, self).save(*args, **kwargs)
 
-    def get_absolute_url(self):
-        return reverse('wlscreens_category', kwargs={'category_slug': self.slug})
-
     def __unicode__(self):
         return u"%s" % self.name
 
@@ -65,35 +62,54 @@
         editable=False,
         storage=OverwriteStorage(),
     )
-    comment = models.TextField(null=True, blank=True)
-    category = models.ForeignKey(Category, related_name='screenshots')
+    comment = models.TextField(
+        null=True,
+        blank=True
+    )
+    category = models.ForeignKey(
+        Category,
+        related_name='screenshots'
+    )
+    position = models.IntegerField(
+        null=True,
+        blank=True,
+        default=0,
+        help_text='The position inside the category',
+    )
 
     class Meta:
         unique_together = ('name', 'category')
+        ordering = ['position',]
 
     def save(self, *args, **kwargs):
         # Open original screenshot which we want to thumbnail using PIL's Image
         # object
-        image = Image.open(self.screenshot)
-
-        # Convert to RGB if necessary
-        if image.mode not in ('L', 'RGB'):
-            image = image.convert('RGB')
-
-        image.thumbnail(settings.THUMBNAIL_SIZE, Image.ANTIALIAS)
-
-        # Save the thumbnail
-        temp_handle = StringIO()
-        image.save(temp_handle, 'png')
-        temp_handle.seek(0)
-
-        # Save to the thumbnail field
-        suf = SimpleUploadedFile(os.path.split(self.screenshot.name)[-1],
-                                 temp_handle.read(), content_type='image/png')
-        self.thumbnail.save(suf.name + '.png', suf, save=False)
-
-        # Save this photo instance
-        super(Screenshot, self).save(*args, **kwargs)
+        try:
+            image = Image.open(self.screenshot)
+    
+            # Convert to RGB if necessary
+            if image.mode not in ('L', 'RGB'):
+                image = image.convert('RGB')
+    
+            image.thumbnail(settings.THUMBNAIL_SIZE, Image.ANTIALIAS)
+    
+            # Save the thumbnail
+            temp_handle = StringIO()
+            image.save(temp_handle, 'png')
+            temp_handle.seek(0)
+    
+            # Save to the thumbnail field
+            suf = SimpleUploadedFile(os.path.split(self.screenshot.name)[-1],
+                                     temp_handle.read(), content_type='image/png')
+            self.thumbnail.save(suf.name + '.png', suf, save=False)
+    
+            # Save this photo instance
+            super(Screenshot, self).save(*args, **kwargs)
+        except IOError:
+            # Likely we have a screenshot in the database which didn't exist
+            # on the filesystem at the given path. Ignore it.            
+            pass
+
 
     def __unicode__(self):
         return u"%s:%s" % (self.category.name, self.name)

=== modified file 'wlscreens/templates/wlscreens/index.html'
--- wlscreens/templates/wlscreens/index.html	2018-11-26 17:39:05 +0000
+++ wlscreens/templates/wlscreens/index.html	2019-04-09 18:52:10 +0000
@@ -24,6 +24,7 @@
 {% block content_header %}
 	<h1>Screenshots</h1>
 {% endblock %}
+
 {% block content_main %}
 {% for c in categories %}
 	<div class="blogEntry">

=== modified file 'wlscreens/urls.py'
--- wlscreens/urls.py	2016-12-13 18:28:51 +0000
+++ wlscreens/urls.py	2019-04-09 18:52:10 +0000
@@ -5,6 +5,5 @@
 from views import *
 
 urlpatterns = [
-    url(r'^$', index, name='wlscreens_index'),
-    url(r'^(?P<category_slug>[-\w]+)/$', category, name='wlscreens_category'),
+    url(r'^$', CategoryList.as_view(), name='wlscreens_index'),
 ]

=== modified file 'wlscreens/views.py'
--- wlscreens/views.py	2018-04-05 18:55:35 +0000
+++ wlscreens/views.py	2019-04-09 18:52:10 +0000
@@ -1,19 +1,9 @@
 # Create your views here.
 
-from models import Category, Screenshot
-from django.shortcuts import render
-from django.http import Http404
-
-
-def index(request):
-    c = Category.objects.order_by('-name')
-
-    return render(request, 'wlscreens/index.html',
-                  {'categories': c, }
-                  )
-
-
-def category(request, category_slug):
-    """Not implemented at the moment."""
-
-    raise Http404
+from models import Category
+from django.views.generic.list import ListView
+
+class CategoryList(ListView):
+    queryset = Category.objects.order_by('-name')
+    template_name = 'wlscreens/index.html'
+    context_object_name = 'categories'


Follow ups