forked from chrisdev/django-flatpages-x
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request chrisdev#7 from adw0rd/master
Refactor, bugfixes and improvements in the README
- Loading branch information
Showing
13 changed files
with
168 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ build* | |
dist* | ||
*.wpr | ||
*.wpu | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
include MANIFEST.in | ||
include README.rst | ||
recursive-include docs * | ||
recursive-include flatpages_x/static * | ||
recursive-include flatpages_x/templates * | ||
recursive-include flatpages_x/fixtures * | ||
exclude *.wpr *.wpu .DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__='0.1.1' | ||
__version__ = '0.1.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +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) | ||
render_func = curry(load_path_attr(PARSER[0],**PARSER[1])) | ||
fp.content= render_func(self.cleaned_data["content_md"]) | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.