[extensions-web] extensions: do not store default icon path in database.



commit 41dcd3c7d04b166518d750e56b688f7e8c757da1
Author: Yuri Konotopov <ykonotopov gnome org>
Date:   Sun Dec 16 22:12:20 2018 +0400

    extensions: do not store default icon path in database.
    
    This does not works anymore with Django 1.10+ since commit fdf5cd34 [1].
    
    [1] https://github.com/django/django/commit/fdf5cd34

 sweettooth/auth/templates/registration/profile.html |  8 ++++----
 .../migrations/0003_auto_20181216_2101.py           | 21 +++++++++++++++++++++
 .../migrations/0004_auto_20181216_2102.py           | 20 ++++++++++++++++++++
 sweettooth/extensions/models.py                     |  2 +-
 .../extensions/templates/extensions/detail.html     |  3 ++-
 .../templates/extensions/detail_edit.html           |  3 ++-
 .../extensions/templatetags/extension_icon.py       |  8 ++++++++
 sweettooth/extensions/views.py                      |  3 ++-
 sweettooth/review/templates/review/list.html        |  3 ++-
 sweettooth/review/templates/review/review.html      |  3 ++-
 10 files changed, 64 insertions(+), 10 deletions(-)
---
diff --git a/sweettooth/auth/templates/registration/profile.html 
b/sweettooth/auth/templates/registration/profile.html
index 7f22554..5e56b66 100644
--- a/sweettooth/auth/templates/registration/profile.html
+++ b/sweettooth/auth/templates/registration/profile.html
@@ -2,7 +2,7 @@
 {% block title %}{{ display_name }}'s Profile - {{ block.super }}{% endblock %}
 
 {% block body %}
-{% load gravatar %}
+{% load gravatar extension_icon %}
 <div class="profile">
   {% if is_editable %}
   <h2 class="editable" data-pk="{{ user.pk }}" id="new_display_name">{{ display_name }}</h2>
@@ -19,7 +19,7 @@
     {% for extension in extensions %}
     <li class="extension profile">
       <h4 class="extension-name"><a href="{{ extension.get_absolute_url }}">
-          <img src="{{ extension.icon.url }}" class="icon"> {{ extension.name }}
+          <img src="{% extension_icon extension.icon %}" class="icon"> {{ extension.name }}
       </a></h4>
 
       <p class="description">{{ extension.first_line_of_description }}</p>
@@ -32,7 +32,7 @@
   {% with version.extension as extension %}
   <li class="extension profile">
     <h4 class="extension-name"><a href="{% url 'review-version' pk=version.pk %}">
-        <img src="{{ extension.icon.url }}" class="icon"> {{ extension.name }}
+        <img src="{% extension_icon extension.icon %}" class="icon"> {{ extension.name }}
     </a></h4>
     <span class="version">(v{{ version.version }})</span>
 
@@ -46,7 +46,7 @@
   {% with version.extension as extension %}
   <li class="extension profile">
     <h4 class="extension-name"><a href="{% url 'review-version' pk=version.pk %}">
-        <img src="{{ extension.icon.url }}" class="icon"> {{ extension.name }}
+        <img src="{% extension_icon extension.icon %}" class="icon"> {{ extension.name }}
     </a></h4>
     <span class="version">(v{{ version.version }})</span>
 
diff --git a/sweettooth/extensions/migrations/0003_auto_20181216_2101.py 
b/sweettooth/extensions/migrations/0003_auto_20181216_2101.py
new file mode 100644
index 0000000..954b01c
--- /dev/null
+++ b/sweettooth/extensions/migrations/0003_auto_20181216_2101.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.17 on 2018-12-16 21:01
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import sweettooth.extensions.models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extensions', '0002_auto_20161027_1641'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='extension',
+            name='icon',
+            field=models.ImageField(blank=True, default=b'', 
upload_to=sweettooth.extensions.models.make_icon_filename),
+        ),
+    ]
diff --git a/sweettooth/extensions/migrations/0004_auto_20181216_2102.py 
b/sweettooth/extensions/migrations/0004_auto_20181216_2102.py
new file mode 100644
index 0000000..22fc3bf
--- /dev/null
+++ b/sweettooth/extensions/migrations/0004_auto_20181216_2102.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import migrations
+from sweettooth.extensions.models import Extension
+
+
+def update_icons(apps, schema_editor):
+    Extension.objects.filter(icon__startswith="/").update(icon="")
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('extensions', '0003_auto_20181216_2101'),
+    ]
+
+    operations = [
+        migrations.RunPython(update_icons)
+    ]
diff --git a/sweettooth/extensions/models.py b/sweettooth/extensions/models.py
index 087e946..ed4d452 100644
--- a/sweettooth/extensions/models.py
+++ b/sweettooth/extensions/models.py
@@ -105,7 +105,7 @@ class Extension(models.Model):
         )
 
     screenshot = models.ImageField(upload_to=make_screenshot_filename, blank=True)
-    icon = models.ImageField(upload_to=make_icon_filename, blank=True, default="/static/images/plugin.png")
+    icon = models.ImageField(upload_to=make_icon_filename, blank=True, default="")
 
     objects = ExtensionManager()
 
diff --git a/sweettooth/extensions/templates/extensions/detail.html 
b/sweettooth/extensions/templates/extensions/detail.html
index a11f47b..2138708 100644
--- a/sweettooth/extensions/templates/extensions/detail.html
+++ b/sweettooth/extensions/templates/extensions/detail.html
@@ -1,6 +1,7 @@
 {% extends "base.html" %}
 {% block title %}{{ extension.name }} - {{ block.super }}{% endblock %}
 
+{% load extension_icon %}
 {% block body %}
     <div class="extension single-page {% block extra-ext-class %}{% endblock %}"
          data-epk="{{ extension.pk }}"
@@ -10,7 +11,7 @@
       <div class="extension-header col-xs-12 col-sm-8 col-md-8 col-lg-8 no-padding">
         {% spaceless %}
         {% block icon %}
-        <img src="{{ extension.icon.url }}" class="icon">
+        <img src="{% extension_icon extension.icon %}" class="icon">
         {% endblock icon %}
         <h3 class="extension-name" id="extension_name">{{ extension.name }}</h3>
         {% endspaceless %}
diff --git a/sweettooth/extensions/templates/extensions/detail_edit.html 
b/sweettooth/extensions/templates/extensions/detail_edit.html
index 75e4c85..6fbc3a8 100644
--- a/sweettooth/extensions/templates/extensions/detail_edit.html
+++ b/sweettooth/extensions/templates/extensions/detail_edit.html
@@ -1,4 +1,5 @@
 {% extends "extensions/detail.html" %}
+{% load extension_icon %}
 
 {% block comments %}
   {% if is_visible %}
@@ -72,7 +73,7 @@
 
 {% block icon %}
 <label class="icon upload">
-  <img src="{{ extension.icon.url }}">
+  <img src="{% extension_icon extension.icon %}">
   <input type="file" accept="image/*">
 </label>
 {% endblock %}
diff --git a/sweettooth/extensions/templatetags/extension_icon.py 
b/sweettooth/extensions/templatetags/extension_icon.py
new file mode 100644
index 0000000..c338218
--- /dev/null
+++ b/sweettooth/extensions/templatetags/extension_icon.py
@@ -0,0 +1,8 @@
+
+from django import template
+
+register = template.Library()
+
+@register.simple_tag
+def extension_icon(icon):
+    return icon.url if icon else "/static/images/plugin.png"
diff --git a/sweettooth/extensions/views.py b/sweettooth/extensions/views.py
index 1144e39..1c54444 100644
--- a/sweettooth/extensions/views.py
+++ b/sweettooth/extensions/views.py
@@ -18,6 +18,7 @@ from sweettooth.extensions import models, search
 from sweettooth.extensions.forms import UploadForm
 
 from sweettooth.decorators import ajax_view, model_view
+from sweettooth.extensions.templatetags.extension_icon import extension_icon
 
 def get_versions_for_version_strings(version_strings):
     def get_version(major, minor, point):
@@ -358,7 +359,7 @@ def ajax_details(extension, version=None):
                    pk = extension.pk,
                    description = extension.description,
                    link = extension.get_absolute_url(),
-                   icon = extension.icon.url,
+                   icon = extension_icon(extension.icon),
                    screenshot = extension.screenshot.url if extension.screenshot else None,
                    shell_version_map = extension.visible_shell_version_map)
 
diff --git a/sweettooth/review/templates/review/list.html b/sweettooth/review/templates/review/list.html
index cbd8f2d..bbef539 100644
--- a/sweettooth/review/templates/review/list.html
+++ b/sweettooth/review/templates/review/list.html
@@ -1,4 +1,5 @@
 {% extends "base.html" %}
+{% load extension_icon %}
 
 {% block title %}Unreviewed Extensions - {{ block.super }}{% endblock %}
 
@@ -8,7 +9,7 @@
 {% for version in version_list %}
 {% with version.extension as extension %}
 <li class="extension">
-  <h3 class="extension-name"><a href="{% url 'review-version' pk=version.pk %}"><img src="{{ 
extension.icon.url }}" class="icon">{{ extension.name }}</a></h3>
+  <h3 class="extension-name"><a href="{% url 'review-version' pk=version.pk %}"><img src="{% extension_icon 
extension.icon %}" class="icon">{{ extension.name }}</a></h3>
   <span class="version">(v{{ version.version }})</span>
   <span class="author">by <a href="{% url 'auth-profile' user=extension.creator %}">{{ extension.creator 
}}</a></span>
   <p class="description">{{ extension.first_line_of_description }}</p>
diff --git a/sweettooth/review/templates/review/review.html b/sweettooth/review/templates/review/review.html
index cce314a..ac7fbd0 100644
--- a/sweettooth/review/templates/review/review.html
+++ b/sweettooth/review/templates/review/review.html
@@ -1,5 +1,6 @@
 {% extends "base.html" %}
 {% load static from staticfiles %}
+{% load extension_icon %}
 
 {% block title %}Review "{{ extension.name }}" - {{ block.super }}{% endblock %}
 
@@ -11,7 +12,7 @@
         {% spaceless %}
             <h3 class="extension-name">
                 <a href="{{ extension.get_absolute_url }}">
-                    <img src="{{ extension.icon.url }}" class="icon">{{ extension.name }}
+                    <img src="{% extension_icon extension.icon %}" class="icon">{{ extension.name }}
                 </a>
             </h3>
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]