Guilherme Gondim avatar Guilherme Gondim committed adca264

Add image field

Comments (0)

Files changed (3)

     date_hierarchy = 'time_created'
     fieldsets = (
         (None, {
-            'fields': ('title', 'path', 'template', 'content')
+            'fields': ('title', 'path', 'template', 'image', 'content')
         }),
     )
     list_display = ('path', 'title', 'time_created', 'time_modified')

paginas/migrations/0011_auto__add_field_page_image.py

+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        # Adding field 'Page.image'
+        db.add_column(u'paginas_page', 'image',
+                      self.gf('django.db.models.fields.files.ImageField')(default='', max_length=100, blank=True),
+                      keep_default=False)
+
+
+    def backwards(self, orm):
+        # Deleting field 'Page.image'
+        db.delete_column(u'paginas_page', 'image')
+
+
+    models = {
+        u'paginas.page': {
+            'Meta': {'ordering': "('path',)", 'object_name': 'Page'},
+            'content': ('django.db.models.fields.TextField', [], {'blank': 'True'}),
+            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'image': ('django.db.models.fields.files.ImageField', [], {'max_length': '100', 'blank': 'True'}),
+            'path': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '128', 'db_index': 'True'}),
+            'template': ('django.db.models.fields.related.ForeignKey', [], {'default': '1', 'to': u"orm['paginas.Template']"}),
+            'time_created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'time_modified': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'title': ('django.db.models.fields.CharField', [], {'max_length': '255'})
+        },
+        u'paginas.template': {
+            'Meta': {'ordering': "('title',)", 'object_name': 'Template'},
+            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'title': ('django.db.models.fields.CharField', [], {'max_length': '64'})
+        }
+    }
+
+    complete_apps = ['paginas']

paginas/models.py

+import binascii
+import os
+
 from django.db import models
+from django.utils.timezone import now
 from django.utils.translation import ugettext_lazy as _
 
 from . import app_settings
 
 
+def filefield_path(instance, filename):
+    today = now()
+    year, month = today.strftime('%Y'), today.strftime('%m')
+    uid = binascii.b2a_hex(os.urandom(2))
+    filename = '{}-{}'.format(uid, filename)
+    classname = instance.__class__.__name__.lower()
+    return os.path.join(classname, year, month, filename)
+
+
 class Page(models.Model):
     title = models.CharField(_('title'), max_length=255)
     path = models.CharField(
         db_index=True,
         help_text=_('Relative URL. Example: <i>/contact/</i>.')
     )
+    image = models.ImageField(
+        _('featured image'),
+        upload_to=filefield_path,
+        blank=True
+    )
     content = models.TextField(_('content'), blank=True)
     template = models.ForeignKey(
         'Template',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.