diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/blog/admin.py | 51 | ||||
-rw-r--r-- | app/blog/models.py | 116 | ||||
-rw-r--r-- | app/blog/widgets.py | 16 | ||||
-rw-r--r-- | app/guide/admin.py | 6 | ||||
-rw-r--r-- | app/guide/models.py | 6 |
5 files changed, 83 insertions, 112 deletions
diff --git a/app/blog/admin.py b/app/blog/admin.py index cae0b59..c4b7e77 100644 --- a/app/blog/admin.py +++ b/app/blog/admin.py @@ -1,18 +1,43 @@ from django.contrib import admin from django import forms -from blog.models import Entry, PostImage, Topic +from blog.models import Entry, PostImage, EntryAside from blog.widgets import AdminImageWidget from django.contrib.gis.admin import OSMGeoAdmin from django.contrib.gis.maps.google import GoogleMap from django.conf import settings - +from models import * GMAP = GoogleMap(key=settings.GOOGLE_MAPS_API_KEY) +class EntryAsideInline(admin.TabularInline): + model = EntryAside + extra = 1 + + +class EntryAsideAdmin(admin.ModelAdmin): + pass + +class BlogEntryForm(forms.ModelForm): + class Meta: + model = Entry + widgets = { + 'body_markdown': forms.Textarea(attrs={'rows':50, 'cols':100}), + } + def clean_point(self): + try: + location = Location.objects.filter(geometry__contains=self.cleaned_data['point']).get() + self.location = location + except Location.DoesNotExist: + raise forms.ValidationError("There is no location associated with that point") + return self.cleaned_data['point'] + + class EntryAdmin(OSMGeoAdmin): + form = BlogEntryForm + inlines = [EntryAsideInline,] def formfield_for_dbfield(self, db_field, **kwargs): - if db_field.name == 'thumbnail': + if db_field.name == 'thumbnail' or db_field.name == 'image' : field = forms.FileField(widget=AdminImageWidget) else: field = super(EntryAdmin,self).formfield_for_dbfield(db_field,**kwargs) @@ -20,17 +45,15 @@ class EntryAdmin(OSMGeoAdmin): list_display = ('title', 'pub_date','template_name', 'status','region','location','photo_gallery') search_fields = ['title', 'body_markdown'] prepopulated_fields = {"slug" : ('title',)} - list_filter = ('pub_date', 'enable_comments', 'status','region','location') + list_filter = ('pub_date', 'enable_comments', 'status','location__state__country__lux_region',) fieldsets = ( - ('Entry', {'fields': ('title','body_markdown', ('location','region'), 'pub_date', ('status','enable_comments'), 'slug','photo_gallery'), 'classes': ('show','extrapretty','wide')}), - ('Pub Location', {'fields': ('point',('image','thumbnail',),'dek', 'topics', 'meta_description','template_name'), 'classes': ('collapse', 'wide')}), + ('Entry', {'fields': ('title','body_markdown', ('pub_date', 'status'), 'slug','point'), 'classes': ('show','extrapretty','wide')}), + ('Formatting data', {'fields': ('dek','meta_description', ('image','thumbnail',),'template_name'), 'classes': ('grp-collapse grp-closed',)}), ) class Media: js = ['/media/admin/custom/model.js'] - extra_js = [GMAP.api_url + GMAP.key] - map_template = 'gis/admin/google.html' - #map_template = 'gis/admin/google.html' + map_template = 'gis/admin/osm.html' # Default GeoDjango OpenLayers map options # Uncomment and modify as desired # To learn more about this jargon visit: @@ -65,16 +88,10 @@ class EntryAdmin(OSMGeoAdmin): #debug = False #widget = OpenLayersWidget - - - class PostImageAdmin(admin.ModelAdmin): - list_display = ('title', 'output_tags') + list_display = ('title', 'post_image') -class TopicAdmin(admin.ModelAdmin): - list_display = ('name', 'slug') - -admin.site.register(Topic, TopicAdmin) admin.site.register(PostImage, PostImageAdmin) +admin.site.register(EntryAside, EntryAsideAdmin) admin.site.register(Entry, EntryAdmin) diff --git a/app/blog/models.py b/app/blog/models.py index a1025cc..bf319ce 100644 --- a/app/blog/models.py +++ b/app/blog/models.py @@ -1,12 +1,14 @@ import datetime from django.contrib.gis.db import models +from django.utils.html import format_html from django.conf import settings from django.contrib.syndication.views import Feed from django.contrib.sitemaps import Sitemap from django.template.defaultfilters import truncatewords_html from PIL import Image -from utils import markdown2 as markdown +#http://freewisdom.org/projects/python-markdown/ +import markdown from photos.models import PhotoGallery from locations.models import Location,Region @@ -21,11 +23,6 @@ def image_url_replace(str): str = str.replace('[[base_url]]', settings.IMAGES_URL) return str -def markdown_processor(md): - processed = markdown.markdown(md, ['footnotes'],safe_mode = False).split('<break>') - html = processed[0]+processed[1] - lede = processed[0] - return html, lede PUB_STATUS = ( (0, 'Draft'), @@ -47,60 +44,29 @@ class PostImage(models.Model): def __unicode__(self): return self.title - def output_tags(self): - return force_unicode('<img src="%s%s" alt="%s" class="postpic"/>' % \ + def post_image(self): + return format_html('<img src="%s%s" alt="%s" class="postpic"/>' % \ (settings.IMAGES_URL, self.image.url.split('images')[1].split('/',1)[1], self.title)) + post_image.allow_tags = True -class Topic(models.Model): - name = models.CharField(max_length=100) - slug = models.SlugField() - - def __unicode__(self): - return self.name - - def get_absolute_url(self): - return "/topics/%s/" % (self.slug) - class Entry(models.Model): title = models.CharField(max_length=200) slug = models.SlugField(unique_for_date='pub_date') - lede = models.TextField(blank=True) body_html = models.TextField(blank=True) body_markdown = models.TextField() dek = models.TextField(null=True,blank=True) pub_date = models.DateTimeField('Date published') - enable_comments = models.BooleanField(default=True) + enable_comments = models.BooleanField(default=False) point = models.PointField(null=True, blank=True) - location = models.ForeignKey(Location, null=True) - region = models.ForeignKey(Region, null=True) + location = models.ForeignKey(Location, null=True, blank=True) status = models.IntegerField(choices=PUB_STATUS, default=0) photo_gallery = models.ForeignKey(PhotoGallery, blank=True, null=True, verbose_name='photo set') image = models.FileField(upload_to=get_upload_path, null=True,blank=True) - image_height = models.CharField(max_length=20, null=True,blank=True) - image_width = models.CharField(max_length=20, null=True,blank=True) thumbnail = models.FileField(upload_to=get_tn_path, null=True,blank=True) - thumb_height = models.CharField(max_length=20, null=True,blank=True) - thumb_width = models.CharField(max_length=20, null=True,blank=True) meta_description = models.CharField(max_length=256, null=True, blank=True) - topics = models.ManyToManyField(Topic, blank=True) template_name = models.IntegerField(choices=TEMPLATES, default=0) - location_name = models.CharField(max_length=200, null=True,blank=True) - state_name = models.CharField(max_length=200, null=True,blank=True) - country_name = models.CharField(max_length=200, null=True,blank=True) - state_iso = models.CharField(max_length=2, null=True,blank=True) - country_iso = models.CharField(max_length=2, null=True,blank=True) - - @property - def longitude(self): - '''Get the site's longitude.''' - return self.point.x - @property - def latitude(self): - '''Get the site's latitude.''' - return self.point.y - class Meta: ordering = ('-pub_date',) get_latest_by = 'pub_date' @@ -111,16 +77,7 @@ class Entry(models.Model): def get_absolute_url(self): return "/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(), self.slug) - - @property - def get_previous_published(self): - return self.get_previous_by_pub_date(status__exact=1) - - @property - def get_next_published(self): - return self.get_next_by_pub_date(status__exact=1) - - + def comment_period_open(self): return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date @@ -131,28 +88,43 @@ class Entry(models.Model): def get_image_url(self): image_dir, img = self.image.url.split('post-images/')[1].split('/') return '%spost-images/%s/%s' %(settings.IMAGES_URL, image_dir, img) - + + @property + def region(self): + return self.location.state.country.lux_region + + @property + def longitude(self): + '''Get the site's longitude.''' + return self.point.x + + @property + def latitude(self): + '''Get the site's latitude.''' + return self.point.y + + @property + def get_previous_published(self): + return self.get_previous_by_pub_date(status__exact=1) + + @property + def get_next_published(self): + return self.get_next_by_pub_date(status__exact=1) + + def save(self): - #get image dimensions - img = Image.open(self.image) - self.image_width, self.image_height = img.size - #same for thumb - img = Image.open(self.thumbnail) - self.thumb_width, self.thumb_height = img.size - #find and replace image urls md = image_url_replace(self.body_markdown) - #run markdown - html,lede = markdown_processor(md) - self.body_html = html - self.lede = lede + self.body_html = markdown.markdown(self.body_markdown, extensions=['extra',], safe_mode = False) self.dek == markdown.markdown(self.dek, safe_mode = False) - self.location_name = self.location.name - self.state_name = self.location.state.name - self.country_name = self.location.state.country.name - self.state_iso = self.location.state.code - self.country_iso = self.location.state.country.iso2 super(Entry, self).save() +class EntryAside(models.Model): + title = models.CharField(max_length=200) + body = models.TextField(null=True,blank=True) + entry = models.ForeignKey(Entry) + + + class BlogSitemap(Sitemap): changefreq = "never" priority = 1.0 @@ -171,9 +143,3 @@ class LatestFull(Feed): def items(self): return Entry.objects.filter(status__exact=1).order_by('-pub_date')[:10] - - -from django.dispatch import dispatcher -from django.db.models import signals - - diff --git a/app/blog/widgets.py b/app/blog/widgets.py index a9451e7..e7d9da8 100644 --- a/app/blog/widgets.py +++ b/app/blog/widgets.py @@ -5,15 +5,10 @@ from django.conf import settings from PIL import Image import os -try: - from sorl.thumbnail.main import DjangoThumbnail - def thumbnail(image_path): - t = DjangoThumbnail(relative_source=image_path, requested_size=(200,200)) - return u'<img src="%s" alt="%s" />' % (t.absolute_url, image_path) -except ImportError: - def thumbnail(image_path): - absolute_url = os.path.join(settings.IMAGES_URL, image_path) - return u'<img src="%s" alt="%s" />' % (absolute_url, image_path) +def thumbnail(image_path): + absolute_url = os.path.join(settings.IMAGES_URL, image_path[7:]) + print absolute_url + return u'<img style="max-width: 400px" src="%s" alt="%s" />' % (absolute_url, image_path) class AdminImageWidget(AdminFileWidget): """ @@ -25,8 +20,7 @@ class AdminImageWidget(AdminFileWidget): file_name = str(value) if file_name: file_path = '%s' % (file_name) - output.append('<a target="_blank" href="%s">%s</a><br />%s <a target="_blank" href="%s">%s</a><br />%s ' % \ - (file_path, thumbnail(file_name), _('Currently:'), file_path, file_name, _('Change:'))) + output.append('<a target="_blank" href="%s">%s</a>' % (file_path, thumbnail(file_name))) output.append(super(AdminFileWidget, self).render(name, value, attrs)) return mark_safe(u''.join(output)) diff --git a/app/guide/admin.py b/app/guide/admin.py index 55f713f..cd24ab3 100644 --- a/app/guide/admin.py +++ b/app/guide/admin.py @@ -10,12 +10,12 @@ from django.conf import settings GMAP = GoogleMap(key=settings.GOOGLE_MAPS_API_KEY) class GuideAdmin(OSMGeoAdmin): - list_display = ('title', 'pub_date','template_name', 'status','region','location','photo_gallery') + list_display = ('title', 'pub_date','template_name', 'status','location','photo_gallery') search_fields = ['title', 'body_markdown'] prepopulated_fields = {"slug" : ('title',)} - list_filter = ('pub_date', 'status','region','location') + list_filter = ('pub_date', 'status','location__state__country__lux_region','location') fieldsets = ( - ('Note', {'fields': ('title','body_markdown', ('location','region'), 'pub_date', 'status', 'slug','photo_gallery'), 'classes': ('show','extrapretty','wide')}), + ('Note', {'fields': ('title','body_markdown', ('location'), 'pub_date', 'status', 'slug','photo_gallery'), 'classes': ('show','extrapretty','wide')}), ('Extra', {'fields': ('dek', 'meta_description','template_name', ('image', 'thumbnail')), 'classes': ('collapse', 'wide')}), ) diff --git a/app/guide/models.py b/app/guide/models.py index 2e19006..5a2036d 100644 --- a/app/guide/models.py +++ b/app/guide/models.py @@ -47,19 +47,13 @@ class Guide(models.Model): dek = models.TextField(null=True,blank=True) pub_date = models.DateTimeField('Date published') location = models.ForeignKey(Location, null=True, blank=True) - country = models.ForeignKey(Country, null=True, blank=True) - region = models.ForeignKey(Region, null=True, blank=True) status = models.IntegerField(choices=PUB_STATUS, default=0) photo_gallery = models.ForeignKey(PhotoGallery, blank=True, null=True, verbose_name='photo set') meta_description = models.CharField(max_length=256, null=True, blank=True) template_name = models.IntegerField(choices=TEMPLATES, default=0) tags = TaggableManager(blank=True) image = models.FileField(upload_to=get_upload_path, null=True,blank=True) - image_height = models.CharField(max_length=20, null=True,blank=True) - image_width = models.CharField(max_length=20, null=True,blank=True) thumbnail = models.FileField(upload_to=get_tn_path, null=True,blank=True) - thumb_height = models.CharField(max_length=20, null=True,blank=True) - thumb_width = models.CharField(max_length=20, null=True,blank=True) @property |