Skip to content

Commit

Permalink
Merge pull request chrisdev#7 from adw0rd/master
Browse files Browse the repository at this point in the history
Refactor, bugfixes and improvements in the README
  • Loading branch information
chrisdev committed Nov 15, 2012
2 parents 5e7a67d + 92c7047 commit 7498e55
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 167 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ build*
dist*
*.wpr
*.wpu
*~
1 change: 0 additions & 1 deletion MANIFEST.in
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
39 changes: 18 additions & 21 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ Django Flatpage Extensions

An extension to django.contrib.flatpages to provide

- Better support for markdown and other similar markup formats. We provide support for Markdown but you can write your own parser to support rst or creole.
- Better support for **markdown** and other similar markup formats. We provide support for Markdown but you can write your own parser to support rst or creole.

- Optional support for the excellent markTtUp jquey editor This requires the installation django-markitup.

- Easy inclusion of images in flatpages. Viewing Admin image thumbnails requires the installation of sorl thumbnails

- The inclusion of HTML metatags such as keywords and descriptions in flatpages
- Optional support for the excellent **markItUp** jquery editor. This requires the installation ``django-markitup``.

- Content revisions
- Easy inclusion of images in flatpages. Viewing Admin **image thumbnails** requires the installation of ``sorl-thumbnail``.

- The inclusion of HTML **metatags** such as keywords and descriptions in flatpages.

- Content **revisions**.

Migrating you data to flapages_x should not be difficult since the
data which currently in the contrib.Flatpage model (content, titles) is not affected.
Your templates will still utilize the *{{flatpage.content}}* and *{{flatpage.body}}*
context variables.
Once you install flatpages_x, the Markdown
Once you install flatpages_x, the Markdown
is actually stored in the related Revisions model.
When you save a flatpage, this will be rendered as html via the markdown
parser and saved to the Flatpage.content field


Contributors
============
* `Christopher Clarke <https://github.com/chrisdev>`_
* `Lendl R Smith <https://github.com/ilendl2>`_
* `Mikhail Andreev <https://github.com/adw0rd>`_

Quickstart
===========
Expand All @@ -37,10 +37,12 @@ Create a virtual environment for your project and activate it::
$ source mysite-env/bin/activate
(mysite-env)$
Next install flatpages_x. ::
Next install ``flatpages_x`` ::

(mysite-env)$ pip install django-flatpages-x

Add ``flatpages_x`` to your INSTALLED_APPS setting.

Inside your project run::

(mysite-env)$ python manage.py syncdb
Expand All @@ -59,7 +61,6 @@ markItUp support
------------------
If you want to use the excellent markItUp! editor widget. Install django-markItUp::

(mysite-env)$ pip install django-markitup
You need a few configuration steps
Expand All @@ -74,14 +75,16 @@ You need a few configuration steps

3. You need to use the AJAX-based preview for the admin widget::

url(r'^markitup/', include('markitup.urls')) in your root URLconf.
url(r'^markitup/', include('markitup.urls'))

in your root URLconf.


Admin thumbnails
----------------
If you want view admin image thumbnails install sorl thumbnails
If you want view admin image thumbnails install sorl-thumbnail::

(mysite-env)$ pip install sorl-thumbnails
(mysite-env)$ pip install sorl-thumbnail
1. Add ``sorl.thumbnail`` to your ``settings.INSTALLED_APPS``.
2. Configure your ``settings``
Expand All @@ -97,8 +100,7 @@ you can supply your own parser by setting the value for *FLATPAGES_X_PARSER*
to settings.py. So if you want to use a parser ``myparser_parser`` simply add
the following to you settings ::

FLATPAGES_X_PARSER= ["flatpages_x.myparser_parser.parse", {}]
FLATPAGES_X_PARSER= ["flatpages_x.markdown_parser.parse", {}]
.. end-here
Expand All @@ -109,8 +111,3 @@ See the `full documentation`_ for more details.

.. _full documentation: http://django-flatpages-x.readthedocs.org/






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'
122 changes: 65 additions & 57 deletions flatpages_x/admin.py
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)
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)
Loading

0 comments on commit 7498e55

Please sign in to comment.