Skip to content

Commit

Permalink
Led to pep8, drove to pyflakes. And fixed minor bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
adw0rd committed Nov 13, 2012
1 parent b7fcade commit b52e9d5
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 135 deletions.
25 changes: 13 additions & 12 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys, os
import sys
import os

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand Down Expand Up @@ -170,21 +171,21 @@
# -- Options for LaTeX output --------------------------------------------------

latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#'preamble': '',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
('index', 'django-flatpages-x.tex', u'django-flatpages-x Documentation',
u'Christopher Clarke', 'manual'),
('index', 'django-flatpages-x.tex', u'django-flatpages-x Documentation',
u'Christopher Clarke', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -227,9 +228,9 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'django-flatpages-x', u'django-flatpages-x Documentation',
u'Christopher Clarke', 'django-flatpages-x', 'One line description of project.',
'Miscellaneous'),
('index', 'django-flatpages-x', u'django-flatpages-x Documentation',
u'Christopher Clarke', 'django-flatpages-x', 'One line description of project.',
'Miscellaneous'),
]

# Documents to append as an appendix to all manuals.
Expand Down
2 changes: 1 addition & 1 deletion flatpages_x/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__='0.1.1'
__version__ = '0.1.1'
108 changes: 57 additions & 51 deletions flatpages_x/admin.py
Original file line number Diff line number Diff line change
@@ -1,106 +1,112 @@
from datetime import datetime

from django import forms
from django.contrib import admin
from django.contrib.flatpages.admin import FlatPageAdmin as StockFlatPageAdmin,FlatpageForm
from django.contrib.flatpages.admin import FlatPageAdmin as StockFlatPageAdmin, FlatpageForm
from django.contrib.flatpages.models import FlatPage
from django import forms
from django.utils.translation import ugettext_lazy as _
from flatpages_x.models import FlatPageImage,FlatPageMeta,Revision
from flatpages_x.settings import FPX_TEMPLATE_CHOICES
from django.utils.functional import curry

from flatpages_x.models import FlatPageImage, FlatPageMeta, Revision
from flatpages_x.settings import FPX_TEMPLATE_CHOICES
from flatpages_x.settings import PARSER
from flatpages_x.utils import load_path_attr
from datetime import datetime
#use markitup if available

# Use markitup if available
try:
from markitup.widgets import AdminMarkItUpWidget as content_widget
from markitup.widgets import AdminMarkItUpWidget as content_widget
except ImportError:
content_widget=forms.Textarea
#thumbnails
content_widget = forms.Textarea

# Thumbnails
try:
from sorl.thumbnail import admin as thumbs
except ImportError:
thumbs = None


class CustomFlatPageForm(FlatpageForm):
template_name=forms.ChoiceField(choices=FPX_TEMPLATE_CHOICES, required=False,
label='Template',
help_text=_("Sepcify a template for displaying your content")
)

content_md = forms.CharField(label="Content", widget = content_widget())
content = forms.CharField(
widget = forms.Textarea(
),
required=False,
)

def __init__(self,*args,**kwargs):

super(CustomFlatPageForm, self).__init__(*args, **kwargs)
fp = self.instance

template_name = forms.ChoiceField(
choices=FPX_TEMPLATE_CHOICES, required=False,
label='Template',
help_text=_("Sepcify a template for displaying your content")
)

content_md = forms.CharField(label="Content", widget=content_widget())
content = forms.CharField(widget=forms.Textarea(), required=False)

def __init__(self, *args, **kwargs):
super(CustomFlatPageForm, self).__init__(*args, **kwargs)
fp = self.instance

try:
latest_revision=fp.revisions.order_by("-updated")[0]
latest_revision = fp.revisions.order_by("-updated")[0]
except IndexError:
latest_revision= None
latest_revision = None

if latest_revision:
self.fields["content_md"].initial= latest_revision.content_source
self.fields["content_md"].initial = latest_revision.content_source

def save(self):
fp = super(CustomFlatPageForm, self).save(commit=False)

if PARSER:
render_func = curry(load_path_attr(PARSER[0], **PARSER[1]))
fp.content = render_func(self.cleaned_data["content_md"])
else:
fp.content = self.cleaned_data["content_md"]

fp.save()

r=Revision()
r.flatpage=fp
r.title=fp.title
r.content_source=self.cleaned_data["content_md"]
r.updated=datetime.now()
r = Revision()
r.flatpage = fp
r.title = fp.title
r.content_source = self.cleaned_data["content_md"]
r.updated = datetime.now()
r.save()

return fp



class FlatPageMetaAdmin(admin.ModelAdmin):
list_display = ('flatpage','created',)
list_display = ('flatpage', 'created',)
list_filter = ('flatpage',)
ordering = ('flatpage',)
search_fields = ('flatpage',)


admin.site.register(FlatPageMeta, FlatPageMetaAdmin)


class MetaInline(admin.StackedInline):
model = FlatPageMeta


class ImageInline(admin.TabularInline):
model=FlatPageImage
model = FlatPageImage

if thumbs is not None:
# Add the mixin to the MRO
class ImageInline(thumbs.AdminImageMixin, ImageInline):
pass





class FlatPageAdmin(StockFlatPageAdmin):
fieldsets= (
(None, {'fields': ('url', 'title', 'content_md','template_name',)}),
(_('Advanced options'), {'classes': ('collapse',),
'fields': ('enable_comments', 'registration_required','sites' )}),
fieldsets = (
(None, {'fields': ('url', 'title', 'content_md', 'template_name',)}),
(_('Advanced options'), {'classes': ('collapse',),
'fields': ('enable_comments', 'registration_required', 'sites')}),
)
form=CustomFlatPageForm
form = CustomFlatPageForm
inlines = [MetaInline,
ImageInline,
]

def save_form(self, request, form, change):
# form.save doesn't take a commit kwarg
return form.save()
return form.save()

admin.site.unregister(FlatPage)
admin.site.register(FlatPage, FlatPageAdmin)
admin.site.register(FlatPageImage)
admin.site.register(Revision)
admin.site.register(Revision)
17 changes: 9 additions & 8 deletions flatpages_x/markdown_parser.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import markdown
import re
import markdown
from markdown.inlinepatterns import IMAGE_REFERENCE_RE

from flatpages_x.models import FlatPageImage
img_ref_re=re.compile(IMAGE_REFERENCE_RE)

img_ref_re = re.compile(IMAGE_REFERENCE_RE)


def parse(text):
md = markdown.Markdown(['codehilite'])

for iref in re.findall(img_ref_re,text):
img_id=iref[7]
for iref in re.findall(img_ref_re, text):
img_id = iref[7]
try:
image=FlatPageImage.objects.get(pk=int(img_id))
md.references[img_id]=(image.image_path.url,'')
image = FlatPageImage.objects.get(pk=int(img_id))
md.references[img_id] = (image.image_path.url, '')
except FlatPageImage.DoesNotExist:
pass

return md.convert(text)
73 changes: 34 additions & 39 deletions flatpages_x/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from os.path import split
from datetime import datetime

from django.db import models
from django.conf import settings
from django.contrib.flatpages.models import FlatPage
from datetime import datetime
from django.utils.translation import ugettext_lazy as _
from os.path import split

# Thumbnailability
try:
Expand All @@ -14,56 +14,51 @@

# Create your models here.
class FlatPageMeta(models.Model):
flatpage=models.OneToOneField(FlatPage,related_name="metadata")
flatpage = models.OneToOneField(FlatPage, related_name="metadata")
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
keywords=models.CharField(blank=True,max_length=250,
help_text= _("Separate keywords by commas") )
description=models.TextField(verbose_name=_('meta description'),blank=True)

keywords = models.CharField(blank=True, max_length=250,
help_text=_("Separate keywords by commas"))
description = models.TextField(
verbose_name=_('meta description'), blank=True)

def __unicode__(self):
return self.flatpage.title

class Meta:
verbose_name="Page MetaData"
verbose_name_plural="Page MetaData"




verbose_name = "Page MetaData"
verbose_name_plural = "Page MetaData"


class FlatPageImage(models.Model):
flatpage=models.ForeignKey(FlatPage, related_name='images')
image_path = ImageField(upload_to="flatpage/%Y/%m/%d")
url = models.CharField(blank=True,max_length=150)
flatpage = models.ForeignKey(FlatPage, related_name='images')
image_path = ImageField(upload_to="flatpage/%Y/%m/%d")
url = models.CharField(blank=True, max_length=150)

def __unicode__(self):
if self.pk is not None:
img_file= split(self.image_path.url)[1]
return "[%s] %s" % (self.pk,img_file)
else:
return "deleted image"
if self.pk is not None:
img_file = split(self.image_path.url)[1]
return "[%s] %s" % (self.pk, img_file)
else:
return "deleted image"


class Revision(models.Model):
"""
Note the revision model stores the markdown while the
Note the revision model stores the markdown while the
flapage contents will store the redered html
"""
flatpage = models.ForeignKey(FlatPage, related_name="revisions")
title = models.CharField(max_length=90)
content_source= models.TextField()
content_source = models.TextField()

updated = models.DateTimeField(default=datetime.now)

view_count = models.IntegerField(default=0, editable=False)

def __unicode__(self):
return 'Revision %s for %s' % (self.updated.strftime('%Y%m%d-%H%M'), self.flatpage)
return 'Revision %s for %s' % (self.updated.strftime('%Y%m%d-%H%M'), self.flatpage)

def inc_views(self):
self.view_count += 1
self.save()



self.view_count += 1
self.save()
5 changes: 2 additions & 3 deletions flatpages_x/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
DEFAULT_TEMPLATE_CHOICES = [
('flatpages/default.html', 'Text Only', ),
]
FPX_TEMPLATE_CHOICES = getattr(settings, 'FLATPAGES_X_TEMPLATE_CHOICES', DEFAULT_TEMPLATE_CHOICES)


FPX_TEMPLATE_CHOICES = getattr(
settings, 'FLATPAGES_X_TEMPLATE_CHOICES', DEFAULT_TEMPLATE_CHOICES)
14 changes: 6 additions & 8 deletions flatpages_x/templatetags/flatpages_x_tags.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from django import template
from django.contrib.flatpages.models import FlatPage
from flatpages_x.models import FlatPageMeta,FlatPageImage

register = template.Library()


@register.inclusion_tag('flatpages_x/_metatag_snippet.html')
def show_meta(flatpage):
ret={
"keywords":flatpage.metadata.keywords,
"description":flatpage.metadata.description
}
return ret

ret = {
"keywords": flatpage.metadata.keywords,
"description": flatpage.metadata.description
}
return ret
Loading

0 comments on commit b52e9d5

Please sign in to comment.