Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Using theirs
  • Loading branch information
samluescher committed Jul 19, 2011
1 parent f7d7abc commit 30589c6
Show file tree
Hide file tree
Showing 55 changed files with 2,164 additions and 509 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.pyc
.*
dist
MANIFEST

3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

2 changes: 2 additions & 0 deletions AUTHORS
@@ -0,0 +1,2 @@
Samuel Lüscher (philomat)
Jannis Leidel (jezdez)
15 changes: 15 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,15 @@
<<<<<<< HEAD
include LICENSE
include README.md

recursive-include form_designer/locale *
recursive-include form_designer/media *
recursive-include form_designer/templates *
=======
include AUTHORS
include README.md
include LICENSE
recursive-include form_designer/templates *.html *.txt
recursive-include form_designer/media *.js
recursive-include form_designer/locale *.po *.mo
>>>>>>> 29a8207f09ef53dba714edf377d444702106ad5c
16 changes: 6 additions & 10 deletions README.md
Expand Up @@ -29,18 +29,19 @@ This document assumes that you are familiar with Python and Django.

2. Make sure `form_designer` is on your `PYTHONPATH`.
3. Make the directory `form_designer/media/form_designer` available under your `MEDIA_ROOT`.
4. Set up the database tables using

$ manage.py syncdb

5. Add `form_designer` to your `INSTALLED_APPS` setting.
4. Add `form_designer` to your `INSTALLED_APPS` setting.

INSTALLED_APPS = (
...
'form_designer',
)

6. Add the form_designer URLs to your URL conf. For instance, in order to make a form named `example-form` available under `http://domain.com/forms/example-form`, add the following line to `urls.py`. Note: __If you are using the form_designer plugin for Django CMS, step 5 is not necessary__:
5. Set up the database tables using

$ manage.py syncdb

6. Add the form_designer URLs to your URL conf. For instance, in order to make a form named `example-form` available under `http://domain.com/forms/example-form`, add the following line to `urls.py`. Note: __If you are using the form_designer plugin for Django CMS, this step is not necessary__:

urlpatterns = patterns('',
(r'^forms/', include('form_designer.urls')),
Expand All @@ -62,8 +63,3 @@ Optional requirements
* The form_designer admin interface requires jQuery and the jQuery UI Sortable plugin to make building forms a lot more user-friendly. The two Javascript files are bundled with form_designer. Optionally, if Django CMS is installed, the files bundled with that app will be used. If you want to use you own jquery.js instead because you're already including it anyway, define JQUERY_JS in your settings file. For instance:

JQUERY_JS = 'jquery/jquery-latest.js'

Missing features
----------------

* File upload fields should be implemented
7 changes: 7 additions & 0 deletions form_designer/__init__.py
@@ -0,0 +1,7 @@
from south.modelsinspector import add_introspection_rules


add_introspection_rules([], ['^form_designer\.pickled_object_field\.PickledObjectField'])
add_introspection_rules([], ['^form_designer\.model_name_field\.ModelNameField'])
add_introspection_rules([], ['^form_designer\.template_field\.TemplateCharField'])
add_introspection_rules([], ['^form_designer\.template_field\.TemplateTextField'])
185 changes: 135 additions & 50 deletions form_designer/admin.py
@@ -1,21 +1,23 @@
import csv
from django.contrib import admin
from django.utils.translation import ugettext as _, ugettext_lazy
from django.conf.urls.defaults import patterns, url
from django.contrib.admin.views.main import ChangeList
from django.db.models import Count
from django.http import HttpResponse
from django.utils.encoding import smart_unicode, smart_str

try:
import xlwt
except ImportError:
xlwt_installed = False
else:
xlwt_installed = True

from form_designer.forms import FormDefinitionForm, FormDefinitionFieldInlineForm
from form_designer.models import FormDefinition, FormDefinitionField, FormLog
from django import forms
from django.utils.translation import ugettext as _
from django.db import models
from django.conf import settings
import os

MEDIA_SUBDIR = 'form_designer'

class FormDefinitionFieldInlineForm(forms.ModelForm):
class Meta:
model = FormDefinitionField

def clean_choice_model(self):
if not self.cleaned_data['choice_model'] and self.cleaned_data.has_key('field_class') and self.cleaned_data['field_class'] in ('forms.ModelChoiceField', 'forms.ModelMultipleChoiceField'):
raise forms.ValidationError(_('This field class requires a model.'))
return self.cleaned_data['choice_model']
from form_designer import settings
from form_designer.templatetags.friendly import friendly

class FormDefinitionFieldInline(admin.StackedInline):
form = FormDefinitionFieldInlineForm
Expand All @@ -31,34 +33,6 @@ class FormDefinitionFieldInline(admin.StackedInline):
(_('Model Choices'), {'fields': ['choice_model', 'choice_model_empty_label']}),
]

class FormDefinitionForm(forms.ModelForm):
class Meta:
model = FormDefinition
class Media:
js = ([
# Use central jQuery
settings.JQUERY_JS,
# and use jQuery UI bundled with this app
os.path.join(MEDIA_SUBDIR, 'lib/jquery/ui.core.js'),
os.path.join(MEDIA_SUBDIR, 'lib/jquery/ui.sortable.js'),
] if hasattr(settings, 'JQUERY_JS') else [
# Use jQuery bundled with CMS
os.path.join(settings.CMS_MEDIA_URL, 'js/lib/jquery.js'),
os.path.join(settings.CMS_MEDIA_URL, 'js/lib/ui.core.js'),
os.path.join(settings.CMS_MEDIA_URL, 'js/lib/ui.sortable.js'),
] if hasattr(settings, 'CMS_MEDIA_URL') else [
# or use jQuery bundled with this app
os.path.join(MEDIA_SUBDIR, 'lib/jquery/jquery.js'),
os.path.join(MEDIA_SUBDIR, 'lib/jquery/ui.core.js'),
os.path.join(MEDIA_SUBDIR, 'lib/jquery/ui.sortable.js'),
])+[os.path.join(MEDIA_SUBDIR, 'js/lib/django-admin-tweaks-js-lib/js', basename) for basename in (
'jquery-inline-positioning.js',
'jquery-inline-rename.js',
'jquery-inline-collapsible.js',
'jquery-inline-fieldset-collapsible.js',
'jquery-inline-prepopulate-label.js',
)]

class FormDefinitionAdmin(admin.ModelAdmin):
fieldsets = [
(_('Basic'), {'fields': ['name', 'method', 'action', 'title', 'allow_get_initial', 'log_data', 'success_redirect', 'success_clear']}),
Expand All @@ -76,33 +50,144 @@ class FormLogAdmin(admin.ModelAdmin):
list_display = ('form_no_link', 'created', 'id', 'data_html')
list_filter = ('form_definition',)
list_display_links = ()

actions = ['export_csv']
if xlwt_installed:
actions.append('export_xls')

# Disabling all edit links: Hack as found at http://stackoverflow.com/questions/1618728/disable-link-to-edit-object-in-djangos-admin-display-list-only
def form_no_link(self, obj):
return '<a>'+obj.form_definition.__unicode__()+'</a>'
form_no_link.admin_order_field = 'form_definition'
form_no_link.allow_tags = True
form_no_link.short_description = _('Form')

def get_urls(self):
urls = patterns('',
url(r'^export/csv/$', self.admin_site.admin_view(self.export_csv), name='form_designer_export_csv'),
)
if xlwt_installed:
urls += patterns('',
url(r'^export/xls/$', self.admin_site.admin_view(self.export_xls), name='form_designer_export_xls'),
)
return urls + super(FormLogAdmin, self).get_urls()

def data_html(self, obj):
return obj.form_definition.compile_message(obj.data, 'html/formdefinition/data_message.html')
data_html.allow_tags = True
data_html.short_description = _('Data')

def get_change_list_query_set(self, request):
cl = ChangeList(request, self.model, self.list_display, self.list_display_links, self.list_filter,
self.date_hierarchy, self.search_fields, self.list_select_related, self.list_per_page, self.list_editable, self)
return cl.get_query_set()

def export_csv(self, request, queryset=None):
response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=' + settings.CSV_EXPORT_FILENAME
writer = csv.writer(response, delimiter=settings.CSV_EXPORT_DELIMITER)
if queryset is None:
queryset = self.get_change_list_query_set(request)

distinct_forms = queryset.aggregate(Count('form_definition', distinct=True))['form_definition__count']

include_created = settings.CSV_EXPORT_INCLUDE_CREATED
include_pk = settings.CSV_EXPORT_INCLUDE_PK
include_header = settings.CSV_EXPORT_INCLUDE_HEADER and distinct_forms == 1
include_form = settings.CSV_EXPORT_INCLUDE_FORM and distinct_forms > 1

if include_header:
header = []
if include_form:
header.append(_('Form'))
if include_created:
header.append(_('Created'))
if include_pk:
header.append(_('ID'))
for field in queryset[0].data:
header.append(field['label'] if field['label'] else field['key'])
writer.writerow([smart_str(cell, encoding=settings.CSV_EXPORT_ENCODING) for cell in header])

for entry in queryset:
row = []
if include_form:
row.append(entry.form_definition)
if include_created:
row.append(entry.created)
if include_pk:
row.append(entry.pk)
for field in entry.data:
value = friendly(field['value'])
row.append(smart_str(
value, encoding=settings.CSV_EXPORT_ENCODING))
writer.writerow(row)
return response
export_csv.short_description = ugettext_lazy("Export selected %(verbose_name_plural)s as CSV")

def export_xls(self, request, queryset=None):
import xlwt

response = HttpResponse(mimetype='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename=%s.xls' % unicode(self.model._meta.verbose_name_plural)
wb = xlwt.Workbook()
ws = wb.add_sheet(unicode(self.model._meta.verbose_name_plural))
if queryset is None:
queryset = self.get_change_list_query_set(request)

distinct_forms = queryset.aggregate(Count('form_definition', distinct=True))['form_definition__count']

include_created = settings.CSV_EXPORT_INCLUDE_CREATED
include_pk = settings.CSV_EXPORT_INCLUDE_PK
include_header = settings.CSV_EXPORT_INCLUDE_HEADER and distinct_forms == 1
include_form = settings.CSV_EXPORT_INCLUDE_FORM and distinct_forms > 1

if include_header:
header = []
if include_form:
header.append(_('Form'))
if include_created:
header.append(_('Created'))
if include_pk:
header.append(_('ID'))
for field in queryset[0].data:
header.append(field['label'] if field['label'] else field['key'])
for i, f in enumerate(header):
ws.write(0, i, smart_unicode(f, encoding=settings.CSV_EXPORT_ENCODING))

for i, entry in enumerate(queryset):
row = []
if include_form:
row.append(entry.form_definition)
if include_created:
row.append(entry.created)
if include_pk:
row.append(entry.pk)
for field in entry.data:
value = friendly(field['value'])
row.append(smart_unicode(
value, encoding=settings.CSV_EXPORT_ENCODING))
for j, cell in enumerate(row):
ws.write(i+1, j, smart_unicode(cell))
wb.save(response)
return response
export_xls.short_description = ugettext_lazy("Export selected %(verbose_name_plural)s as XLS")

def changelist_view(self, request, extra_context=None):
from django.core.urlresolvers import reverse, NoReverseMatch
from django.core.urlresolvers import reverse, NoReverseMatch
extra_context = extra_context or {}
try:
query_string = '?'+request.META['QUERY_STRING']
except TypeError, KeyError:
except (TypeError, KeyError):
query_string = ''
try:
extra_context['export_csv_url'] = reverse('form_designer_export_csv')+query_string
extra_context['export_csv_url'] = reverse('admin:form_designer_export_csv')+query_string
except NoReverseMatch:
request.user.message_set.create(message=_('CSV export is not enabled.'))

if xlwt_installed:
try:
extra_context['export_xls_url'] = reverse('admin:form_designer_export_xls')+query_string
except NoReverseMatch:
request.user.message_set.create(message=_('XLS export is not enabled.'))
return super(FormLogAdmin, self).changelist_view(request, extra_context)

admin.site.register(FormDefinition, FormDefinitionAdmin)
admin.site.register(FormLog, FormLogAdmin)

7 changes: 0 additions & 7 deletions form_designer/admin_urls.py

This file was deleted.

62 changes: 0 additions & 62 deletions form_designer/admin_views.py

This file was deleted.

8 changes: 0 additions & 8 deletions form_designer/app_settings.py

This file was deleted.

0 comments on commit 30589c6

Please sign in to comment.