diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/blog/admin.py | 1 | ||||
-rw-r--r-- | app/blog/models.py | 36 | ||||
-rw-r--r-- | app/builder/base.py | 15 | ||||
-rw-r--r-- | app/builder/views.py | 18 | ||||
-rw-r--r-- | app/notes/admin.py | 1 | ||||
-rw-r--r-- | app/pages/admin.py | 18 | ||||
-rw-r--r-- | app/pages/models.py | 90 | ||||
-rw-r--r-- | app/pages/views.py | 2 |
8 files changed, 78 insertions, 103 deletions
diff --git a/app/blog/admin.py b/app/blog/admin.py index 3038a4f..c30244e 100644 --- a/app/blog/admin.py +++ b/app/blog/admin.py @@ -17,6 +17,7 @@ class EntryAsideAdmin(admin.ModelAdmin): class BlogEntryForm(forms.ModelForm): class Meta: model = Entry + fields = '__all__' widgets = { 'body_markdown': forms.Textarea(attrs={'rows': 50, 'cols': 100}), } diff --git a/app/blog/models.py b/app/blog/models.py index 4ff1935..4ad6022 100644 --- a/app/blog/models.py +++ b/app/blog/models.py @@ -162,7 +162,6 @@ class HomepageCurrator(models.Model): template_name = models.CharField(max_length=200,help_text="full path") - class BlogSitemap(Sitemap): changefreq = "never" priority = 1.0 @@ -183,3 +182,38 @@ class LatestFull(Feed): def items(self): return Entry.objects.filter(status__exact=1).order_by('-pub_date')[:10] + + +import os +import io +import urllib.request +import urllib.parse +import urllib.error +import shutil +from django_gravatar.helpers import get_gravatar_url, has_gravatar, get_gravatar_profile_url, calculate_gravatar_hash +from django.dispatch import receiver +from django_comments.signals import comment_was_posted +from django_comments import Comment + +@receiver(comment_was_posted, sender=Comment) +def cache_gravatar(sender, comment, **kwargs): + gravatar_exists = has_gravatar(comment.email) + grav_dir = settings.IMAGES_ROOT + '/gravcache/' + print(gravatar_exists) + if gravatar_exists: + url = get_gravatar_url(comment.email, size=60) + if not os.path.isdir(grav_dir): + os.makedirs(grav_dir) + local_grav = '%s/%s.jpg' % (grav_dir, calculate_gravatar_hash(comment.email)) + urllib.request.urlretrieve(url, local_grav) + else: + dest = '%s/%s.jpg' % (grav_dir, calculate_gravatar_hash(comment.email)) + src = settings.IMAGES_ROOT + '/gravcache/default.jpg' + try: + shutil.copy(src, dest) + # eg. src and dest are the same file + except shutil.Error as e: + print('Error: %s' % e) + # eg. source or destination doesn't exist + except IOError as e: + print('Error: %s' % e.strerror) diff --git a/app/builder/base.py b/app/builder/base.py index 7382655..0b92832 100644 --- a/app/builder/base.py +++ b/app/builder/base.py @@ -7,7 +7,7 @@ from django.template import Context from django.db.models import get_model from django.conf import settings -from pages.models import PageGenerator +#from pages.models import PageGenerator class Build(): def write_file(self, path, text_object, ext='html', filename='index'): @@ -57,7 +57,7 @@ class BuildAll(Build): BuildMap().build() BuildWritingFeed().build() BuildSitemap().build() - p = PageGenerator(settings.PROJ_ROOT + '_pages') + BuildPages().build() p.write_files() class BuildWriting(Build): @@ -261,6 +261,17 @@ class BuildWritingFeed(Build): fpath = '%s' % ('rss/',) self.write_file(fpath, t, 'xml') +class BuildPages(Build): + def build(self): + model = get_model('pages', 'page') + pages = model.objects.all() + for page in pages: + c = Context({'object':page,'SITE_URL':settings.SITE_URL, 'MEDIA_URL':settings.BAKED_MEDIA_URL}) + t = render_to_string(["details/%s.html" % page.slug, 'details/page.html'],c).encode('utf-8') + s = render_to_string('details/page.txt',c).encode('utf-8') + fpath = '%s' %(page.slug) + self.write_file(fpath,t) + self.write_file(fpath,s,'txt') class BuildMap(Build): def build(self): diff --git a/app/builder/views.py b/app/builder/views.py index 04c1285..4d820e2 100644 --- a/app/builder/views.py +++ b/app/builder/views.py @@ -1,14 +1,14 @@ from django.shortcuts import render_to_response from django.template import RequestContext from django.conf import settings -from builder.base import BuildWriting, BuildWritingFeed, BuildMap, BuildPhotos, BuildProjects, BuildSitemap -from pages.models import PageGenerator +from builder.base import BuildWriting, BuildWritingFeed, BuildMap, BuildPhotos, BuildProjects, BuildSitemap, BuildPages +#from pages.models import PageGenerator options = { 'writing': BuildWriting, 'photo_galleries': BuildPhotos, 'projects': BuildProjects, - 'pages': PageGenerator, + 'pages': BuildPages, 'map': BuildMap, 'feed': BuildWritingFeed, 'sitemap': BuildSitemap, @@ -24,10 +24,6 @@ def do_build(request): BuildWritingFeed().build() BuildSitemap().build() context = {'message': 'Writing %s to Disk' % section} - elif section == 'pages': - p = PageGenerator(settings.PROJ_ROOT + '_pages') - p.write_files() - context = {'message': 'Building pages from flatfiles'} elif section == 'scrapeflickr': from photos import retriever retriever.sync_sets() @@ -36,3 +32,11 @@ def do_build(request): options[section]().build() context = {'message': 'Writing %s to Disk' % section} return render_to_response('admin/message.html', context, context_instance=RequestContext(request)) + + +""" +elif section == 'pages': + p = PageGenerator(settings.PROJ_ROOT + '_pages') + p.write_files() + context = {'message': 'Building pages from flatfiles'} +""" diff --git a/app/notes/admin.py b/app/notes/admin.py index 3bbdc8d..ad3e84b 100644 --- a/app/notes/admin.py +++ b/app/notes/admin.py @@ -9,6 +9,7 @@ class NoteModelForm(forms.ModelForm): class Meta: model = Note + fields = '__all__' class NoteAdmin(OSMGeoAdmin): diff --git a/app/pages/admin.py b/app/pages/admin.py index 66e4d8f..844007e 100644 --- a/app/pages/admin.py +++ b/app/pages/admin.py @@ -1,13 +1,22 @@ from django.contrib import admin +from django import forms from django.forms import Textarea from django.db import models -''' +from pages.models import Page + +class PageEntryForm(forms.ModelForm): + class Meta: + model = Page + fields = '__all__' + widgets = { + 'body_markdown': forms.Textarea(attrs={'rows': 50, 'cols': 100}), + } + class PageAdmin(admin.ModelAdmin): - formfield_overrides = { - models.TextField: {'widget': Textarea(attrs={'rows': 25, 'cols': 40})}, - } + form = PageEntryForm + list_display = ('title', 'slug',) search_fields = ['title', 'body_markdown'] prepopulated_fields = {"slug": ('title',)} @@ -23,4 +32,3 @@ class PageAdmin(admin.ModelAdmin): ) admin.site.register(Page, PageAdmin) -''' diff --git a/app/pages/models.py b/app/pages/models.py index 3ef1a71..832b958 100644 --- a/app/pages/models.py +++ b/app/pages/models.py @@ -1,16 +1,8 @@ from django.db import models from django.contrib.sitemaps import Sitemap import markdown - - -import os -import yaml -from django.conf import settings -from django.template.loader import render_to_string -from django.template import Context from mdx_attr_list.mdx_attr_list import AttrListExtension - def markdown_processor(txt): md = markdown.Markdown( extensions=[AttrListExtension(),'footnotes',], @@ -18,7 +10,7 @@ def markdown_processor(txt): safe_mode=False ) return md.convert(txt) -''' + class Page(models.Model): title = models.CharField(max_length=200) slug = models.SlugField() @@ -36,7 +28,7 @@ class Page(models.Model): #run markdown self.body_html = markdown_processor(self.body_markdown) super(Page, self).save() -''' + class PageSitemap(Sitemap): changefreq = "never" @@ -44,80 +36,4 @@ class PageSitemap(Sitemap): protocol = "https" def items(self): - p = PageGenerator(settings.PROJ_ROOT + '_pages') - return p.objects(include_in_sitemap=True) - #return Page.objects.all() - - -class PageGenerator(object): - - def __init__(self, path, *args, **kwargs): - self._objects = [] - for (dirpath, dirnames, filenames) in os.walk(path): - self.dirpath = dirpath - self.file_list = filter(lambda item: not (item.startswith('.') or item.endswith('~') or item.endswith('.md')), filenames) - self.get_files() - - def get_files(self): - for f in self.file_list: - p = Page(self.dirpath + '/' + f) - self._objects.append(p) - - def objects(self, *args, **kwargs): - filtered_list = [] - if kwargs: - for item in self._objects: - found = False - for k, v in kwargs.items(): - if getattr(item, k) == v and not found: - found = True - filtered_list.append(item) - elif getattr(item, k) != v and found: - filtered_list.remove(item) - return filtered_list - return self._objects - - def write_files(self): - for obj in self.objects(): - c = Context({'object': obj, 'SITE_URL': settings.SITE_URL}) - t = render_to_string(["details/%s.html" % obj.template], c) - s = render_to_string('details/page.txt', c) - _FileWriter('', t, ext="html", filename=obj.slug) - _FileWriter('', s, ext="txt", filename=obj.slug) - - -class _FileWriter(object): - """ - Given a path and text object; write the page to disc - """ - def __init__(self, path, text_object, ext='html', filename='index'): - self.path = '%s%s' % (settings.FLATFILES_ROOT, path) - if not os.path.isdir(self.path): - os.makedirs(self.path) - fpath = '%s%s.%s' % (self.path, filename, ext) - self.write(fpath, text_object) - - def write(self, fpath, text_object): - file = open(fpath, 'wb') - file.write(text_object.encode('utf-8')) - file.close() - - -class _FileLoader(object): - - def __init__(self, filename, *args, **kwargs): - self.filename = filename - metadata = self.read() - for k, v in metadata.items(): - setattr(self, k, v) - if self.body_markdown: - self.body_html = markdown_processor(self.body_markdown) - - def read(self): - with open(self.filename, "r", encoding="utf-8") as f: - contents = f.read() - metayaml, self.body_markdown = contents.split('\n---') - return yaml.load(metayaml) - -class Page(_FileLoader): - pass + return Page.objects.all() diff --git a/app/pages/views.py b/app/pages/views.py index e933399..934cb40 100644 --- a/app/pages/views.py +++ b/app/pages/views.py @@ -3,6 +3,6 @@ from django.template import RequestContext from pages.models import Page def page(request, slug): - obj = "" #get_object_or_404(Page, slug__exact=slug) + obj = get_object_or_404(Page, slug__exact=slug) context = {'object': obj, } return render_to_response(["details/%s.html" % obj.slug, 'details/page.html'], context, context_instance=RequestContext(request)) |