From fa4a13668b3796185fcd6a3b3da6c2d28818a550 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 17 Nov 2011 23:21:56 -0400 Subject: [PATCH 01/78] Added views to create, list and delete document groups via the project setup --- apps/grouping/__init__.py | 29 ++++++- apps/grouping/forms.py | 7 ++ apps/grouping/static/images/icons/package.png | Bin 0 -> 972 bytes .../static/images/icons/package_delete.png | Bin 0 -> 1538 bytes apps/grouping/urls.py | 4 + apps/grouping/views.py | 76 +++++++++++++++++- 6 files changed, 111 insertions(+), 5 deletions(-) create mode 100644 apps/grouping/static/images/icons/package.png create mode 100644 apps/grouping/static/images/icons/package_delete.png diff --git a/apps/grouping/__init__.py b/apps/grouping/__init__.py index b939fc5fa6..4bfe65aea0 100644 --- a/apps/grouping/__init__.py +++ b/apps/grouping/__init__.py @@ -1,11 +1,34 @@ from django.utils.translation import ugettext_lazy as _ from navigation.api import register_links - +from permissions.api import register_permission, set_namespace_title +from project_setup.api import register_setup from documents.literals import PERMISSION_DOCUMENT_VIEW from documents.models import Document -document_group_link = {'text': _(u'group actions'), 'view': 'document_group_view', 'famfam': 'page_go', 'permissions': [PERMISSION_DOCUMENT_VIEW]} -groups_for_document = {'text': _(u'groups'), 'view': 'groups_for_document', 'args': 'object.pk', 'famfam': 'page_go', 'permissions': [PERMISSION_DOCUMENT_VIEW]} +from grouping.models import DocumentGroup + + +PERMISSION_DOCUMENT_GROUP_VIEW = {'namespace': 'grouping', 'name': 'group_view', 'label': _(u'View existing document groups')} +PERMISSION_DOCUMENT_GROUP_CREATE = {'namespace': 'grouping', 'name': 'group_create', 'label': _(u'Create new document groups')} +PERMISSION_DOCUMENT_GROUP_DELETE = {'namespace': 'grouping', 'name': 'group_delete', 'label': _(u'Delete document groups')} + +set_namespace_title('grouping', _(u'Grouping')) +register_permission(PERMISSION_DOCUMENT_GROUP_VIEW) +register_permission(PERMISSION_DOCUMENT_GROUP_CREATE) +register_permission(PERMISSION_DOCUMENT_GROUP_DELETE) + +document_group_link = {'text': _(u'group actions'), 'view': 'document_group_view', 'famfam': 'package_go', 'permissions': [PERMISSION_DOCUMENT_VIEW]} +groups_for_document = {'text': _(u'groups'), 'view': 'groups_for_document', 'args': 'object.pk', 'famfam': 'package_go', 'permissions': [PERMISSION_DOCUMENT_VIEW]} + +document_groups_setup = {'text': _(u'document groups'), 'view': 'document_group_list', 'icon': 'package.png', 'permissions': [PERMISSION_DOCUMENT_GROUP_VIEW]} +document_group_list = {'text': _(u'document groups'), 'view': 'document_group_list', 'famfam': 'package', 'permissions': [PERMISSION_DOCUMENT_GROUP_VIEW]} +document_group_create = {'text': _(u'create new'), 'view': 'document_group_create', 'famfam': 'package_add', 'permissions': [PERMISSION_DOCUMENT_GROUP_CREATE]} +document_group_delete = {'text': _(u'delete'), 'view': 'document_group_delete', 'args': 'object.pk', 'famfam': 'package_delete', 'permissions': [PERMISSION_DOCUMENT_GROUP_DELETE]} register_links(Document, [groups_for_document], menu_name='form_header') + +register_links(DocumentGroup, [document_group_delete]) +register_links(['document_group_list', 'document_group_create', 'document_group_delete'], [document_group_list, document_group_create], menu_name='sidebar') + +register_setup(document_groups_setup) diff --git a/apps/grouping/forms.py b/apps/grouping/forms.py index 389987ab7a..3c5d65792b 100644 --- a/apps/grouping/forms.py +++ b/apps/grouping/forms.py @@ -8,6 +8,13 @@ from django.conf import settings from tags.widgets import get_tags_inline_widget +from grouping.models import DocumentGroup + + +class DocumentGroupForm(forms.ModelForm): + class Meta: + model = DocumentGroup + class DocumentGroupImageWidget(forms.widgets.Widget): def render(self, name, value, attrs=None): diff --git a/apps/grouping/static/images/icons/package.png b/apps/grouping/static/images/icons/package.png new file mode 100644 index 0000000000000000000000000000000000000000..448681eaf5aaf6361059da71e97f791a878a6591 GIT binary patch literal 972 zcmV;-12g=IP)AR(c6?{HTP#idWZ@v2OqcDHd4BW0&pR{AIOo{TOTji@I{@q_ecJT+k;jJ0M!+0S z$1WW8TgynS{s(hM8fleZD?(9pi;AQs+F%ytDiN$OMU7dc!qXzw^2GnZc$>W%tdWgZxuUa5ji}@2S)pzI-W=d1~+nboo!EVuhX*ba#i5Ookvzn{?$}qyfAN)^`yfpI*n{mDwf; z)+~O=I(Xp$Y9DS23()LU6xYhgWeX^*SSXds2!{OVhz5~JgyAkBGVF^5Eagm?YbE&o z2Ex01h{nPQgba#;EiItzzlyi_&Jo0$280V>%#06O{gGag=}^e>4h9kz(O#nBN`8*z1@e%@%xy)GmL>zCmjMn ze|=|Yp~`&m#Z(TlWE9cP2$H>Vgd%N@^&9Pncr`hZHOJC5MZK-%SCN{}AeYV~OBxB% zuq&nKN3Xm*MR5ittr{N~xkix^CC2J{9Hv`#f4N#a8DFBq3)N;e3HIh|XAspRd@)CgQQp;VhwML(9LeVMzit&pr z;-3g;O>ndt8;+X-R-x5Rtw+F10LGJGer9c=^^%j6DQmY9- z6oeu?AWSIFC>{dP_(F?s^Z-zVN-5wTACj1GAvQ!nY#>yeQ~+WRXWjxZF0~lj2%y)Nms*F1?%Q|?Bjl-llIH=rCDX3B ug{N^|KM|SR{aQc^e%z_F|){C0W0IIcuSr5G452^j4}O5RUoSf1wOdgME{D_05Es$S&?sidA05Es%4&+-_ZvqRRp&$vg zseo{_fIFi(_&gT)=UXR%U{Z0A#zT=j8fe_>Hq#shP!y>CmoaeuFajTSP6NTFDc{wW zU%YWA)PZklR04NDcPZh}?!jOhBO~KP9tBpLNn4c57U`dW1q+<;`yE=SNN+i09E4f| zYk3FPe*7GPt(OfTm{IxCeCylaLHYYDP64uBL^fSOG%}9yWFEO(0d|KK^W1j$d`?sj zkmmN#IL4wGWYRfUt!6lDEpU6BusO`+1({}*AS~H|tB2ks5LYHZQ~;Q}_XP57AD&0- z>?XwHc|^nG$YhGNfd%s$-SBv7V6$5^tI7gH3>YhWh)Xt;MXT zzTN`Yy2H4Hd-=eyu<^1|V&K{?nJnc^=46PeMM z6;kMuj-?R19Y%CCh6w5Kkq)_-`)TFYORtijVY7&G`;!|#CQp%Nw7RTMMMhUL(_B?P zjl`b@pvlkhh=x<8$TIXC-`UP}a{v}qmLWC0i15+XfM}VFj5^!dhBlP*@VS?WN>&J>1K@My3(X30x}5dhQ-y{sO9X1PT`-@!hl2So(7 zZ$s%u57I{tpcD?4m3aNAf9833wts>v`}g6-{v!~4q7i^9XDI}nfa1dgU>vW2~IIMb%r?ODe#`v6t8`fj+k7IX*fQTVd2-GT?Bo06{O9*$2 zV$qs;$X)0Iqok%53ioUnm?%OX=!2_mC9V&ZCIM(2j9MTT^BP`V{Iy(0gc5Jv1aT2{ zr!U`kO=81xu-iAGO*R0KWH76vI{xbb1hPy`j1h;d4_?CO#K$Ib4F{k`Q}XZKCr++X zs;oL%`Lzke9S$H4v9=DWH(sEGQ^K?g#^*|sHb;ra4S689_Os4+>F$}Dn5wv>qiu`7 zjA!xzkBc_9SoKcm9?6P#R&+Wd@dT13xeFRKwX}pGl2G+2P`hluj>W%jzGFY+^LezB zr0}9M-2oJ_vfKX+hH)+w-^&{ke&`jwJ<5=zXCT?GL@K(*Q`=HZrxrCfG-x0=FBr9S z{4R3W(>vbWcK&q@{qfYP9le9!(AqI^7K$u4KshlI?OIh=)0|7jAG294a5$ZmBP>KZ zljizpEQX%Geq4(tPruc9{Zm?Vhl6LcROz`PbtH|Ontji$_q*P6OJeKPwmn*subv9V z4jdi|U7)|i{6z9HF\d+)/group/(?P\d+)/$', 'document_group_view', (), 'document_group_view'), url(r'^groups/for_document/(?P\d+)/$', 'groups_for_document', (), 'groups_for_document'), + + url(r'^setup/list/$', 'document_group_list', (), 'document_group_list'), + url(r'^setup/create/$', 'document_group_create', (), 'document_group_create'), + url(r'^setup/(?P\d+)/delete/$', 'document_group_delete', (), 'document_group_delete'), ) diff --git a/apps/grouping/views.py b/apps/grouping/views.py index 704483ce45..a47099eb0d 100644 --- a/apps/grouping/views.py +++ b/apps/grouping/views.py @@ -5,13 +5,20 @@ from django.shortcuts import get_object_or_404, render_to_response from django.core.urlresolvers import reverse from django.template import RequestContext +from common.utils import generate_choices_w_labels, encapsulate +from common.widgets import two_state_template + from documents.models import Document from documents.views import document_list +from permissions.api import check_permissions + from grouping.models import DocumentGroup from grouping.conf.settings import SHOW_EMPTY_GROUPS -from grouping.forms import DocumentDataGroupForm +from grouping.forms import DocumentDataGroupForm, DocumentGroupForm from grouping import document_group_link +from grouping import PERMISSION_DOCUMENT_GROUP_VIEW, \ + PERMISSION_DOCUMENT_GROUP_CREATE, PERMISSION_DOCUMENT_GROUP_DELETE def document_group_action(request): @@ -80,4 +87,69 @@ def groups_for_document(request, document_id): 'object': document, 'document': document, 'subtemplates_list': subtemplates_list, - }, context_instance=RequestContext(request)) + }, context_instance=RequestContext(request)) + + +def document_group_list(request): + check_permissions(request.user, [PERMISSION_DOCUMENT_GROUP_VIEW]) + + return render_to_response('generic_list.html', { + 'title': _(u'document groups'), + 'object_list': DocumentGroup.objects.all(), + 'extra_columns': [ + {'name': _(u'dynamic title'), 'attribute': 'dynamic_title'}, + {'name': _(u'enabled'), 'attribute': encapsulate(lambda x: two_state_template(x.enabled))}, + ], + 'hide_link': True, + }, context_instance=RequestContext(request)) + + +def document_group_create(request): + check_permissions(request.user, [PERMISSION_DOCUMENT_GROUP_CREATE]) + + if request.method == 'POST': + form = DocumentGroupForm(request.POST) + if form.is_valid(): + document_group = form.save() + messages.success(request, _(u'Document group: %s created successfully.') % document_group) + return HttpResponseRedirect(reverse('document_group_list')) + else: + form = DocumentGroupForm() + + return render_to_response('generic_form.html', { + 'form': form, + 'title': _(u'Create new document group') + }, context_instance=RequestContext(request)) + + +def document_group_delete(request, document_group_id): + check_permissions(request.user, [PERMISSION_DOCUMENT_GROUP_DELETE]) + + document_group = get_object_or_404(DocumentGroup, pk=document_group_id) + + next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/'))) + previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) + + if request.method == 'POST': + try: + document_group.delete() + messages.success(request, _(u'Document group: %s deleted successfully.') % document_group) + except Exception, error: + messages.error(request, _(u'Error deleting document group: %(document_group)s; %(error)s.') % { + 'document_group': document_group, + 'error': error + }) + return HttpResponseRedirect(next) + + return render_to_response('generic_confirm.html', { + 'delete_view': True, + 'object': document_group, + 'next': next, + 'previous': previous, + 'form_icon': u'package_delete.png', + #'temporary_navigation_links': {'form_header': {'staging_file_delete': {'links': results['tab_links']}}}, + }, context_instance=RequestContext(request)) + + + + From 3934b4c53579552806ca28ab62a4dc1d55a61fd3 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 17 Nov 2011 23:22:33 -0400 Subject: [PATCH 02/78] Add the document grouping setup url to the project setup menu link child urls list --- apps/project_setup/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/project_setup/__init__.py b/apps/project_setup/__init__.py index 93a570a912..7f160810a2 100644 --- a/apps/project_setup/__init__.py +++ b/apps/project_setup/__init__.py @@ -3,4 +3,4 @@ from django.utils.translation import ugettext_lazy as _ from navigation.api import register_top_menu #TODO: FIXME dynamic children_path_regext on api register_setup -register_top_menu('setup_menu', link={'text': _(u'setup'), 'view': 'setup_list', 'famfam': 'cog'}, children_path_regex=[r'^settings/', r'^user_management/', r'^permissions', r'^documents/type', r'^metadata/setup', r'sources/setup'], position=-2) +register_top_menu('setup_menu', link={'text': _(u'setup'), 'view': 'setup_list', 'famfam': 'cog'}, children_path_regex=[r'^settings/', r'^user_management/', r'^permissions', r'^documents/type', r'^metadata/setup', r'sources/setup', r'grouping/setup'], position=-2) From 96e92b296e0fa6f8929f698960adbbbbb8f6f35b Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 17 Nov 2011 23:23:18 -0400 Subject: [PATCH 03/78] Improved POST redirect default url from None to '/' --- apps/permissions/views.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/permissions/views.py b/apps/permissions/views.py index 65bd30ec56..3f58846eb9 100644 --- a/apps/permissions/views.py +++ b/apps/permissions/views.py @@ -102,8 +102,8 @@ def role_create(request): def role_delete(request, role_id): check_permissions(request.user, [PERMISSION_ROLE_DELETE]) - next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', None))) - previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', None))) + next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/'))) + previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) return delete_object(request, model=Role, object_id=role_id, template_name='generic_confirm.html', @@ -122,8 +122,8 @@ def permission_grant(request): items_property_list = loads(request.GET.get('items_property_list', [])) post_action_redirect = None - next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', None))) - previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', None))) + next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/'))) + previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) items = [] for item_properties in items_property_list: From 1f784aa6b0860aa39671d0faa64bb5510e57d09b Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 18 Nov 2011 18:29:59 -0400 Subject: [PATCH 04/78] Started the rename of the document grouping app to smart linking --- apps/grouping/__init__.py | 26 ++++++------- apps/grouping/forms.py | 2 +- apps/grouping/models.py | 10 ++--- apps/grouping/static/images/icons/link.png | Bin 0 -> 1282 bytes .../static/images/icons/link_delete.png | Bin 0 -> 1921 bytes apps/grouping/static/images/icons/package.png | Bin 972 -> 0 bytes .../static/images/icons/package_delete.png | Bin 1538 -> 0 bytes apps/grouping/views.py | 35 ++++++++---------- 8 files changed, 35 insertions(+), 38 deletions(-) create mode 100644 apps/grouping/static/images/icons/link.png create mode 100644 apps/grouping/static/images/icons/link_delete.png delete mode 100644 apps/grouping/static/images/icons/package.png delete mode 100644 apps/grouping/static/images/icons/package_delete.png diff --git a/apps/grouping/__init__.py b/apps/grouping/__init__.py index 4bfe65aea0..a30c52586d 100644 --- a/apps/grouping/__init__.py +++ b/apps/grouping/__init__.py @@ -9,22 +9,22 @@ from documents.models import Document from grouping.models import DocumentGroup -PERMISSION_DOCUMENT_GROUP_VIEW = {'namespace': 'grouping', 'name': 'group_view', 'label': _(u'View existing document groups')} -PERMISSION_DOCUMENT_GROUP_CREATE = {'namespace': 'grouping', 'name': 'group_create', 'label': _(u'Create new document groups')} -PERMISSION_DOCUMENT_GROUP_DELETE = {'namespace': 'grouping', 'name': 'group_delete', 'label': _(u'Delete document groups')} +PERMISSION_SMART_LINK_VIEW = {'namespace': 'grouping', 'name': 'group_view', 'label': _(u'View existing smart links')} +PERMISSION_SMART_LINK_CREATE = {'namespace': 'grouping', 'name': 'group_create', 'label': _(u'Create new smart links')} +PERMISSION_SMART_LINK_DELETE = {'namespace': 'grouping', 'name': 'group_delete', 'label': _(u'Delete smart links')} -set_namespace_title('grouping', _(u'Grouping')) -register_permission(PERMISSION_DOCUMENT_GROUP_VIEW) -register_permission(PERMISSION_DOCUMENT_GROUP_CREATE) -register_permission(PERMISSION_DOCUMENT_GROUP_DELETE) +set_namespace_title('grouping', _(u'Smart links')) +register_permission(PERMISSION_SMART_LINK_VIEW) +register_permission(PERMISSION_SMART_LINK_CREATE) +register_permission(PERMISSION_SMART_LINK_DELETE) -document_group_link = {'text': _(u'group actions'), 'view': 'document_group_view', 'famfam': 'package_go', 'permissions': [PERMISSION_DOCUMENT_VIEW]} -groups_for_document = {'text': _(u'groups'), 'view': 'groups_for_document', 'args': 'object.pk', 'famfam': 'package_go', 'permissions': [PERMISSION_DOCUMENT_VIEW]} +document_group_link = {'text': _(u'smart links actions'), 'view': 'document_group_view', 'famfam': 'page_link', 'permissions': [PERMISSION_DOCUMENT_VIEW]} +groups_for_document = {'text': _(u'smart links'), 'view': 'groups_for_document', 'args': 'object.pk', 'famfam': 'page_link', 'permissions': [PERMISSION_DOCUMENT_VIEW]} -document_groups_setup = {'text': _(u'document groups'), 'view': 'document_group_list', 'icon': 'package.png', 'permissions': [PERMISSION_DOCUMENT_GROUP_VIEW]} -document_group_list = {'text': _(u'document groups'), 'view': 'document_group_list', 'famfam': 'package', 'permissions': [PERMISSION_DOCUMENT_GROUP_VIEW]} -document_group_create = {'text': _(u'create new'), 'view': 'document_group_create', 'famfam': 'package_add', 'permissions': [PERMISSION_DOCUMENT_GROUP_CREATE]} -document_group_delete = {'text': _(u'delete'), 'view': 'document_group_delete', 'args': 'object.pk', 'famfam': 'package_delete', 'permissions': [PERMISSION_DOCUMENT_GROUP_DELETE]} +document_groups_setup = {'text': _(u'smart links'), 'view': 'document_group_list', 'icon': 'link.png', 'permissions': [PERMISSION_SMART_LINK_VIEW]} +document_group_list = {'text': _(u'smart links'), 'view': 'document_group_list', 'famfam': 'link', 'permissions': [PERMISSION_SMART_LINK_VIEW]} +document_group_create = {'text': _(u'create new'), 'view': 'document_group_create', 'famfam': 'link_add', 'permissions': [PERMISSION_SMART_LINK_CREATE]} +document_group_delete = {'text': _(u'delete'), 'view': 'document_group_delete', 'args': 'object.pk', 'famfam': 'link_delete', 'permissions': [PERMISSION_SMART_LINK_DELETE]} register_links(Document, [groups_for_document], menu_name='form_header') diff --git a/apps/grouping/forms.py b/apps/grouping/forms.py index 3c5d65792b..bd28945eda 100644 --- a/apps/grouping/forms.py +++ b/apps/grouping/forms.py @@ -83,7 +83,7 @@ class DocumentGroupImageWidget(forms.widgets.Widget): 'document_name': document, 'static_url': settings.STATIC_URL, 'tags_template': tags_template if tags_template else u'', - 'string': _(u'group document'), + 'string': _(u'smart links'), }) output.append(u'') output.append( diff --git a/apps/grouping/models.py b/apps/grouping/models.py index fc8bb76ed5..05841b2a85 100644 --- a/apps/grouping/models.py +++ b/apps/grouping/models.py @@ -17,12 +17,12 @@ class DocumentGroup(models.Model): return self.title class Meta: - verbose_name = _(u'document group') - verbose_name_plural = _(u'document groups') + verbose_name = _(u'smart link') + verbose_name_plural = _(u'smart links') class DocumentGroupItem(models.Model): - document_group = models.ForeignKey(DocumentGroup, verbose_name=_(u'document group')) + document_group = models.ForeignKey(DocumentGroup, verbose_name=_(u'smart link')) inclusion = models.CharField(default=INCLUSION_AND, max_length=16, choices=INCLUSION_CHOICES, help_text=_(u'The inclusion is ignored for the first item.')) foreign_document_data = models.CharField(max_length=32, verbose_name=_(u'foreign document data'), help_text=_(u'This represents the metadata of all other documents. Available objects: `document.` and `metadata.`.')) operator = models.CharField(max_length=16, choices=OPERATOR_CHOICES) @@ -36,5 +36,5 @@ class DocumentGroupItem(models.Model): return u'[%s] %s foreign %s %s %s %s' % (u'x' if self.enabled else u' ', self.get_inclusion_display(), self.foreign_document_data, _(u'not') if self.negated else u'', self.get_operator_display(), self.expression) class Meta: - verbose_name = _(u'group item') - verbose_name_plural = _(u'group items') + verbose_name = _(u'link condition') + verbose_name_plural = _(u'link conditions') diff --git a/apps/grouping/static/images/icons/link.png b/apps/grouping/static/images/icons/link.png new file mode 100644 index 0000000000000000000000000000000000000000..b0c35b59a32a35922e235ce13cfaea86fb337aa3 GIT binary patch literal 1282 zcmV+d1^xPoP)20c}Tt0!?8C z=xDn*fBM6aEEdGD1Q?8M&Y9_)i5akqKa4SPh|I~&Y>66U;$^0z!)-+4-0+8)a_I%U zEl{ADC53V5Mi*U6ZE3c)r#Ij4^t1vi!Z=-alJ}&^cTS)8d%x#>pXcx*kqA7@D}~&6 zJYKT@m)F|XS&}XiT?~c8d7)qsBB3zQ0s3OgyG?s>Js5wGR3OO-@Y_329w;g(R99>( z2cOpuF1H)(4hKw6O~ahc25s*je-xjiA4CHD*7gq$$cl>8H8s^>7#;>{#tH%24?=+u zBvL8ZY&U?Sti2z9cxpGPL`pJuz8GAqGl1D_2Ghj&r{+JenN(%vZ}@266FE6KnAB`7 zJ}(Q+YtOLuHVAAbFlu>?>CD>Xxb$L#{SQhBQJ&=DPBID+pZ;MLWYpw%0&4X0q{ z`gF%1zyGGk^}ESPAT<;U!MyVyu>XA< z_x1fmj>aCF1Dr`1!U?C>YN4>G0Cv{Z!slO}2A|IdTedvevmCrX3Vw^-eIywi0GG?@ zk|^X^yXtm;`b-yGy*dJ~A3X|$aQ%bYMBsNY!pRuLsc#>9XLj>ujF8&{$uO ztrlk3&RI$mtJ;5(+H>LDIb6T_)#)#`&CT6x%Fo{j8Ch9iw>wA$=96Md?@={$GOU^C zW_vie|CLv_%jJp=GG6-!w5V#-5>?}x0HbPNpP$$zKnwNJBJh}`eionYQh%=AnbHH2hq1bORO`x*Qe6b9y2~yrUDVF8gVkKsoJ_d9XpCe_*>rGa>6K)xosNU38rrDURE=u_q!PIp#RnS>pWGl6ZY7)`u~t>%ngG*^7}*$k7@6$xtXb8P skwAemnR1J#a!przMkS>B{!K=f2;$=brQXoyTGE zKit8?uV)wxv7ZPbAz26pAqpY*1O885`R)E4v>u#gyvX2u@#}Bq>0@F_cJ6o@Uat=> zw;Ohc1AV=H7`c8Ohkk!RLw0^;gK9Cs4;B9*FUDXfc`h#(P0cN6ZfPapUU=LdELoZa zK?q>o1M44ZZfg99>_co*)MA2D*d_3q^L4QG^uXHP^=DhpMQdjE=3n~!zIA4^8G{2u zIb^S+;aMyP?tnj=3k!i?vf0qx)p_{j-#_>Vt;jxt7*%ErtVXK@c$EoP>RB9BFCyVmuUXcGt)u_~1w%4Awz+i~MQ%`Oq z@Z-e!wFLh0R>pQTe23Sqc@jXL)f>q#nuv;fb%&J=JwdI zurQKgbdf5sBtRy#3Z2t7y-7O~%?& zhNR@2^)D16aM=o9%XtLtqv1PVvJBcEWI?_5L0ow6FK~^G|7}aNEnjhhzZW>8S4-8o z175EeJD&a-N=nP5Qp?KBMnI7E`^q<@ephFV&so1GAHIs?@SQykOnVR&6DJM!o6WJc z4P+B*GVLkdn7&ClJby1$>+EPhc;?JMzaVyV=I<>;Q)4p<^79cALsGW0ib&C_i&sAO z1pGChK^VIZUaJKktp*@xT9*%%FTo>z z-amR|%gD&J{VP_aAkJvSxZMG7Ajl|H8>NcMG9{QG^j`+YqC(NaEh>Ur)AG0}&9OCR z6K^&`!}FWurbH^k*{asVnu(6XvYs;n2|kne+)on{%*Bjet0h$%#D&iGUoq!HJP+<# zztr0qKkvLJfw;Fm4DM>bQU=kA17TI826A9vh#a`^b_BR2sU$3aoh>Z=#a&-E)k}#) z4!|`DUakG+?uFTqEja#m0Wb&DtW9FwYrsE)tHZ<9H1{tVd^gkcxxzx+UM7=F45-dS zE*DgBMojPA43V{5qBw_bIB=8?%!b*r|Ff7-B>rZtvtFmi_cJnnm%3`z9-UtQqiA*X z9~C|Coh>Y*GQ;UyL80B{^=(Wf0Salfge}gYn($dU0_9avaJbxD^W<2K)ON_-j>PFR5bdq-QeQ z9Cv@<=JeX-$;pxvyh=satdhP}Ec7(Tkppc#m(c5)uH1KV=m=v^mP0D}VIkuAijbMm|I$4MW6h z9|M|}tHvoGO88&AoN_X~>g0$j&lcqac;y3$_<#ERTYv!oyoAR(c6?{HTP#idWZ@v2OqcDHd4BW0&pR{AIOo{TOTji@I{@q_ecJT+k;jJ0M!+0S z$1WW8TgynS{s(hM8fleZD?(9pi;AQs+F%ytDiN$OMU7dc!qXzw^2GnZc$>W%tdWgZxuUa5ji}@2S)pzI-W=d1~+nboo!EVuhX*ba#i5Ookvzn{?$}qyfAN)^`yfpI*n{mDwf; z)+~O=I(Xp$Y9DS23()LU6xYhgWeX^*SSXds2!{OVhz5~JgyAkBGVF^5Eagm?YbE&o z2Ex01h{nPQgba#;EiItzzlyi_&Jo0$280V>%#06O{gGag=}^e>4h9kz(O#nBN`8*z1@e%@%xy)GmL>zCmjMn ze|=|Yp~`&m#Z(TlWE9cP2$H>Vgd%N@^&9Pncr`hZHOJC5MZK-%SCN{}AeYV~OBxB% zuq&nKN3Xm*MR5ittr{N~xkix^CC2J{9Hv`#f4N#a8DFBq3)N;e3HIh|XAspRd@)CgQQp;VhwML(9LeVMzit&pr z;-3g;O>ndt8;+X-R-x5Rtw+F10LGJGer9c=^^%j6DQmY9- z6oeu?AWSIFC>{dP_(F?s^Z-zVN-5wTACj1GAvQ!nY#>yeQ~+WRXWjxZF0~lj2%y)Nms*F1?%Q|?Bjl-llIH=rCDX3B ug{N^|KM|SR{aQc^e%z_F|){C0W0IIcuSr5G452^j4}O5RUoSf1wOdgME{D_05Es$S&?sidA05Es%4&+-_ZvqRRp&$vg zseo{_fIFi(_&gT)=UXR%U{Z0A#zT=j8fe_>Hq#shP!y>CmoaeuFajTSP6NTFDc{wW zU%YWA)PZklR04NDcPZh}?!jOhBO~KP9tBpLNn4c57U`dW1q+<;`yE=SNN+i09E4f| zYk3FPe*7GPt(OfTm{IxCeCylaLHYYDP64uBL^fSOG%}9yWFEO(0d|KK^W1j$d`?sj zkmmN#IL4wGWYRfUt!6lDEpU6BusO`+1({}*AS~H|tB2ks5LYHZQ~;Q}_XP57AD&0- z>?XwHc|^nG$YhGNfd%s$-SBv7V6$5^tI7gH3>YhWh)Xt;MXT zzTN`Yy2H4Hd-=eyu<^1|V&K{?nJnc^=46PeMM z6;kMuj-?R19Y%CCh6w5Kkq)_-`)TFYORtijVY7&G`;!|#CQp%Nw7RTMMMhUL(_B?P zjl`b@pvlkhh=x<8$TIXC-`UP}a{v}qmLWC0i15+XfM}VFj5^!dhBlP*@VS?WN>&J>1K@My3(X30x}5dhQ-y{sO9X1PT`-@!hl2So(7 zZ$s%u57I{tpcD?4m3aNAf9833wts>v`}g6-{v!~4q7i^9XDI}nfa1dgU>vW2~IIMb%r?ODe#`v6t8`fj+k7IX*fQTVd2-GT?Bo06{O9*$2 zV$qs;$X)0Iqok%53ioUnm?%OX=!2_mC9V&ZCIM(2j9MTT^BP`V{Iy(0gc5Jv1aT2{ zr!U`kO=81xu-iAGO*R0KWH76vI{xbb1hPy`j1h;d4_?CO#K$Ib4F{k`Q}XZKCr++X zs;oL%`Lzke9S$H4v9=DWH(sEGQ^K?g#^*|sHb;ra4S689_Os4+>F$}Dn5wv>qiu`7 zjA!xzkBc_9SoKcm9?6P#R&+Wd@dT13xeFRKwX}pGl2G+2P`hluj>W%jzGFY+^LezB zr0}9M-2oJ_vfKX+hH)+w-^&{ke&`jwJ<5=zXCT?GL@K(*Q`=HZrxrCfG-x0=FBr9S z{4R3W(>vbWcK&q@{qfYP9le9!(AqI^7K$u4KshlI?OIh=)0|7jAG294a5$ZmBP>KZ zljizpEQX%Geq4(tPruc9{Zm?Vhl6LcROz`PbtH|Ontji$_q*P6OJeKPwmn*subv9V z4jdi|U7)|i{6z9HF Date: Fri, 18 Nov 2011 21:03:08 -0400 Subject: [PATCH 05/78] Finished adding CRUD views for document groupping and document groupping items, started naming change to smart links and smart link conditions --- apps/grouping/__init__.py | 24 ++- apps/grouping/forms.py | 11 +- apps/grouping/models.py | 4 +- .../static/images/icons/cog_delete.png | Bin 0 -> 2273 bytes apps/grouping/urls.py | 6 + apps/grouping/views.py | 162 ++++++++++++++++-- 6 files changed, 185 insertions(+), 22 deletions(-) create mode 100644 apps/grouping/static/images/icons/cog_delete.png diff --git a/apps/grouping/__init__.py b/apps/grouping/__init__.py index a30c52586d..870daeecf0 100644 --- a/apps/grouping/__init__.py +++ b/apps/grouping/__init__.py @@ -6,29 +6,39 @@ from project_setup.api import register_setup from documents.literals import PERMISSION_DOCUMENT_VIEW from documents.models import Document -from grouping.models import DocumentGroup +from grouping.models import DocumentGroup, DocumentGroupItem PERMISSION_SMART_LINK_VIEW = {'namespace': 'grouping', 'name': 'group_view', 'label': _(u'View existing smart links')} PERMISSION_SMART_LINK_CREATE = {'namespace': 'grouping', 'name': 'group_create', 'label': _(u'Create new smart links')} PERMISSION_SMART_LINK_DELETE = {'namespace': 'grouping', 'name': 'group_delete', 'label': _(u'Delete smart links')} +PERMISSION_SMART_LINK_EDIT = {'namespace': 'grouping', 'name': 'group_edit', 'label': _(u'Edit smart links')} set_namespace_title('grouping', _(u'Smart links')) register_permission(PERMISSION_SMART_LINK_VIEW) register_permission(PERMISSION_SMART_LINK_CREATE) register_permission(PERMISSION_SMART_LINK_DELETE) +register_permission(PERMISSION_SMART_LINK_EDIT) document_group_link = {'text': _(u'smart links actions'), 'view': 'document_group_view', 'famfam': 'page_link', 'permissions': [PERMISSION_DOCUMENT_VIEW]} groups_for_document = {'text': _(u'smart links'), 'view': 'groups_for_document', 'args': 'object.pk', 'famfam': 'page_link', 'permissions': [PERMISSION_DOCUMENT_VIEW]} -document_groups_setup = {'text': _(u'smart links'), 'view': 'document_group_list', 'icon': 'link.png', 'permissions': [PERMISSION_SMART_LINK_VIEW]} -document_group_list = {'text': _(u'smart links'), 'view': 'document_group_list', 'famfam': 'link', 'permissions': [PERMISSION_SMART_LINK_VIEW]} -document_group_create = {'text': _(u'create new'), 'view': 'document_group_create', 'famfam': 'link_add', 'permissions': [PERMISSION_SMART_LINK_CREATE]} -document_group_delete = {'text': _(u'delete'), 'view': 'document_group_delete', 'args': 'object.pk', 'famfam': 'link_delete', 'permissions': [PERMISSION_SMART_LINK_DELETE]} +document_groups_setup = {'text': _(u'smart links'), 'view': 'document_group_list', 'icon': 'link.png', 'permissions': [PERMISSION_SMART_LINK_CREATE]} +document_group_list = {'text': _(u'smart links list'), 'view': 'document_group_list', 'famfam': 'link', 'permissions': [PERMISSION_SMART_LINK_CREATE]} +document_group_create = {'text': _(u'create new smart link'), 'view': 'document_group_create', 'famfam': 'link_add', 'permissions': [PERMISSION_SMART_LINK_CREATE]} +document_group_edit = {'text': _(u'edit'), 'view': 'document_group_edit', 'args': 'smart_link.pk', 'famfam': 'link_edit', 'permissions': [PERMISSION_SMART_LINK_EDIT]} +document_group_delete = {'text': _(u'delete'), 'view': 'document_group_delete', 'args': 'smart_link.pk', 'famfam': 'link_delete', 'permissions': [PERMISSION_SMART_LINK_DELETE]} + +smart_link_condition_list = {'text': _(u'conditions'), 'view': 'smart_link_condition_list', 'args': 'smart_link.pk', 'famfam': 'cog', 'permissions': [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_CREATE]} +smart_link_condition_create = {'text': _(u'create condition'), 'view': 'smart_link_condition_create', 'args': 'smart_link.pk', 'famfam': 'cog_add', 'permissions': [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]} +smart_link_condition_edit = {'text': _(u'edit'), 'view': 'smart_link_condition_edit', 'args': 'condition.pk', 'famfam': 'cog_edit', 'permissions': [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]} +smart_link_condition_delete = {'text': _(u'delete'), 'view': 'smart_link_condition_delete', 'args': 'condition.pk', 'famfam': 'cog_delete', 'permissions': [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]} register_links(Document, [groups_for_document], menu_name='form_header') -register_links(DocumentGroup, [document_group_delete]) -register_links(['document_group_list', 'document_group_create', 'document_group_delete'], [document_group_list, document_group_create], menu_name='sidebar') +register_links(DocumentGroup, [document_group_edit, document_group_delete, smart_link_condition_list]) +register_links(DocumentGroupItem, [smart_link_condition_edit, smart_link_condition_delete]) +register_links(['document_group_list', 'document_group_create', 'document_group_edit', 'document_group_delete', 'smart_link_condition_list', 'smart_link_condition_create', 'smart_link_condition_edit', 'smart_link_condition_delete'], [document_group_list, document_group_create], menu_name='sidebar') +register_links(['smart_link_condition_list', 'smart_link_condition_create', 'smart_link_condition_edit', 'smart_link_condition_delete'], [smart_link_condition_create], menu_name='sidebar') register_setup(document_groups_setup) diff --git a/apps/grouping/forms.py b/apps/grouping/forms.py index bd28945eda..7dd9c69242 100644 --- a/apps/grouping/forms.py +++ b/apps/grouping/forms.py @@ -8,13 +8,22 @@ from django.conf import settings from tags.widgets import get_tags_inline_widget -from grouping.models import DocumentGroup +from grouping.models import DocumentGroup, DocumentGroupItem class DocumentGroupForm(forms.ModelForm): class Meta: model = DocumentGroup + +class DocumentGroupItemForm(forms.ModelForm): + class Meta: + model = DocumentGroupItem + + def __init__(self, *args, **kwargs): + super(DocumentGroupItemForm, self).__init__(*args, **kwargs) + self.fields['document_group'].widget = forms.HiddenInput() + class DocumentGroupImageWidget(forms.widgets.Widget): def render(self, name, value, attrs=None): diff --git a/apps/grouping/models.py b/apps/grouping/models.py index 05841b2a85..bd8e3d1348 100644 --- a/apps/grouping/models.py +++ b/apps/grouping/models.py @@ -8,7 +8,7 @@ from grouping.literals import OPERATOR_CHOICES, INCLUSION_AND, \ class DocumentGroup(models.Model): title = models.CharField(max_length=96, verbose_name=_(u'title')) - dynamic_title = models.CharField(blank=True, max_length=96, verbose_name=_(u'dynamic title')) + dynamic_title = models.CharField(blank=True, max_length=96, verbose_name=_(u'dynamic title'), help_text=_(u'This expression will be evaluated against the current selected document. The document metadata is available as variables `metadata` and document properties under the variable `document`.')) enabled = models.BooleanField(default=True, verbose_name=_(u'enabled')) objects = DocumentGroupManager() @@ -33,7 +33,7 @@ class DocumentGroupItem(models.Model): enabled = models.BooleanField(default=True, verbose_name=_(u'enabled')) def __unicode__(self): - return u'[%s] %s foreign %s %s %s %s' % (u'x' if self.enabled else u' ', self.get_inclusion_display(), self.foreign_document_data, _(u'not') if self.negated else u'', self.get_operator_display(), self.expression) + return u'%s foreign %s %s %s %s' % (self.get_inclusion_display(), self.foreign_document_data, _(u'not') if self.negated else u'', self.get_operator_display(), self.expression) class Meta: verbose_name = _(u'link condition') diff --git a/apps/grouping/static/images/icons/cog_delete.png b/apps/grouping/static/images/icons/cog_delete.png new file mode 100644 index 0000000000000000000000000000000000000000..924e04d69ca011457c71bc0a76f992fbc0f1fda3 GIT binary patch literal 2273 zcmV<72p;!|P)scq%-Os9z0T|R{my}d|Njw>9DVaF0B4dU zDKNnAl47rS7Cx>1CvY4OvMj+9rXS52q*V-1RUA1siczY6sqBR<-xn2r zfidA=`RgCAT$%nvT3V{U{Ix&+7soVZFTT9Z?ePFcfrXtsr#Jnkahj)fw4V0SemaNF zyPE;?Cxrv$ul_A7BV$WZQ6a>~$AiHT6_J>?~N8k_>j6Eh9TOH>dW(`7;ERg$&?=!+x;*wW_SFtj$G* z>)}d6BRHKdShFS{Wb{(PaFgQE(NU({@4dt%CME`!E=>d;)57r_G&VLvNQf5J{$LGU ztgDCi_V$15+w<~M3#B6%10g&wtdnIKW-+9soDOepz6w!Mk*Ii#<((9B90#EH z%*qUli7~>b8C`{^87N9TSQNP|R~XsPoTK(W&mAox$fc@|5_BE@XNfsRSfYi zmnV>K%rnEqvSrDTxFi8$?$$;E`N1iK|)g2(NKn7BnfO^pq;boO_FP<&H6x;1FED-a1@y_|>OzXjI9k zrsng{E|jBV%KXT&8BG@n$4|d3Om*4H6ynpek!I(S}f3YLKF*l0Q*j%<9X(VTBU~6wDkA|$iy2F5uv4dALd`g zv{AkV%DeKNN5>@8D=$EX;v5!LxR`(@Nj{jJ^&#u^1qjDxpMlJ*Oz7(F{u3^)nrDO< zzj#Rn&D)#~;58cH@M=nEcv2#mUX$^o?oM zA8r#KxdN5APacEhb%h|bH-c~A7I2sv4nfW4sXpNKQu43>lGanR)#fTPorc6wm! zbZ|!$;&eJ%C?Io0M1@AD3u9jEkyf=@E#nF-uYRHL=%MG|7a=M(8hkgqfur^eYXLYg zpbncNb~Z+HzMEYT6|Dyi&lix+;C?`U_no&&w(t1GagS%V&}1^PD`>G;;M}=SL9dU1 zty{O^TaR_#=SKzcghoZ3De(QbkV63U)z|LYgiihBLE!ps&Rqp{umNZ`3{`&kzFC%} zqv&BF4YstjjGp+*pPzZ8c*DaEhjZU<%4tZ)Ega|#@e*P%P zm@<&(u^wTD*b&!ASY_w9etui-Q#u#!gZJOtsmy<)6dwa&gPKw|wPQTT07jJCAD0Lj zb{80ohyw(r>@Na$6n=kc7hw!b{j}HDLe4Jqf5Ds)E`=wYO40GU-R{c=4jd{V@0k*6 zOcAI?!{W}&p`Mk-MR9%_R%*+<0I;T&p5CER_-thI7&R%}J^T|BPF*;PabD!eV6Jji zia8@T`pJA`Mk)X@tAO%t*B32=CS=jB@#(5\d+)/delete/$', 'document_group_delete', (), 'document_group_delete'), + url(r'^setup/(?P\d+)/edit/$', 'document_group_edit', (), 'document_group_edit'), + + url(r'^setup/(?P\d+)/condition/list/$', 'smart_link_condition_list', (), 'smart_link_condition_list'), + url(r'^setup/(?P\d+)/condition/create/$', 'smart_link_condition_create', (), 'smart_link_condition_create'), + url(r'^setup/smart_link/condition/(?P\d+)/edit/$', 'smart_link_condition_edit', (), 'smart_link_condition_edit'), + url(r'^setup/smart_link/condition/(?P\d+)/delete/$', 'smart_link_condition_delete', (), 'smart_link_condition_delete'), ) diff --git a/apps/grouping/views.py b/apps/grouping/views.py index 4dccf5d56f..c9483b0e0c 100644 --- a/apps/grouping/views.py +++ b/apps/grouping/views.py @@ -13,15 +13,19 @@ from documents.views import document_list from permissions.api import check_permissions -from grouping.models import DocumentGroup +from grouping.models import DocumentGroup, DocumentGroupItem from grouping.conf.settings import SHOW_EMPTY_GROUPS -from grouping.forms import DocumentDataGroupForm, DocumentGroupForm +from grouping.forms import (DocumentDataGroupForm, DocumentGroupForm, + DocumentGroupItemForm) from grouping import document_group_link -from grouping import PERMISSION_SMART_LINK_VIEW, \ - PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_DELETE +from grouping import (PERMISSION_SMART_LINK_VIEW, + PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_DELETE, + PERMISSION_SMART_LINK_EDIT) def document_group_action(request): + check_permissions(request.user, [PERMISSION_SMART_LINK_VIEW]) + action = request.GET.get('action', None) if not action: @@ -32,6 +36,8 @@ def document_group_action(request): def document_group_view(request, document_id, document_group_id): + check_permissions(request.user, [PERMISSION_SMART_LINK_VIEW]) + document = get_object_or_404(Document, pk=document_id) document_group = get_object_or_404(DocumentGroup, pk=document_group_id) object_list, errors = DocumentGroup.objects.get_groups_for(document, document_group) @@ -49,6 +55,8 @@ def document_group_view(request, document_id, document_group_id): def groups_for_document(request, document_id): + check_permissions(request.user, [PERMISSION_SMART_LINK_VIEW]) + subtemplates_list = [] document = get_object_or_404(Document, pk=document_id) document_groups, errors = DocumentGroup.objects.get_groups_for(document) @@ -91,7 +99,7 @@ def groups_for_document(request, document_id): def document_group_list(request): - check_permissions(request.user, [PERMISSION_SMART_LINK_VIEW]) + check_permissions(request.user, [PERMISSION_SMART_LINK_CREATE]) return render_to_response('generic_list.html', { 'title': _(u'smart links'), @@ -101,6 +109,8 @@ def document_group_list(request): {'name': _(u'enabled'), 'attribute': encapsulate(lambda x: two_state_template(x.enabled))}, ], 'hide_link': True, + 'list_object_variable_name': 'smart_link', + }, context_instance=RequestContext(request)) @@ -122,31 +132,159 @@ def document_group_create(request): }, context_instance=RequestContext(request)) +def document_group_edit(request, document_group_id): + check_permissions(request.user, [PERMISSION_SMART_LINK_EDIT]) + + smart_link = get_object_or_404(DocumentGroup, pk=document_group_id) + + if request.method == 'POST': + form = DocumentGroupForm(request.POST, instance=smart_link) + if form.is_valid(): + smart_link = form.save() + messages.success(request, _(u'Smart link: %s edited successfully.') % smart_link) + return HttpResponseRedirect(reverse('document_group_list')) + else: + form = DocumentGroupForm(instance=smart_link) + + return render_to_response('generic_form.html', { + 'navigation_object_name': 'smart_link', + 'smart_link': smart_link, + 'form': form, + 'title': _(u'Edit smart link: %s') % smart_link + }, context_instance=RequestContext(request)) + + def document_group_delete(request, document_group_id): check_permissions(request.user, [PERMISSION_SMART_LINK_DELETE]) - document_group = get_object_or_404(DocumentGroup, pk=document_group_id) + smart_link = get_object_or_404(DocumentGroup, pk=document_group_id) next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/'))) previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) if request.method == 'POST': try: - document_group.delete() - messages.success(request, _(u'Smart link: %s deleted successfully.') % document_group) + smart_link.delete() + messages.success(request, _(u'Smart link: %s deleted successfully.') % smart_link) except Exception, error: - messages.error(request, _(u'Error deleting smart link: %(document_group)s; %(error)s.') % { - 'document_group': document_group, + messages.error(request, _(u'Error deleting smart link: %(smart_link)s; %(error)s.') % { + 'smart_link': smart_link, 'error': error }) return HttpResponseRedirect(next) return render_to_response('generic_confirm.html', { 'delete_view': True, - 'object': document_group, + 'navigation_object_name': 'smart_link', + 'smart_link': smart_link, + 'title': _(u'Are you sure you wish to delete smart link: %s?') % smart_link, 'next': next, 'previous': previous, 'form_icon': u'link_delete.png', - #'temporary_navigation_links': {'form_header': {'staging_file_delete': {'links': results['tab_links']}}}, }, context_instance=RequestContext(request)) + +def smart_link_condition_list(request, smart_link_pk): + check_permissions(request.user, [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]) + + smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) + + return render_to_response('generic_list.html', { + 'title': _(u'conditions for smart link: %s') % smart_link, + 'object_list': smart_link.documentgroupitem_set.all(), + 'extra_columns': [ + {'name': _(u'enabled'), 'attribute': encapsulate(lambda x: two_state_template(x.enabled))}, + ], + 'hide_link': True, + 'smart_link': smart_link, + 'navigation_object_name': 'smart_link', + 'list_object_variable_name': 'condition', + }, context_instance=RequestContext(request)) + + +def smart_link_condition_create(request, smart_link_pk): + check_permissions(request.user, [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]) + + smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) + + if request.method == 'POST': + form = DocumentGroupItemForm(request.POST, initial={'document_group': smart_link}) + if form.is_valid(): + smart_link_condition = form.save() + messages.success(request, _(u'Smart link condition: "%s" created successfully.') % smart_link_condition) + return HttpResponseRedirect(reverse('smart_link_condition_list', args=[smart_link.pk])) + else: + form = DocumentGroupItemForm(initial={'document_group': smart_link}) + + return render_to_response('generic_form.html', { + 'form': form, + 'title': _(u'Add new conditions to smart link: "%s"') % smart_link, + 'navigation_object_name': 'smart_link', + 'smart_link': smart_link, + }, context_instance=RequestContext(request)) + + +def smart_link_condition_edit(request, smart_link_condition_pk): + check_permissions(request.user, [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]) + + smart_link_condition = get_object_or_404(DocumentGroupItem, pk=smart_link_condition_pk) + + next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/'))) + previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) + + if request.method == 'POST': + form = DocumentGroupItemForm(request.POST, instance=smart_link_condition) + if form.is_valid(): + smart_link_condition = form.save() + messages.success(request, _(u'Smart link condition: "%s" created successfully.') % smart_link_condition) + return HttpResponseRedirect(next) + else: + form = DocumentGroupItemForm(instance=smart_link_condition) + + return render_to_response('generic_form.html', { + 'form': form, + 'title': _(u'Edit smart link condition'), + 'next': next, + 'previous': previous, + 'condition': smart_link_condition, + 'smart_link': smart_link_condition.document_group, + 'navigation_object_list': [ + {'object': 'smart_link', 'name': _(u'smart link')}, + {'object': 'condition', 'name': _(u'condition')} + ], + + }, context_instance=RequestContext(request)) + + +def smart_link_condition_delete(request, smart_link_condition_pk): + check_permissions(request.user, [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]) + + smart_link_condition = get_object_or_404(DocumentGroupItem, pk=smart_link_condition_pk) + + next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/'))) + previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) + + if request.method == 'POST': + try: + smart_link_condition.delete() + messages.success(request, _(u'Smart link condition: "%s" deleted successfully.') % smart_link_condition) + except Exception, error: + messages.error(request, _(u'Error deleting smart link condition: %(smart_link_condition)s; %(error)s.') % { + 'smart_link_condition': smart_link_condition, + 'error': error + }) + return HttpResponseRedirect(next) + + return render_to_response('generic_confirm.html', { + 'delete_view': True, + 'condition': smart_link_condition, + 'smart_link': smart_link_condition.document_group, + 'navigation_object_list': [ + {'object': 'smart_link', 'name': _(u'smart link')}, + {'object': 'condition', 'name': _(u'condition')} + ], + 'title': _(u'Are you sure you wish to delete smart link condition: "%s"?') % smart_link_condition, + 'next': next, + 'previous': previous, + 'form_icon': u'cog_delete.png', + }, context_instance=RequestContext(request)) From 9f4112187e6b5bc0cbb3238f42e0e5bb9be0a54e Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 18 Nov 2011 23:08:01 -0400 Subject: [PATCH 06/78] Further document grouping rename --- apps/grouping/__init__.py | 6 ++--- apps/grouping/forms.py | 15 ++++++----- apps/grouping/urls.py | 10 ++++---- apps/grouping/views.py | 54 +++++++++++++++++++-------------------- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/apps/grouping/__init__.py b/apps/grouping/__init__.py index 870daeecf0..18b0ce37f4 100644 --- a/apps/grouping/__init__.py +++ b/apps/grouping/__init__.py @@ -20,8 +20,8 @@ register_permission(PERMISSION_SMART_LINK_CREATE) register_permission(PERMISSION_SMART_LINK_DELETE) register_permission(PERMISSION_SMART_LINK_EDIT) -document_group_link = {'text': _(u'smart links actions'), 'view': 'document_group_view', 'famfam': 'page_link', 'permissions': [PERMISSION_DOCUMENT_VIEW]} -groups_for_document = {'text': _(u'smart links'), 'view': 'groups_for_document', 'args': 'object.pk', 'famfam': 'page_link', 'permissions': [PERMISSION_DOCUMENT_VIEW]} +smart_link_instance_view_link = {'text': _(u'smart links actions'), 'view': 'smart_link_instance_view', 'famfam': 'page_link', 'permissions': [PERMISSION_DOCUMENT_VIEW]} +smart_link_instances_for_document = {'text': _(u'smart links'), 'view': 'smart_link_instances_for_document', 'args': 'object.pk', 'famfam': 'page_link', 'permissions': [PERMISSION_DOCUMENT_VIEW]} document_groups_setup = {'text': _(u'smart links'), 'view': 'document_group_list', 'icon': 'link.png', 'permissions': [PERMISSION_SMART_LINK_CREATE]} document_group_list = {'text': _(u'smart links list'), 'view': 'document_group_list', 'famfam': 'link', 'permissions': [PERMISSION_SMART_LINK_CREATE]} @@ -34,7 +34,7 @@ smart_link_condition_create = {'text': _(u'create condition'), 'view': 'smart_li smart_link_condition_edit = {'text': _(u'edit'), 'view': 'smart_link_condition_edit', 'args': 'condition.pk', 'famfam': 'cog_edit', 'permissions': [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]} smart_link_condition_delete = {'text': _(u'delete'), 'view': 'smart_link_condition_delete', 'args': 'condition.pk', 'famfam': 'cog_delete', 'permissions': [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]} -register_links(Document, [groups_for_document], menu_name='form_header') +register_links(Document, [smart_link_instances_for_document], menu_name='form_header') register_links(DocumentGroup, [document_group_edit, document_group_delete, smart_link_condition_list]) register_links(DocumentGroupItem, [smart_link_condition_edit, smart_link_condition_delete]) diff --git a/apps/grouping/forms.py b/apps/grouping/forms.py index 7dd9c69242..0aa5df627e 100644 --- a/apps/grouping/forms.py +++ b/apps/grouping/forms.py @@ -11,12 +11,12 @@ from tags.widgets import get_tags_inline_widget from grouping.models import DocumentGroup, DocumentGroupItem -class DocumentGroupForm(forms.ModelForm): +class SmartLinkForm(forms.ModelForm): class Meta: model = DocumentGroup -class DocumentGroupItemForm(forms.ModelForm): +class SmartLinkConditionForm(forms.ModelForm): class Meta: model = DocumentGroupItem @@ -25,9 +25,10 @@ class DocumentGroupItemForm(forms.ModelForm): self.fields['document_group'].widget = forms.HiddenInput() -class DocumentGroupImageWidget(forms.widgets.Widget): +class SmartLinkImageWidget(forms.widgets.Widget): def render(self, name, value, attrs=None): output = [] + # TODO: convert to navigation app if value['links']: output.append(u'') @@ -102,15 +103,15 @@ class DocumentGroupImageWidget(forms.widgets.Widget): return mark_safe(u''.join(output)) -class DocumentDataGroupForm(forms.Form): +class SmartLinkInstanceForm(forms.Form): def __init__(self, *args, **kwargs): groups = kwargs.pop('groups', None) links = kwargs.pop('links', None) current_document = kwargs.pop('current_document', None) - super(DocumentDataGroupForm, self).__init__(*args, **kwargs) + super(SmartLinkInstanceForm, self).__init__(*args, **kwargs) for group, data in groups.items(): self.fields['preview-%s' % group] = forms.CharField( - widget=DocumentGroupImageWidget(), + widget=SmartLinkImageWidget(), label=u'%s (%d)' % (unicode(data['title']), len(data['documents'])), required=False, initial={ diff --git a/apps/grouping/urls.py b/apps/grouping/urls.py index 5bb89e3da6..74a852a69a 100644 --- a/apps/grouping/urls.py +++ b/apps/grouping/urls.py @@ -1,14 +1,14 @@ from django.conf.urls.defaults import patterns, url urlpatterns = patterns('grouping.views', - url(r'^action/$', 'document_group_action', (), 'document_group_action'), - url(r'^document/(?P\d+)/group/(?P\d+)/$', 'document_group_view', (), 'document_group_view'), - url(r'^groups/for_document/(?P\d+)/$', 'groups_for_document', (), 'groups_for_document'), + url(r'^action/$', 'smart_link_action', (), 'smart_link_action'), + url(r'^document/(?P\d+)/smart_link/(?P\d+)/$', 'smart_link_instance_view', (), 'smart_link_instance_view'), + url(r'^smart/for_document/(?P\d+)/$', 'smart_link_instances_for_document', (), 'smart_link_instances_for_document'), url(r'^setup/list/$', 'document_group_list', (), 'document_group_list'), url(r'^setup/create/$', 'document_group_create', (), 'document_group_create'), - url(r'^setup/(?P\d+)/delete/$', 'document_group_delete', (), 'document_group_delete'), - url(r'^setup/(?P\d+)/edit/$', 'document_group_edit', (), 'document_group_edit'), + url(r'^setup/(?P\d+)/delete/$', 'document_group_delete', (), 'document_group_delete'), + url(r'^setup/(?P\d+)/edit/$', 'document_group_edit', (), 'document_group_edit'), url(r'^setup/(?P\d+)/condition/list/$', 'smart_link_condition_list', (), 'smart_link_condition_list'), url(r'^setup/(?P\d+)/condition/create/$', 'smart_link_condition_create', (), 'smart_link_condition_create'), diff --git a/apps/grouping/views.py b/apps/grouping/views.py index c9483b0e0c..348538d38a 100644 --- a/apps/grouping/views.py +++ b/apps/grouping/views.py @@ -15,15 +15,15 @@ from permissions.api import check_permissions from grouping.models import DocumentGroup, DocumentGroupItem from grouping.conf.settings import SHOW_EMPTY_GROUPS -from grouping.forms import (DocumentDataGroupForm, DocumentGroupForm, - DocumentGroupItemForm) -from grouping import document_group_link +from grouping.forms import (SmartLinkInstanceForm, SmartLinkForm, + SmartLinkConditionForm) +from grouping import smart_link_instance_view_link from grouping import (PERMISSION_SMART_LINK_VIEW, PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_DELETE, PERMISSION_SMART_LINK_EDIT) -def document_group_action(request): +def smart_link_action(request): check_permissions(request.user, [PERMISSION_SMART_LINK_VIEW]) action = request.GET.get('action', None) @@ -35,12 +35,12 @@ def document_group_action(request): return HttpResponseRedirect(action) -def document_group_view(request, document_id, document_group_id): +def smart_link_instance_view(request, document_id, smart_link_pk): check_permissions(request.user, [PERMISSION_SMART_LINK_VIEW]) document = get_object_or_404(Document, pk=document_id) - document_group = get_object_or_404(DocumentGroup, pk=document_group_id) - object_list, errors = DocumentGroup.objects.get_groups_for(document, document_group) + smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) + object_list, errors = DocumentGroup.objects.get_groups_for(document, smart_link) return document_list( request, @@ -54,12 +54,12 @@ def document_group_view(request, document_id, document_group_id): ) -def groups_for_document(request, document_id): +def smart_link_instances_for_document(request, document_id): check_permissions(request.user, [PERMISSION_SMART_LINK_VIEW]) subtemplates_list = [] document = get_object_or_404(Document, pk=document_id) - document_groups, errors = DocumentGroup.objects.get_groups_for(document) + smart_links, errors = DocumentGroup.objects.get_groups_for(document) if (request.user.is_staff or request.user.is_superuser) and errors: for error in errors: messages.warning(request, _(u'Smart link query error: %s' % error)) @@ -69,16 +69,16 @@ def groups_for_document(request, document_id): #dictionary document_groups = dict([(group, data) for group, data in document_groups.items() if data['documents']]) - if document_groups: + if smart_links: subtemplates_list = [{ 'name': 'generic_form_subtemplate.html', 'context': { - 'title': _(u'smart links (%s)') % len(document_groups.keys()), - 'form': DocumentDataGroupForm( - groups=document_groups, current_document=document, - links=[document_group_link] + 'title': _(u'smart links (%s)') % len(smart_links.keys()), + 'form': SmartLinkInstanceForm( + groups=smart_links, current_document=document, + links=[smart_link_instance_view_link] ), - 'form_action': reverse('document_group_action'), + 'form_action': reverse('smart_link_action'), 'submit_method': 'GET', } }] @@ -118,13 +118,13 @@ def document_group_create(request): check_permissions(request.user, [PERMISSION_SMART_LINK_CREATE]) if request.method == 'POST': - form = DocumentGroupForm(request.POST) + form = SmartLinkForm(request.POST) if form.is_valid(): document_group = form.save() messages.success(request, _(u'Smart link: %s created successfully.') % document_group) return HttpResponseRedirect(reverse('document_group_list')) else: - form = DocumentGroupForm() + form = SmartLinkForm() return render_to_response('generic_form.html', { 'form': form, @@ -132,19 +132,19 @@ def document_group_create(request): }, context_instance=RequestContext(request)) -def document_group_edit(request, document_group_id): +def document_group_edit(request, smart_link_pk): check_permissions(request.user, [PERMISSION_SMART_LINK_EDIT]) - smart_link = get_object_or_404(DocumentGroup, pk=document_group_id) + smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) if request.method == 'POST': - form = DocumentGroupForm(request.POST, instance=smart_link) + form = SmartLinkForm(request.POST, instance=smart_link) if form.is_valid(): smart_link = form.save() messages.success(request, _(u'Smart link: %s edited successfully.') % smart_link) return HttpResponseRedirect(reverse('document_group_list')) else: - form = DocumentGroupForm(instance=smart_link) + form = SmartLinkForm(instance=smart_link) return render_to_response('generic_form.html', { 'navigation_object_name': 'smart_link', @@ -154,10 +154,10 @@ def document_group_edit(request, document_group_id): }, context_instance=RequestContext(request)) -def document_group_delete(request, document_group_id): +def document_group_delete(request, smart_link_pk): check_permissions(request.user, [PERMISSION_SMART_LINK_DELETE]) - smart_link = get_object_or_404(DocumentGroup, pk=document_group_id) + smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/'))) previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) @@ -208,13 +208,13 @@ def smart_link_condition_create(request, smart_link_pk): smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) if request.method == 'POST': - form = DocumentGroupItemForm(request.POST, initial={'document_group': smart_link}) + form = SmartLinkConditionForm(request.POST, initial={'document_group': smart_link}) if form.is_valid(): smart_link_condition = form.save() messages.success(request, _(u'Smart link condition: "%s" created successfully.') % smart_link_condition) return HttpResponseRedirect(reverse('smart_link_condition_list', args=[smart_link.pk])) else: - form = DocumentGroupItemForm(initial={'document_group': smart_link}) + form = SmartLinkConditionForm(initial={'document_group': smart_link}) return render_to_response('generic_form.html', { 'form': form, @@ -233,13 +233,13 @@ def smart_link_condition_edit(request, smart_link_condition_pk): previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) if request.method == 'POST': - form = DocumentGroupItemForm(request.POST, instance=smart_link_condition) + form = SmartLinkConditionForm(request.POST, instance=smart_link_condition) if form.is_valid(): smart_link_condition = form.save() messages.success(request, _(u'Smart link condition: "%s" created successfully.') % smart_link_condition) return HttpResponseRedirect(next) else: - form = DocumentGroupItemForm(instance=smart_link_condition) + form = SmartLinkConditionForm(instance=smart_link_condition) return render_to_response('generic_form.html', { 'form': form, From 7a32eccc1939560f4b73c905495ce0d48116826c Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 18 Nov 2011 23:29:19 -0400 Subject: [PATCH 07/78] More document grouping rename updates --- apps/grouping/forms.py | 8 ++++---- apps/grouping/managers.py | 2 +- apps/grouping/models.py | 4 ++-- apps/grouping/views.py | 10 +++++----- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/apps/grouping/forms.py b/apps/grouping/forms.py index 0aa5df627e..506b20ee6d 100644 --- a/apps/grouping/forms.py +++ b/apps/grouping/forms.py @@ -105,17 +105,17 @@ class SmartLinkImageWidget(forms.widgets.Widget): class SmartLinkInstanceForm(forms.Form): def __init__(self, *args, **kwargs): - groups = kwargs.pop('groups', None) + smart_link_instances = kwargs.pop('smart_link_instances', None) links = kwargs.pop('links', None) current_document = kwargs.pop('current_document', None) super(SmartLinkInstanceForm, self).__init__(*args, **kwargs) - for group, data in groups.items(): - self.fields['preview-%s' % group] = forms.CharField( + for smart_link_instance, data in smart_link_instances.items(): + self.fields['preview-%s' % smart_link_instance] = forms.CharField( widget=SmartLinkImageWidget(), label=u'%s (%d)' % (unicode(data['title']), len(data['documents'])), required=False, initial={ - 'group': group, + 'group': smart_link_instance, 'group_data': data['documents'], 'current_document': current_document, 'links': links diff --git a/apps/grouping/managers.py b/apps/grouping/managers.py index 4ed9a174d4..b8f3204ecf 100644 --- a/apps/grouping/managers.py +++ b/apps/grouping/managers.py @@ -7,7 +7,7 @@ from documents.models import Document from grouping.literals import INCLUSION_AND, INCLUSION_OR -class DocumentGroupManager(models.Manager): +class SmartLinkManager(models.Manager): def get_groups_for(self, document, group_obj=None): errors = [] document_groups = {} diff --git a/apps/grouping/models.py b/apps/grouping/models.py index bd8e3d1348..ab032cec19 100644 --- a/apps/grouping/models.py +++ b/apps/grouping/models.py @@ -1,7 +1,7 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ -from grouping.managers import DocumentGroupManager +from grouping.managers import SmartLinkManager from grouping.literals import OPERATOR_CHOICES, INCLUSION_AND, \ INCLUSION_CHOICES @@ -11,7 +11,7 @@ class DocumentGroup(models.Model): dynamic_title = models.CharField(blank=True, max_length=96, verbose_name=_(u'dynamic title'), help_text=_(u'This expression will be evaluated against the current selected document. The document metadata is available as variables `metadata` and document properties under the variable `document`.')) enabled = models.BooleanField(default=True, verbose_name=_(u'enabled')) - objects = DocumentGroupManager() + objects = SmartLinkManager() def __unicode__(self): return self.title diff --git a/apps/grouping/views.py b/apps/grouping/views.py index 348538d38a..e6085b9d4f 100644 --- a/apps/grouping/views.py +++ b/apps/grouping/views.py @@ -59,7 +59,7 @@ def smart_link_instances_for_document(request, document_id): subtemplates_list = [] document = get_object_or_404(Document, pk=document_id) - smart_links, errors = DocumentGroup.objects.get_groups_for(document) + smart_link_instances, errors = DocumentGroup.objects.get_groups_for(document) if (request.user.is_staff or request.user.is_superuser) and errors: for error in errors: messages.warning(request, _(u'Smart link query error: %s' % error)) @@ -67,15 +67,15 @@ def smart_link_instances_for_document(request, document_id): if not SHOW_EMPTY_GROUPS: #If GROUP_SHOW_EMPTY is False, remove empty groups from #dictionary - document_groups = dict([(group, data) for group, data in document_groups.items() if data['documents']]) + smart_link_instances = dict([(group, data) for group, data in smart_link_instances.items() if data['documents']]) - if smart_links: + if smart_link_instances: subtemplates_list = [{ 'name': 'generic_form_subtemplate.html', 'context': { - 'title': _(u'smart links (%s)') % len(smart_links.keys()), + 'title': _(u'smart links (%s)') % len(smart_link_instances.keys()), 'form': SmartLinkInstanceForm( - groups=smart_links, current_document=document, + smart_link_instances=smart_link_instances, current_document=document, links=[smart_link_instance_view_link] ), 'form_action': reverse('smart_link_action'), From ad53632f7ff6759c7f1665b234a38870370f423c Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 18 Nov 2011 23:29:40 -0400 Subject: [PATCH 08/78] Added smart linking setup help --- apps/grouping/__init__.py | 4 ++-- apps/grouping/templates/smart_links_help.html | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 apps/grouping/templates/smart_links_help.html diff --git a/apps/grouping/__init__.py b/apps/grouping/__init__.py index 18b0ce37f4..fc014bce6f 100644 --- a/apps/grouping/__init__.py +++ b/apps/grouping/__init__.py @@ -1,6 +1,6 @@ from django.utils.translation import ugettext_lazy as _ -from navigation.api import register_links +from navigation.api import register_links, register_sidebar_template from permissions.api import register_permission, set_namespace_title from project_setup.api import register_setup from documents.literals import PERMISSION_DOCUMENT_VIEW @@ -8,7 +8,6 @@ from documents.models import Document from grouping.models import DocumentGroup, DocumentGroupItem - PERMISSION_SMART_LINK_VIEW = {'namespace': 'grouping', 'name': 'group_view', 'label': _(u'View existing smart links')} PERMISSION_SMART_LINK_CREATE = {'namespace': 'grouping', 'name': 'group_create', 'label': _(u'Create new smart links')} PERMISSION_SMART_LINK_DELETE = {'namespace': 'grouping', 'name': 'group_delete', 'label': _(u'Delete smart links')} @@ -42,3 +41,4 @@ register_links(['document_group_list', 'document_group_create', 'document_group_ register_links(['smart_link_condition_list', 'smart_link_condition_create', 'smart_link_condition_edit', 'smart_link_condition_delete'], [smart_link_condition_create], menu_name='sidebar') register_setup(document_groups_setup) +register_sidebar_template(['document_group_list'], 'smart_links_help.html') diff --git a/apps/grouping/templates/smart_links_help.html b/apps/grouping/templates/smart_links_help.html new file mode 100644 index 0000000000..8a23361a9b --- /dev/null +++ b/apps/grouping/templates/smart_links_help.html @@ -0,0 +1,5 @@ +{% load i18n %} +
+

{% trans "What are smart links?" %}

+

{% blocktrans %}Smart links are a set of conditional statements that are used to query the database using the current document the user is accessing as the data source, the results of these queries are a list of documents that relate in some manner to the document being displayed and allow users the ability to jump to and from linked documents very easily.{% endblocktrans %}

+
From d358bd991a19eb7acf5f2fdae56676f2d32193a0 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 19 Nov 2011 01:06:34 -0400 Subject: [PATCH 09/78] Close possible security hole by not trusting user input when saving smart link conditions --- apps/grouping/forms.py | 5 +---- apps/grouping/views.py | 6 ++++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/apps/grouping/forms.py b/apps/grouping/forms.py index 506b20ee6d..12b41ce1c3 100644 --- a/apps/grouping/forms.py +++ b/apps/grouping/forms.py @@ -19,10 +19,7 @@ class SmartLinkForm(forms.ModelForm): class SmartLinkConditionForm(forms.ModelForm): class Meta: model = DocumentGroupItem - - def __init__(self, *args, **kwargs): - super(DocumentGroupItemForm, self).__init__(*args, **kwargs) - self.fields['document_group'].widget = forms.HiddenInput() + exclude = ('document_group',) class SmartLinkImageWidget(forms.widgets.Widget): diff --git a/apps/grouping/views.py b/apps/grouping/views.py index e6085b9d4f..9f1ec13262 100644 --- a/apps/grouping/views.py +++ b/apps/grouping/views.py @@ -235,8 +235,10 @@ def smart_link_condition_edit(request, smart_link_condition_pk): if request.method == 'POST': form = SmartLinkConditionForm(request.POST, instance=smart_link_condition) if form.is_valid(): - smart_link_condition = form.save() - messages.success(request, _(u'Smart link condition: "%s" created successfully.') % smart_link_condition) + new_smart_link_condition = form.save(commit=False) + new_smart_link_condition.document_group = smart_link_condition.document_group + new_smart_link_condition.save() + messages.success(request, _(u'Smart link condition: "%s" edited successfully.') % smart_link_condition) return HttpResponseRedirect(next) else: form = SmartLinkConditionForm(instance=smart_link_condition) From fbad8375b361dbc6dfb45311d1446e34317f7cca Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 19 Nov 2011 02:01:55 -0400 Subject: [PATCH 10/78] Finished converting grouping app to the linking app --- apps/grouping/admin.py | 16 ------ apps/grouping/conf/settings.py | 11 ---- apps/{grouping => linking}/__init__.py | 16 +++--- apps/linking/admin.py | 16 ++++++ apps/{grouping => linking}/conf/__init__.py | 0 apps/linking/conf/settings.py | 13 +++++ apps/{grouping => linking}/forms.py | 10 ++-- apps/{grouping => linking}/literals.py | 0 .../locale/en/LC_MESSAGES/django.po | 0 .../locale/es/LC_MESSAGES/django.mo | Bin .../locale/es/LC_MESSAGES/django.po | 0 .../locale/pt/LC_MESSAGES/django.mo | Bin .../locale/pt/LC_MESSAGES/django.po | 0 .../locale/ru/LC_MESSAGES/django.mo | Bin .../locale/ru/LC_MESSAGES/django.po | 0 apps/{grouping => linking}/managers.py | 54 +++++++++--------- apps/{grouping => linking}/models.py | 12 ++-- .../static/images/icons/cog_delete.png | Bin .../static/images/icons/link.png | Bin .../static/images/icons/link_delete.png | Bin .../templates/smart_links_help.html | 0 apps/{grouping => linking}/tests.py | 0 apps/{grouping => linking}/urls.py | 2 +- apps/{grouping => linking}/views.py | 54 +++++++++--------- settings.py | 2 +- urls.py | 2 +- 26 files changed, 106 insertions(+), 102 deletions(-) delete mode 100644 apps/grouping/admin.py delete mode 100644 apps/grouping/conf/settings.py rename apps/{grouping => linking}/__init__.py (80%) create mode 100644 apps/linking/admin.py rename apps/{grouping => linking}/conf/__init__.py (100%) create mode 100644 apps/linking/conf/settings.py rename apps/{grouping => linking}/forms.py (97%) rename apps/{grouping => linking}/literals.py (100%) rename apps/{grouping => linking}/locale/en/LC_MESSAGES/django.po (100%) rename apps/{grouping => linking}/locale/es/LC_MESSAGES/django.mo (100%) rename apps/{grouping => linking}/locale/es/LC_MESSAGES/django.po (100%) rename apps/{grouping => linking}/locale/pt/LC_MESSAGES/django.mo (100%) rename apps/{grouping => linking}/locale/pt/LC_MESSAGES/django.po (100%) rename apps/{grouping => linking}/locale/ru/LC_MESSAGES/django.mo (100%) rename apps/{grouping => linking}/locale/ru/LC_MESSAGES/django.po (100%) rename apps/{grouping => linking}/managers.py (53%) rename apps/{grouping => linking}/models.py (81%) rename apps/{grouping => linking}/static/images/icons/cog_delete.png (100%) rename apps/{grouping => linking}/static/images/icons/link.png (100%) rename apps/{grouping => linking}/static/images/icons/link_delete.png (100%) rename apps/{grouping => linking}/templates/smart_links_help.html (100%) rename apps/{grouping => linking}/tests.py (100%) rename apps/{grouping => linking}/urls.py (96%) rename apps/{grouping => linking}/views.py (85%) diff --git a/apps/grouping/admin.py b/apps/grouping/admin.py deleted file mode 100644 index 0f741d02b8..0000000000 --- a/apps/grouping/admin.py +++ /dev/null @@ -1,16 +0,0 @@ -from django.contrib import admin - -from grouping.models import DocumentGroup, DocumentGroupItem - - -class DocumentGroupItemInline(admin.StackedInline): - model = DocumentGroupItem - extra = 1 - classes = ('collapse-open',) - allow_add = True - - -class DocumentGroupAdmin(admin.ModelAdmin): - inlines = [DocumentGroupItemInline] - -admin.site.register(DocumentGroup, DocumentGroupAdmin) diff --git a/apps/grouping/conf/settings.py b/apps/grouping/conf/settings.py deleted file mode 100644 index 5625201a15..0000000000 --- a/apps/grouping/conf/settings.py +++ /dev/null @@ -1,11 +0,0 @@ -"""Configuration options for the grouping app""" - -from smart_settings.api import register_settings - -register_settings( - namespace=u'grouping', - module=u'grouping.conf.settings', - settings=[ - {'name': u'SHOW_EMPTY_GROUPS', 'global_name': u'GROUPING_SHOW_EMPTY_GROUPS', 'default': True}, - ] -) diff --git a/apps/grouping/__init__.py b/apps/linking/__init__.py similarity index 80% rename from apps/grouping/__init__.py rename to apps/linking/__init__.py index fc014bce6f..02af2176c8 100644 --- a/apps/grouping/__init__.py +++ b/apps/linking/__init__.py @@ -6,14 +6,14 @@ from project_setup.api import register_setup from documents.literals import PERMISSION_DOCUMENT_VIEW from documents.models import Document -from grouping.models import DocumentGroup, DocumentGroupItem +from linking.models import SmartLink, SmartLinkCondition -PERMISSION_SMART_LINK_VIEW = {'namespace': 'grouping', 'name': 'group_view', 'label': _(u'View existing smart links')} -PERMISSION_SMART_LINK_CREATE = {'namespace': 'grouping', 'name': 'group_create', 'label': _(u'Create new smart links')} -PERMISSION_SMART_LINK_DELETE = {'namespace': 'grouping', 'name': 'group_delete', 'label': _(u'Delete smart links')} -PERMISSION_SMART_LINK_EDIT = {'namespace': 'grouping', 'name': 'group_edit', 'label': _(u'Edit smart links')} +PERMISSION_SMART_LINK_VIEW = {'namespace': 'linking', 'name': 'group_view', 'label': _(u'View existing smart links')} +PERMISSION_SMART_LINK_CREATE = {'namespace': 'linking', 'name': 'group_create', 'label': _(u'Create new smart links')} +PERMISSION_SMART_LINK_DELETE = {'namespace': 'linking', 'name': 'group_delete', 'label': _(u'Delete smart links')} +PERMISSION_SMART_LINK_EDIT = {'namespace': 'linking', 'name': 'group_edit', 'label': _(u'Edit smart links')} -set_namespace_title('grouping', _(u'Smart links')) +set_namespace_title('linking', _(u'Smart links')) register_permission(PERMISSION_SMART_LINK_VIEW) register_permission(PERMISSION_SMART_LINK_CREATE) register_permission(PERMISSION_SMART_LINK_DELETE) @@ -35,8 +35,8 @@ smart_link_condition_delete = {'text': _(u'delete'), 'view': 'smart_link_conditi register_links(Document, [smart_link_instances_for_document], menu_name='form_header') -register_links(DocumentGroup, [document_group_edit, document_group_delete, smart_link_condition_list]) -register_links(DocumentGroupItem, [smart_link_condition_edit, smart_link_condition_delete]) +register_links(SmartLink, [document_group_edit, document_group_delete, smart_link_condition_list]) +register_links(SmartLinkCondition, [smart_link_condition_edit, smart_link_condition_delete]) register_links(['document_group_list', 'document_group_create', 'document_group_edit', 'document_group_delete', 'smart_link_condition_list', 'smart_link_condition_create', 'smart_link_condition_edit', 'smart_link_condition_delete'], [document_group_list, document_group_create], menu_name='sidebar') register_links(['smart_link_condition_list', 'smart_link_condition_create', 'smart_link_condition_edit', 'smart_link_condition_delete'], [smart_link_condition_create], menu_name='sidebar') diff --git a/apps/linking/admin.py b/apps/linking/admin.py new file mode 100644 index 0000000000..045244bc5a --- /dev/null +++ b/apps/linking/admin.py @@ -0,0 +1,16 @@ +from django.contrib import admin + +from linking.models import SmartLink, SmartLinkCondition + + +class SmartLinkConditionInline(admin.StackedInline): + model = SmartLinkCondition + extra = 1 + classes = ('collapse-open',) + allow_add = True + + +class SmartLinkAdmin(admin.ModelAdmin): + inlines = [SmartLinkConditionInline] + +admin.site.register(SmartLink, SmartLinkAdmin) diff --git a/apps/grouping/conf/__init__.py b/apps/linking/conf/__init__.py similarity index 100% rename from apps/grouping/conf/__init__.py rename to apps/linking/conf/__init__.py diff --git a/apps/linking/conf/settings.py b/apps/linking/conf/settings.py new file mode 100644 index 0000000000..cf0d855350 --- /dev/null +++ b/apps/linking/conf/settings.py @@ -0,0 +1,13 @@ +'''Configuration options for the linking app''' + +from django.utils.translation import ugettext_lazy as _ + +from smart_settings.api import register_settings + +register_settings( + namespace=u'linking', + module=u'linking.conf.settings', + settings=[ + {'name': u'SHOW_EMPTY_SMART_LINKS', 'global_name': u'LINKING_SHOW_EMPTY_SMART_LINKS', 'default': True, 'description': _(u'Show smart link that don\'t return any documents.')}, + ] +) diff --git a/apps/grouping/forms.py b/apps/linking/forms.py similarity index 97% rename from apps/grouping/forms.py rename to apps/linking/forms.py index 12b41ce1c3..509d803a4a 100644 --- a/apps/grouping/forms.py +++ b/apps/linking/forms.py @@ -8,18 +8,18 @@ from django.conf import settings from tags.widgets import get_tags_inline_widget -from grouping.models import DocumentGroup, DocumentGroupItem +from linking.models import SmartLink, SmartLinkCondition class SmartLinkForm(forms.ModelForm): class Meta: - model = DocumentGroup + model = SmartLink class SmartLinkConditionForm(forms.ModelForm): class Meta: - model = DocumentGroupItem - exclude = ('document_group',) + model = SmartLinkCondition + exclude = ('smart_link',) class SmartLinkImageWidget(forms.widgets.Widget): @@ -105,7 +105,9 @@ class SmartLinkInstanceForm(forms.Form): smart_link_instances = kwargs.pop('smart_link_instances', None) links = kwargs.pop('links', None) current_document = kwargs.pop('current_document', None) + super(SmartLinkInstanceForm, self).__init__(*args, **kwargs) + for smart_link_instance, data in smart_link_instances.items(): self.fields['preview-%s' % smart_link_instance] = forms.CharField( widget=SmartLinkImageWidget(), diff --git a/apps/grouping/literals.py b/apps/linking/literals.py similarity index 100% rename from apps/grouping/literals.py rename to apps/linking/literals.py diff --git a/apps/grouping/locale/en/LC_MESSAGES/django.po b/apps/linking/locale/en/LC_MESSAGES/django.po similarity index 100% rename from apps/grouping/locale/en/LC_MESSAGES/django.po rename to apps/linking/locale/en/LC_MESSAGES/django.po diff --git a/apps/grouping/locale/es/LC_MESSAGES/django.mo b/apps/linking/locale/es/LC_MESSAGES/django.mo similarity index 100% rename from apps/grouping/locale/es/LC_MESSAGES/django.mo rename to apps/linking/locale/es/LC_MESSAGES/django.mo diff --git a/apps/grouping/locale/es/LC_MESSAGES/django.po b/apps/linking/locale/es/LC_MESSAGES/django.po similarity index 100% rename from apps/grouping/locale/es/LC_MESSAGES/django.po rename to apps/linking/locale/es/LC_MESSAGES/django.po diff --git a/apps/grouping/locale/pt/LC_MESSAGES/django.mo b/apps/linking/locale/pt/LC_MESSAGES/django.mo similarity index 100% rename from apps/grouping/locale/pt/LC_MESSAGES/django.mo rename to apps/linking/locale/pt/LC_MESSAGES/django.mo diff --git a/apps/grouping/locale/pt/LC_MESSAGES/django.po b/apps/linking/locale/pt/LC_MESSAGES/django.po similarity index 100% rename from apps/grouping/locale/pt/LC_MESSAGES/django.po rename to apps/linking/locale/pt/LC_MESSAGES/django.po diff --git a/apps/grouping/locale/ru/LC_MESSAGES/django.mo b/apps/linking/locale/ru/LC_MESSAGES/django.mo similarity index 100% rename from apps/grouping/locale/ru/LC_MESSAGES/django.mo rename to apps/linking/locale/ru/LC_MESSAGES/django.mo diff --git a/apps/grouping/locale/ru/LC_MESSAGES/django.po b/apps/linking/locale/ru/LC_MESSAGES/django.po similarity index 100% rename from apps/grouping/locale/ru/LC_MESSAGES/django.po rename to apps/linking/locale/ru/LC_MESSAGES/django.po diff --git a/apps/grouping/managers.py b/apps/linking/managers.py similarity index 53% rename from apps/grouping/managers.py rename to apps/linking/managers.py index b8f3204ecf..03f9203b24 100644 --- a/apps/grouping/managers.py +++ b/apps/linking/managers.py @@ -4,13 +4,13 @@ from django.db.models import Q from metadata.classes import MetadataObject from documents.models import Document -from grouping.literals import INCLUSION_AND, INCLUSION_OR +from linking.literals import INCLUSION_AND, INCLUSION_OR class SmartLinkManager(models.Manager): - def get_groups_for(self, document, group_obj=None): + def get_smart_link_instances_for(self, document, smart_link_obj=None): errors = [] - document_groups = {} + result = {} metadata_dict = {} for document_metadata in document.documentmetadata_set.all(): metadata_dict[document_metadata.metadata_type.name] = document_metadata.value @@ -18,38 +18,38 @@ class SmartLinkManager(models.Manager): eval_dict['document'] = document eval_dict['metadata'] = MetadataObject(metadata_dict) - if group_obj: - groups_qs = self.model.objects.filter(Q(enabled=True) & Q(pk=group_obj.pk)) + if smart_link_obj: + smart_link_qs = self.model.objects.filter(Q(enabled=True) & Q(pk=smart_link_obj.pk)) else: - groups_qs = self.model.objects.filter(enabled=True) + smart_link_qs = self.model.objects.filter(enabled=True) - for group in groups_qs: + for smart_link in smart_link_qs: total_query = Q() - for item in group.documentgroupitem_set.filter(enabled=True): - cls, attribute = item.foreign_document_data.lower().split(u'.') + for condition in smart_link.smartlinkcondition_set.filter(enabled=True): + cls, attribute = condition.foreign_document_data.lower().split(u'.') try: if cls == u'metadata': - value_query = Q(**{'documentmetadata__value__%s' % item.operator: eval(item.expression, eval_dict)}) - if item.negated: + value_query = Q(**{'documentmetadata__value__%s' % condition.operator: eval(condition.expression, eval_dict)}) + if condition.negated: query = (Q(documentmetadata__metadata_type__name=attribute) & ~value_query) else: query = (Q(documentmetadata__metadata_type__name=attribute) & value_query) - if item.inclusion == INCLUSION_AND: + if condition.inclusion == INCLUSION_AND: total_query &= query - elif item.inclusion == INCLUSION_OR: + elif condition.inclusion == INCLUSION_OR: total_query |= query elif cls == u'document': value_query = Q(**{ - '%s__%s' % (attribute, item.operator): eval(item.expression, eval_dict) + '%s__%s' % (attribute, condition.operator): eval(condition.expression, eval_dict) }) - if item.negated: + if condition.negated: query = ~value_query else: query = value_query - if item.inclusion == INCLUSION_AND: + if condition.inclusion == INCLUSION_AND: total_query &= query - elif item.inclusion == INCLUSION_OR: + elif condition.inclusion == INCLUSION_OR: total_query |= query except Exception, e: @@ -59,24 +59,24 @@ class SmartLinkManager(models.Manager): if total_query: try: document_qs = Document.objects.filter(total_query) - document_groups[group] = {'documents': document_qs.order_by('file_filename') or []} + result[smart_link] = {'documents': document_qs.order_by('file_filename') or []} except Exception, e: - document_groups[group] = {'documents': []} + result[smart_link] = {'documents': []} errors.append(e) else: - document_groups[group] = {'documents': []} + result[smart_link] = {'documents': []} - if group.dynamic_title: + if smart_link.dynamic_title: try: - document_groups[group]['title'] = eval(group.dynamic_title, eval_dict) + result[smart_link]['title'] = eval(smart_link.dynamic_title, eval_dict) except Exception, e: - document_groups[group]['title'] = 'Error; %s' % e + result[smart_link]['title'] = 'Error; %s' % e else: - document_groups[group]['title'] = group.title + result[smart_link]['title'] = smart_link.title - if group_obj: + if smart_link_obj: # Return a single group if documents even if there were # many matches - return document_groups[group_obj], errors + return result[smart_link_obj], errors - return document_groups, errors + return result, errors diff --git a/apps/grouping/models.py b/apps/linking/models.py similarity index 81% rename from apps/grouping/models.py rename to apps/linking/models.py index ab032cec19..ccc04f5537 100644 --- a/apps/grouping/models.py +++ b/apps/linking/models.py @@ -1,12 +1,12 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ -from grouping.managers import SmartLinkManager -from grouping.literals import OPERATOR_CHOICES, INCLUSION_AND, \ +from linking.managers import SmartLinkManager +from linking.literals import OPERATOR_CHOICES, INCLUSION_AND, \ INCLUSION_CHOICES -class DocumentGroup(models.Model): +class SmartLink(models.Model): title = models.CharField(max_length=96, verbose_name=_(u'title')) dynamic_title = models.CharField(blank=True, max_length=96, verbose_name=_(u'dynamic title'), help_text=_(u'This expression will be evaluated against the current selected document. The document metadata is available as variables `metadata` and document properties under the variable `document`.')) enabled = models.BooleanField(default=True, verbose_name=_(u'enabled')) @@ -21,13 +21,11 @@ class DocumentGroup(models.Model): verbose_name_plural = _(u'smart links') -class DocumentGroupItem(models.Model): - document_group = models.ForeignKey(DocumentGroup, verbose_name=_(u'smart link')) +class SmartLinkCondition(models.Model): + smart_link = models.ForeignKey(SmartLink, verbose_name=_(u'smart link')) inclusion = models.CharField(default=INCLUSION_AND, max_length=16, choices=INCLUSION_CHOICES, help_text=_(u'The inclusion is ignored for the first item.')) foreign_document_data = models.CharField(max_length=32, verbose_name=_(u'foreign document data'), help_text=_(u'This represents the metadata of all other documents. Available objects: `document.` and `metadata.`.')) operator = models.CharField(max_length=16, choices=OPERATOR_CHOICES) - - #local_document_data = models.ForeignKey(MetadataType, related_name='metadata_type_local', verbose_name=_(u'local metadata'), help_text=_(u'This represents the metadata of the current document.')) expression = models.TextField(verbose_name=_(u'expression'), help_text=_(u'This expression will be evaluated against the current selected document. The document metadata is available as variables `metadata` and document properties under the variable `document`.')) negated = models.BooleanField(default=False, verbose_name=_(u'negated'), help_text=_(u'Inverts the logic of the operator.')) enabled = models.BooleanField(default=True, verbose_name=_(u'enabled')) diff --git a/apps/grouping/static/images/icons/cog_delete.png b/apps/linking/static/images/icons/cog_delete.png similarity index 100% rename from apps/grouping/static/images/icons/cog_delete.png rename to apps/linking/static/images/icons/cog_delete.png diff --git a/apps/grouping/static/images/icons/link.png b/apps/linking/static/images/icons/link.png similarity index 100% rename from apps/grouping/static/images/icons/link.png rename to apps/linking/static/images/icons/link.png diff --git a/apps/grouping/static/images/icons/link_delete.png b/apps/linking/static/images/icons/link_delete.png similarity index 100% rename from apps/grouping/static/images/icons/link_delete.png rename to apps/linking/static/images/icons/link_delete.png diff --git a/apps/grouping/templates/smart_links_help.html b/apps/linking/templates/smart_links_help.html similarity index 100% rename from apps/grouping/templates/smart_links_help.html rename to apps/linking/templates/smart_links_help.html diff --git a/apps/grouping/tests.py b/apps/linking/tests.py similarity index 100% rename from apps/grouping/tests.py rename to apps/linking/tests.py diff --git a/apps/grouping/urls.py b/apps/linking/urls.py similarity index 96% rename from apps/grouping/urls.py rename to apps/linking/urls.py index 74a852a69a..0a00851384 100644 --- a/apps/grouping/urls.py +++ b/apps/linking/urls.py @@ -1,6 +1,6 @@ from django.conf.urls.defaults import patterns, url -urlpatterns = patterns('grouping.views', +urlpatterns = patterns('linking.views', url(r'^action/$', 'smart_link_action', (), 'smart_link_action'), url(r'^document/(?P\d+)/smart_link/(?P\d+)/$', 'smart_link_instance_view', (), 'smart_link_instance_view'), url(r'^smart/for_document/(?P\d+)/$', 'smart_link_instances_for_document', (), 'smart_link_instances_for_document'), diff --git a/apps/grouping/views.py b/apps/linking/views.py similarity index 85% rename from apps/grouping/views.py rename to apps/linking/views.py index 9f1ec13262..1e948ee9b0 100644 --- a/apps/grouping/views.py +++ b/apps/linking/views.py @@ -13,12 +13,12 @@ from documents.views import document_list from permissions.api import check_permissions -from grouping.models import DocumentGroup, DocumentGroupItem -from grouping.conf.settings import SHOW_EMPTY_GROUPS -from grouping.forms import (SmartLinkInstanceForm, SmartLinkForm, +from linking.models import SmartLink, SmartLinkCondition +from linking.conf.settings import SHOW_EMPTY_SMART_LINKS +from linking.forms import (SmartLinkInstanceForm, SmartLinkForm, SmartLinkConditionForm) -from grouping import smart_link_instance_view_link -from grouping import (PERMISSION_SMART_LINK_VIEW, +from linking import smart_link_instance_view_link +from linking import (PERMISSION_SMART_LINK_VIEW, PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_DELETE, PERMISSION_SMART_LINK_EDIT) @@ -39,8 +39,8 @@ def smart_link_instance_view(request, document_id, smart_link_pk): check_permissions(request.user, [PERMISSION_SMART_LINK_VIEW]) document = get_object_or_404(Document, pk=document_id) - smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) - object_list, errors = DocumentGroup.objects.get_groups_for(document, smart_link) + smart_link = get_object_or_404(SmartLink, pk=smart_link_pk) + object_list, errors = SmartLink.objects.get_smart_link_instances_for(document, smart_link) return document_list( request, @@ -59,13 +59,13 @@ def smart_link_instances_for_document(request, document_id): subtemplates_list = [] document = get_object_or_404(Document, pk=document_id) - smart_link_instances, errors = DocumentGroup.objects.get_groups_for(document) + smart_link_instances, errors = SmartLink.objects.get_smart_link_instances_for(document) if (request.user.is_staff or request.user.is_superuser) and errors: for error in errors: messages.warning(request, _(u'Smart link query error: %s' % error)) - if not SHOW_EMPTY_GROUPS: - #If GROUP_SHOW_EMPTY is False, remove empty groups from + if not SHOW_EMPTY_SMART_LINKS: + #If SHOW_EMPTY_SMART_LINKS is False, remove empty groups from #dictionary smart_link_instances = dict([(group, data) for group, data in smart_link_instances.items() if data['documents']]) @@ -103,7 +103,7 @@ def document_group_list(request): return render_to_response('generic_list.html', { 'title': _(u'smart links'), - 'object_list': DocumentGroup.objects.all(), + 'object_list': SmartLink.objects.all(), 'extra_columns': [ {'name': _(u'dynamic title'), 'attribute': 'dynamic_title'}, {'name': _(u'enabled'), 'attribute': encapsulate(lambda x: two_state_template(x.enabled))}, @@ -135,7 +135,7 @@ def document_group_create(request): def document_group_edit(request, smart_link_pk): check_permissions(request.user, [PERMISSION_SMART_LINK_EDIT]) - smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) + smart_link = get_object_or_404(SmartLink, pk=smart_link_pk) if request.method == 'POST': form = SmartLinkForm(request.POST, instance=smart_link) @@ -157,7 +157,7 @@ def document_group_edit(request, smart_link_pk): def document_group_delete(request, smart_link_pk): check_permissions(request.user, [PERMISSION_SMART_LINK_DELETE]) - smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) + smart_link = get_object_or_404(SmartLink, pk=smart_link_pk) next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/'))) previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) @@ -187,11 +187,11 @@ def document_group_delete(request, smart_link_pk): def smart_link_condition_list(request, smart_link_pk): check_permissions(request.user, [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]) - smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) + smart_link = get_object_or_404(SmartLink, pk=smart_link_pk) return render_to_response('generic_list.html', { 'title': _(u'conditions for smart link: %s') % smart_link, - 'object_list': smart_link.documentgroupitem_set.all(), + 'object_list': smart_link.smartlinkcondition_set.all(), 'extra_columns': [ {'name': _(u'enabled'), 'attribute': encapsulate(lambda x: two_state_template(x.enabled))}, ], @@ -205,16 +205,18 @@ def smart_link_condition_list(request, smart_link_pk): def smart_link_condition_create(request, smart_link_pk): check_permissions(request.user, [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]) - smart_link = get_object_or_404(DocumentGroup, pk=smart_link_pk) + smart_link = get_object_or_404(SmartLink, pk=smart_link_pk) if request.method == 'POST': - form = SmartLinkConditionForm(request.POST, initial={'document_group': smart_link}) + form = SmartLinkConditionForm(request.POST) if form.is_valid(): - smart_link_condition = form.save() - messages.success(request, _(u'Smart link condition: "%s" created successfully.') % smart_link_condition) + new_smart_link_condition = form.save(commit=False) + new_smart_link_condition.smart_link = smart_link + new_smart_link_condition.save() + messages.success(request, _(u'Smart link condition: "%s" created successfully.') % new_smart_link_condition) return HttpResponseRedirect(reverse('smart_link_condition_list', args=[smart_link.pk])) else: - form = SmartLinkConditionForm(initial={'document_group': smart_link}) + form = SmartLinkConditionForm(initial={'smart_link': smart_link}) return render_to_response('generic_form.html', { 'form': form, @@ -227,7 +229,7 @@ def smart_link_condition_create(request, smart_link_pk): def smart_link_condition_edit(request, smart_link_condition_pk): check_permissions(request.user, [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]) - smart_link_condition = get_object_or_404(DocumentGroupItem, pk=smart_link_condition_pk) + smart_link_condition = get_object_or_404(SmartLinkCondition, pk=smart_link_condition_pk) next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/'))) previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) @@ -236,9 +238,9 @@ def smart_link_condition_edit(request, smart_link_condition_pk): form = SmartLinkConditionForm(request.POST, instance=smart_link_condition) if form.is_valid(): new_smart_link_condition = form.save(commit=False) - new_smart_link_condition.document_group = smart_link_condition.document_group + new_smart_link_condition.smart_link = smart_link_condition.smart_link new_smart_link_condition.save() - messages.success(request, _(u'Smart link condition: "%s" edited successfully.') % smart_link_condition) + messages.success(request, _(u'Smart link condition: "%s" edited successfully.') % new_smart_link_condition) return HttpResponseRedirect(next) else: form = SmartLinkConditionForm(instance=smart_link_condition) @@ -249,7 +251,7 @@ def smart_link_condition_edit(request, smart_link_condition_pk): 'next': next, 'previous': previous, 'condition': smart_link_condition, - 'smart_link': smart_link_condition.document_group, + 'smart_link': smart_link_condition.smart_link, 'navigation_object_list': [ {'object': 'smart_link', 'name': _(u'smart link')}, {'object': 'condition', 'name': _(u'condition')} @@ -261,7 +263,7 @@ def smart_link_condition_edit(request, smart_link_condition_pk): def smart_link_condition_delete(request, smart_link_condition_pk): check_permissions(request.user, [PERMISSION_SMART_LINK_CREATE, PERMISSION_SMART_LINK_EDIT]) - smart_link_condition = get_object_or_404(DocumentGroupItem, pk=smart_link_condition_pk) + smart_link_condition = get_object_or_404(SmartLinkCondition, pk=smart_link_condition_pk) next = request.POST.get('next', request.GET.get('next', request.META.get('HTTP_REFERER', '/'))) previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) @@ -280,7 +282,7 @@ def smart_link_condition_delete(request, smart_link_condition_pk): return render_to_response('generic_confirm.html', { 'delete_view': True, 'condition': smart_link_condition, - 'smart_link': smart_link_condition.document_group, + 'smart_link': smart_link_condition.smart_link, 'navigation_object_list': [ {'object': 'smart_link', 'name': _(u'smart link')}, {'object': 'condition', 'name': _(u'condition')} diff --git a/settings.py b/settings.py index 4322e12875..ea37f7cbf0 100644 --- a/settings.py +++ b/settings.py @@ -153,7 +153,7 @@ INSTALLED_APPS = ( 'document_comments', 'user_management', 'documents', - 'grouping', + 'linking', 'mptt', 'document_indexing', 'ocr', diff --git a/urls.py b/urls.py index 6127fe570e..4736d4d377 100644 --- a/urls.py +++ b/urls.py @@ -21,7 +21,7 @@ urlpatterns = patterns('', (r'^user_management/', include('user_management.urls')), (r'^settings/', include('smart_settings.urls')), (r'^metadata/', include('metadata.urls')), - (r'^grouping/', include('grouping.urls')), + (r'^linking/', include('linking.urls')), (r'^document_indexing/', include('document_indexing.urls')), (r'^history/', include('history.urls')), (r'^converter/', include('converter.urls')), From 91203a9593c9b18b9c3291363aa1337eb55f6681 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sat, 19 Nov 2011 03:59:09 -0400 Subject: [PATCH 11/78] Merged all document widget generation code into one place --- apps/documents/forms.py | 83 +++++++++++---------------------------- apps/documents/widgets.py | 61 +++++++++++++++++++++------- apps/linking/forms.py | 71 ++++++++------------------------- 3 files changed, 85 insertions(+), 130 deletions(-) diff --git a/apps/documents/forms.py b/apps/documents/forms.py index 9fdc19c5c2..a6f71c0a12 100644 --- a/apps/documents/forms.py +++ b/apps/documents/forms.py @@ -9,10 +9,11 @@ from common.forms import DetailForm from common.literals import PAGE_SIZE_CHOICES, PAGE_ORIENTATION_CHOICES from common.conf.settings import DEFAULT_PAPER_SIZE from common.conf.settings import DEFAULT_PAGE_ORIENTATION -from common.widgets import TextAreaDiv +from common.widgets import TextAreaDiv from documents.models import Document, DocumentType, \ DocumentPage, DocumentPageTransformation, DocumentTypeFilename +from documents.widgets import document_html_widget # Document page forms @@ -32,22 +33,10 @@ class DocumentPageImageWidget(forms.widgets.Widget): rotation = final_attrs.get('rotation', 0) if value: output = [] - output.append(''' -
-
- %(string)s - -
-
''' % { - 'img': reverse('document_display', args=[value.document.id]), - 'page': value.page_number, - 'zoom': zoom, - 'rotation': rotation, - 'static_url': settings.STATIC_URL, - 'string': ugettext(u'page image') - }) + output.append('
') + + output.append(document_html_widget(value.document, size='document_display', page=value.page_number, zoom=zoom, rotation=rotation)) + output.append('
') return mark_safe(u''.join(output)) else: return u'' @@ -113,52 +102,24 @@ class DocumentPagesCarouselWidget(forms.widgets.Widget): output.append(u'
') for page in value.documentpage_set.all(): - try: - page.document.get_valid_image() - template = u'''
-
%(page_string)s %(page)s
- - -
''' - except: - template = u'''
-
%(page_string)s %(page)s
-
- %(string)s - -
- -
''' - - - output.append(template % { - 'url': reverse('document_page_view', args=[page.pk]), - 'img': reverse('document_preview_multipage', args=[value.pk]), - 'page': page.page_number, - 'view_url': reverse('document_display', args=[page.document.pk]), - 'page_string': ugettext(u'Page'), - 'details_string': ugettext(u'Details'), - 'static_url': settings.STATIC_URL, - 'string': _(u'document page') - }) + output.append(u'
') + output.append( + document_html_widget( + page.document, + size='document_preview_multipage', + click_view='document_display', + page=page.page_number, + gallery_name='document_pages', + fancybox_class='fancybox-noscaling', + ) + ) + output.append(u'
') + output.append(u'%s' % (reverse('document_page_view', args=[page.pk]), ugettext(u'Details'))) + output.append(u'
') + output.append(u'
') output.append(u'
') - output.append( - u'
%s' % - ugettext(u'Click on the image for full size preview')) + output.append(u'
%s' % ugettext(u'Click on the image for full size preview')) return mark_safe(u''.join(output)) diff --git a/apps/documents/widgets.py b/apps/documents/widgets.py index b875aade25..bdca584c2e 100644 --- a/apps/documents/widgets.py +++ b/apps/documents/widgets.py @@ -2,25 +2,56 @@ from django.utils.safestring import mark_safe from django.conf import settings from django.utils.translation import ugettext_lazy as _ from django.core.urlresolvers import reverse +from django.utils.http import urlencode + +from converter.exceptions import UnknownFileFormat, UnkownConvertError def document_thumbnail(document): - try: - document.get_valid_image() - template = u'%(string)s' - except: - template = u'%(string)s' - - try: - return mark_safe(template % { - 'url': reverse('document_preview', args=[document.pk]), - 'thumbnail': reverse('document_thumbnail', args=[document.pk]), - 'static_url': settings.STATIC_URL, - 'string': _(u'thumbnail') - }) - except: - return u'' + return document_html_widget(document, click_view='document_preview') def document_link(document): return mark_safe(u'%s' % (reverse('document_view_simple', args=[document.pk]), document)) + + +def document_html_widget(document, size='document_thumbnail', click_view=None, page=None, zoom=None, rotation=None, gallery_name=None, fancybox_class='fancybox'): + result = [] + + alt_text = _(u'document page image') + query_dict = {} + + if page: + query_dict['page'] = page + + if zoom: + query_dict['zoom'] = zoom + + if rotation: + query_dict['rotation'] = rotation + + if gallery_name: + gallery_template = u'rel="%s"' % gallery_name + else: + gallery_template = u'' + + query_string = urlencode(query_dict) + preview_view = u'%s?%s' % (reverse(size, args=[document.pk]), query_string) + + try: + document.get_valid_image() + result.append('') + except UnknownFileFormat, UnkownConvertError: + result.append('
') + result.append('%s' % (preview_view, settings.STATIC_URL, alt_text)) + result.append('' % (preview_view, alt_text)) + result.append('
') + + return mark_safe(u''.join(result)) diff --git a/apps/linking/forms.py b/apps/linking/forms.py index 509d803a4a..864b532dcc 100644 --- a/apps/linking/forms.py +++ b/apps/linking/forms.py @@ -6,6 +6,7 @@ from django.utils.safestring import mark_safe from django.template.defaultfilters import capfirst from django.conf import settings +from documents.widgets import document_html_widget from tags.widgets import get_tags_inline_widget from linking.models import SmartLink, SmartLinkCondition @@ -36,62 +37,24 @@ class SmartLinkImageWidget(forms.widgets.Widget): ''' % { 'famfam': link.get('famfam', u'link'), 'text': capfirst(link['text']), - 'action': reverse(link.get('view'), args=[value['current_document'].pk, value['group'].pk]) + 'action': reverse(link.get('view'), args=[value['current_document'].pk, value['smart_link_instance'].pk]) }) output.append(u'') output.append(u'
') - for document in value['group_data']: - tags_template = get_tags_inline_widget(document) - - try: - document.get_valid_image() - template = u'''
-
%(document_name)s
-
%(page_string)s: %(document_pages)d
- %(tags_template)s - - -
''' - except: - template = u'''
-
%(document_name)s
-
%(page_string)s: %(document_pages)d
- %(tags_template)s -
- %(string)s - -
- -
''' - - output.append(template % { - 'url': reverse('document_view_simple', args=[document.pk]), - 'img': reverse('document_preview_multipage', args=[document.pk]), - 'current': u'border: 5px solid black; padding: 3px;' if value['current_document'] == document else u'', - 'view_url': reverse('document_display', args=[document.pk]), - 'document_pages': document.documentpage_set.count(), - 'page_string': ugettext(u'Pages'), - 'details_string': ugettext(u'Select'), - 'group_id': value['group'].pk, - 'document_name': document, - 'static_url': settings.STATIC_URL, - 'tags_template': tags_template if tags_template else u'', - 'string': _(u'smart links'), - }) + for document in value['documents']: + output.append(u'
' % (u'border: 5px solid black; padding: 3px;' if value['current_document'] == document else u'')) + output.append(u'
%s
' % document) + output.append(u'
%s: %d
' % (ugettext(u'Pages'), document.documentpage_set.count())) + output.append(get_tags_inline_widget(document)) + output.append(u'
' % document) + output.append(document_html_widget(document, click_view='document_display', size='document_preview_multipage', fancybox_class='fancybox-noscaling', gallery_name=u'smart_link_%d_documents_gallery' % value['smart_link_instance'].pk)) + output.append(u'
') + output.append(u'
') + output.append(u'%s' % (reverse('document_view_simple', args=[document.pk]), ugettext(u'Select'))) + output.append(u'
') + output.append(u'
') + output.append(u'
') output.append( u'
%s' % @@ -114,8 +77,8 @@ class SmartLinkInstanceForm(forms.Form): label=u'%s (%d)' % (unicode(data['title']), len(data['documents'])), required=False, initial={ - 'group': smart_link_instance, - 'group_data': data['documents'], + 'smart_link_instance': smart_link_instance, + 'documents': data['documents'], 'current_document': current_document, 'links': links } From 67b3e1903180842cab774216d62c5634549c6349 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sun, 20 Nov 2011 02:48:34 -0400 Subject: [PATCH 12/78] Initial commit of the new office converter class --- apps/converter/api.py | 52 ++++---------- apps/converter/exceptions.py | 4 ++ apps/converter/office_converter.py | 106 +++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 38 deletions(-) create mode 100644 apps/converter/office_converter.py diff --git a/apps/converter/api.py b/apps/converter/api.py index bdcfe40f77..82aefe63bb 100644 --- a/apps/converter/api.py +++ b/apps/converter/api.py @@ -4,8 +4,6 @@ import hashlib from common.conf.settings import TEMPORARY_DIRECTORY -from converter.conf.settings import UNOCONV_PATH -from converter.exceptions import OfficeConversionError from converter.literals import DEFAULT_PAGE_NUMBER, \ DEFAULT_ZOOM_LEVEL, DEFAULT_ROTATION, DEFAULT_FILE_FORMAT @@ -16,28 +14,12 @@ from converter.literals import TRANSFORMATION_RESIZE, \ from converter.literals import DIMENSION_SEPARATOR from converter.literals import FILE_FORMATS from converter.utils import cleanup +from converter.office_converter import OfficeConverter + HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest() - -CONVERTER_OFFICE_FILE_EXTENSIONS = [ - u'ods', u'docx', u'doc' -] - - -def execute_unoconv(input_filepath, arguments=''): - """ - Executes the program unoconv using subprocess's Popen - """ - command = [] - command.append(UNOCONV_PATH) - command.extend(unicode(arguments).split()) - command.append(input_filepath) - proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE) - return_code = proc.wait() - if return_code != 0: - raise OfficeConversionError(proc.stderr.readline()) - + def cache_cleanup(input_filepath, *args, **kwargs): try: os.remove(create_image_cache_filename(input_filepath, *args, **kwargs)) @@ -53,13 +35,6 @@ def create_image_cache_filename(input_filepath, *args, **kwargs): return None -def convert_office_document(input_filepath): - if os.path.exists(UNOCONV_PATH): - execute_unoconv(input_filepath, arguments='-f pdf') - return input_filepath + u'.pdf' - return None - - def convert(input_filepath, output_filepath=None, cleanup_files=False, *args, **kwargs): size = kwargs.get('size') file_format = kwargs.get('file_format', DEFAULT_FILE_FORMAT) @@ -70,20 +45,23 @@ def convert(input_filepath, output_filepath=None, cleanup_files=False, *args, ** if transformations is None: transformations = [] - unoconv_output = None - if output_filepath is None: output_filepath = create_image_cache_filename(input_filepath, *args, **kwargs) + print 'cache image', output_filepath if os.path.exists(output_filepath): return output_filepath + + print 'cleanup_files', cleanup_files - path, extension = os.path.splitext(input_filepath) - if extension[1:].lower() in CONVERTER_OFFICE_FILE_EXTENSIONS: - result = convert_office_document(input_filepath) - if result: - unoconv_output = result - input_filepath = result + office_converter = OfficeConverter(input_filepath) + if office_converter: + try: + #cleanup_files =False. + input_filepath = office_converter.output_filepath + except OfficeConverter: + print 'office converter exception' + raise UnknownFileFormat('office converter exception') if size: transformations.append( @@ -114,8 +92,6 @@ def convert(input_filepath, output_filepath=None, cleanup_files=False, *args, ** finally: if cleanup_files: cleanup(input_filepath) - if unoconv_output: - cleanup(unoconv_output) return output_filepath diff --git a/apps/converter/exceptions.py b/apps/converter/exceptions.py index e90fd4bb34..1423d38002 100644 --- a/apps/converter/exceptions.py +++ b/apps/converter/exceptions.py @@ -29,3 +29,7 @@ class UnkownConvertError(ConvertError): class OfficeConversionError(ConvertError): pass + + +class OfficeBackendError(OfficeConversionError): + pass diff --git a/apps/converter/office_converter.py b/apps/converter/office_converter.py new file mode 100644 index 0000000000..c592d4b765 --- /dev/null +++ b/apps/converter/office_converter.py @@ -0,0 +1,106 @@ +import os +import subprocess +import hashlib + +from mimetype.api import get_mimetype +from common.conf.settings import TEMPORARY_DIRECTORY + +from converter.conf.settings import UNOCONV_PATH +from converter.exceptions import (OfficeConversionError, + OfficeBackendError, UnknownFileFormat) + +HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest() + +CACHED_FILE_SUFFIX = u'_office_converter' + +CONVERTER_OFFICE_FILE_MIMETYPES = [ + 'application/msword', + 'application/mswrite', + 'application/mspowerpoint', + 'application/msexcel', + 'application/vnd.ms-excel', + 'application/vnd.ms-powerpoint', + 'text/plain', + 'application/vnd.oasis.opendocument.presentation', +] +# 'application/vnd.oasis.opendocument.text': 'ODF_textdocument_32x32.png', +# 'application/vnd.oasis.opendocument.spreadsheet': 'ODF_spreadsheet_32x32.png', +# 'application/vnd.oasis.opendocument.presentation': 'ODF_presentation_32x32.png', +# 'application/vnd.oasis.opendocument.graphics': 'ODF_drawing_32x32.png', +# 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'file_extension_xls.png', +# 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'file_extension_doc.png', +# 'application/vnd.oasis.opendocument.text': 'ODF_textdocument_32x32.png', + +class OfficeConverter(object): + def __init__(self, input_filepath): + self.backend = OfficeConverterBackendUnoconv(unoconv_path=UNOCONV_PATH) + self.input_filepath = input_filepath + self.exists = False + + # Make sure file is of a known office format + descriptor = open(self.input_filepath) + mimetype, encoding = get_mimetype(descriptor, self.input_filepath) + + if mimetype in CONVERTER_OFFICE_FILE_MIMETYPES: + # Hash file to cache results of conversion + #descriptor = open(self.input_filepath) + #file_hash = HASH_FUNCTION(descriptor.read()) + #descriptor.close() + + #self.output_filepath = os.path.join(TEMPORARY_DIRECTORY, u''.join([file_hash, CACHED_FILE_SUFFIX])) + self.output_filepath = os.path.join(TEMPORARY_DIRECTORY, u''.join([self.input_filepath, CACHED_FILE_SUFFIX])) + self.exists = os.path.exists(self.output_filepath) + print 'self.input_filepath',self.input_filepath + print 'self.output_filepath',self.output_filepath + print 'self.exists', self.exists + if not self.exists: + try: + self.backend.convert(self.input_filepath, self.output_filepath) + except OfficeBackendError, msg: + print 'OFFICE EXCEPTION' + # convert exception so that atleas the mime type icon is displayed + raise UnknownFileFormat(msg) + + + + def __unicode__(self): + return getattr(self, 'output_filepath', None) + + def __str__(self): + return str(self.__unicode__()) + + def __nonzero__(self): + return self.exists + + __bool__ = __nonzero__ + + +class OfficeConverterBackendUnoconv(object): + def __init__(self, unoconv_path=None): + self.unoconv_path = unoconv_path if unoconv_path else u'/usr/bin/unoconv' + if not os.path.exists(self.unoconv_path): + raise OfficeBackendError('cannot find unoconv executable') + + def convert(self, input_filepath, output_filepath): + """ + Executes the program unoconv using subprocess's Popen + """ + self.input_filepath = input_filepath + self.output_filepath = output_filepath + + command = [] + command.append(self.unoconv_path) + #command.append(u'-v') + command.append(u'--pipe') + command.append(u'--format="pdf"') + command.append(u'--output=%s' % self.output_filepath) + command.append(self.input_filepath) + print 'convert' + try: + proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) + return_code = proc.wait() + readline = proc.stderr.readline() + if return_code != 0: + raise OfficeBackendError(proc.stderr.readline()) + except OSError, msg: + raise OfficeBackendError(msg) From e590cb041cba77b2b0229b22f1d79900ecaf7bd2 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 21 Nov 2011 02:47:52 -0400 Subject: [PATCH 13/78] Finished office converter using MIME type detection --- apps/converter/api.py | 5 --- apps/converter/backends/python/base.py | 2 +- apps/converter/office_converter.py | 51 ++++++++++---------------- 3 files changed, 21 insertions(+), 37 deletions(-) diff --git a/apps/converter/api.py b/apps/converter/api.py index 82aefe63bb..e118cce3bb 100644 --- a/apps/converter/api.py +++ b/apps/converter/api.py @@ -47,20 +47,15 @@ def convert(input_filepath, output_filepath=None, cleanup_files=False, *args, ** if output_filepath is None: output_filepath = create_image_cache_filename(input_filepath, *args, **kwargs) - print 'cache image', output_filepath if os.path.exists(output_filepath): return output_filepath - print 'cleanup_files', cleanup_files - office_converter = OfficeConverter(input_filepath) if office_converter: try: - #cleanup_files =False. input_filepath = office_converter.output_filepath except OfficeConverter: - print 'office converter exception' raise UnknownFileFormat('office converter exception') if size: diff --git a/apps/converter/backends/python/base.py b/apps/converter/backends/python/base.py index a57e1f0d2a..a7e404bd46 100644 --- a/apps/converter/backends/python/base.py +++ b/apps/converter/backends/python/base.py @@ -64,7 +64,7 @@ class ConverterClass(ConverterBase): 'gs', '-q', '-dQUIET', '-dSAFER', '-dBATCH', '-dNOPAUSE', '-dNOPROMPT', first_page_tmpl, last_page_tmpl, - '-sDEVICE=jpeg', '-dJPEGQ=75', + '-sDEVICE=jpeg', '-dJPEGQ=95', '-r150', output_file_tmpl, input_file_tmpl, '-c "60000000 setvmthreshold"', # use 30MB diff --git a/apps/converter/office_converter.py b/apps/converter/office_converter.py index c592d4b765..093a234fd0 100644 --- a/apps/converter/office_converter.py +++ b/apps/converter/office_converter.py @@ -1,16 +1,13 @@ import os import subprocess -import hashlib from mimetype.api import get_mimetype from common.conf.settings import TEMPORARY_DIRECTORY -from converter.conf.settings import UNOCONV_PATH +from converter.conf.settings import UNOCONV_PATH, UNOCONV_USE_PIPE from converter.exceptions import (OfficeConversionError, OfficeBackendError, UnknownFileFormat) -HASH_FUNCTION = lambda x: hashlib.sha256(x).hexdigest() - CACHED_FILE_SUFFIX = u'_office_converter' CONVERTER_OFFICE_FILE_MIMETYPES = [ @@ -22,18 +19,17 @@ CONVERTER_OFFICE_FILE_MIMETYPES = [ 'application/vnd.ms-powerpoint', 'text/plain', 'application/vnd.oasis.opendocument.presentation', + 'application/vnd.oasis.opendocument.text', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + 'application/vnd.oasis.opendocument.spreadsheet', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', + 'application/vnd.oasis.opendocument.graphics', ] -# 'application/vnd.oasis.opendocument.text': 'ODF_textdocument_32x32.png', -# 'application/vnd.oasis.opendocument.spreadsheet': 'ODF_spreadsheet_32x32.png', -# 'application/vnd.oasis.opendocument.presentation': 'ODF_presentation_32x32.png', -# 'application/vnd.oasis.opendocument.graphics': 'ODF_drawing_32x32.png', -# 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'file_extension_xls.png', -# 'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'file_extension_doc.png', -# 'application/vnd.oasis.opendocument.text': 'ODF_textdocument_32x32.png', + class OfficeConverter(object): def __init__(self, input_filepath): - self.backend = OfficeConverterBackendUnoconv(unoconv_path=UNOCONV_PATH) + self.backend_class = OfficeConverterBackendUnoconv self.input_filepath = input_filepath self.exists = False @@ -42,27 +38,17 @@ class OfficeConverter(object): mimetype, encoding = get_mimetype(descriptor, self.input_filepath) if mimetype in CONVERTER_OFFICE_FILE_MIMETYPES: - # Hash file to cache results of conversion - #descriptor = open(self.input_filepath) - #file_hash = HASH_FUNCTION(descriptor.read()) - #descriptor.close() - - #self.output_filepath = os.path.join(TEMPORARY_DIRECTORY, u''.join([file_hash, CACHED_FILE_SUFFIX])) + # Cache results of conversion self.output_filepath = os.path.join(TEMPORARY_DIRECTORY, u''.join([self.input_filepath, CACHED_FILE_SUFFIX])) self.exists = os.path.exists(self.output_filepath) - print 'self.input_filepath',self.input_filepath - print 'self.output_filepath',self.output_filepath - print 'self.exists', self.exists if not self.exists: try: + self.backend = self.backend_class() self.backend.convert(self.input_filepath, self.output_filepath) except OfficeBackendError, msg: - print 'OFFICE EXCEPTION' - # convert exception so that atleas the mime type icon is displayed + # convert exception so that at least the mime type icon is displayed raise UnknownFileFormat(msg) - - def __unicode__(self): return getattr(self, 'output_filepath', None) @@ -76,8 +62,8 @@ class OfficeConverter(object): class OfficeConverterBackendUnoconv(object): - def __init__(self, unoconv_path=None): - self.unoconv_path = unoconv_path if unoconv_path else u'/usr/bin/unoconv' + def __init__(self): + self.unoconv_path = UNOCONV_PATH if UNOCONV_PATH else u'/usr/bin/unoconv' if not os.path.exists(self.unoconv_path): raise OfficeBackendError('cannot find unoconv executable') @@ -90,12 +76,15 @@ class OfficeConverterBackendUnoconv(object): command = [] command.append(self.unoconv_path) - #command.append(u'-v') - command.append(u'--pipe') - command.append(u'--format="pdf"') + + if UNOCONV_USE_PIPE: + command.append(u'--pipe') + command.append(u'mayan') + + command.append(u'--format=pdf') command.append(u'--output=%s' % self.output_filepath) command.append(self.input_filepath) - print 'convert' + try: proc = subprocess.Popen(command, close_fds=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) return_code = proc.wait() From e4b9b135dac3aa9e2dc0d55864acc8c0c6fb800d Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 21 Nov 2011 02:48:29 -0400 Subject: [PATCH 14/78] Added new configuration option CONVERTER_UNOCONV_USE_PIPE to let uniconv use pipes instead of ports to call libreoffice --- apps/converter/conf/settings.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/apps/converter/conf/settings.py b/apps/converter/conf/settings.py index 08377880b4..1dbfe4d6bc 100644 --- a/apps/converter/conf/settings.py +++ b/apps/converter/conf/settings.py @@ -1,4 +1,5 @@ -"""Configuration options for the converter app""" +'''Configuration options for the converter app''' + from django.utils.translation import ugettext_lazy as _ from smart_settings.api import register_settings @@ -12,7 +13,9 @@ register_settings( {'name': u'GM_PATH', 'global_name': u'CONVERTER_GM_PATH', 'default': u'/usr/bin/gm', 'description': _(u'File path to graphicsmagick\'s program.'), 'exists': True}, {'name': u'GM_SETTINGS', 'global_name': u'CONVERTER_GM_SETTINGS', 'default': u''}, {'name': u'GRAPHICS_BACKEND', 'global_name': u'CONVERTER_GRAPHICS_BACKEND', 'default': u'converter.backends.python', 'description': _(u'Graphics conversion backend to use. Options are: converter.backends.imagemagick, converter.backends.graphicsmagick and converter.backends.python.')}, - {'name': u'UNOCONV_PATH', 'global_name': u'CONVERTER_UNOCONV_PATH', 'default': u'/usr/bin/unoconv', 'exists': True}, + {'name': u'UNOCONV_PATH', 'global_name': u'CONVERTER_UNOCONV_PATH', 'default': u'/usr/bin/unoconv', 'exists': True, 'description': _(u'Path to the unoconv program.')}, + {'name': u'UNOCONV_USE_PIPE', 'global_name': u'CONVERTER_UNOCONV_USE_PIPE', 'default': True, 'description': _(u'Use alternate method of connection to LibreOffice using a pipe, it is slower but less prone to segmentation faults.')}, + #{'name': u'OCR_OPTIONS', 'global_name': u'CONVERTER_OCR_OPTIONS', 'default': u'-colorspace Gray -depth 8 -resample 200x200'}, #{'name': u'HIGH_QUALITY_OPTIONS', 'global_name': u'CONVERTER_HIGH_QUALITY_OPTIONS', 'default': u'-density 400'}, #{'name': u'PRINT_QUALITY_OPTIONS', 'global_name': u'CONVERTER_PRINT_QUALITY_OPTIONS', 'default': u'-density 500'}, From 9b95201b4d656a79ee82a9d4cf11578de68ec289 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 21 Nov 2011 05:38:05 -0400 Subject: [PATCH 15/78] Moved project wide schedule instance to the runtime.py file --- apps/scheduler/__init__.py | 3 --- apps/scheduler/api.py | 2 +- apps/scheduler/runtime.py | 4 ++++ 3 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 apps/scheduler/runtime.py diff --git a/apps/scheduler/__init__.py b/apps/scheduler/__init__.py index a9440e946b..8b13789179 100644 --- a/apps/scheduler/__init__.py +++ b/apps/scheduler/__init__.py @@ -1,4 +1 @@ -from apscheduler.scheduler import Scheduler -scheduler = Scheduler() -scheduler.start() diff --git a/apps/scheduler/api.py b/apps/scheduler/api.py index 59162e10d0..5f79d2433b 100644 --- a/apps/scheduler/api.py +++ b/apps/scheduler/api.py @@ -1,4 +1,4 @@ -from scheduler import scheduler +from scheduler.runtime import scheduler from scheduler.exceptions import AlreadyScheduled registered_jobs = {} diff --git a/apps/scheduler/runtime.py b/apps/scheduler/runtime.py new file mode 100644 index 0000000000..a9440e946b --- /dev/null +++ b/apps/scheduler/runtime.py @@ -0,0 +1,4 @@ +from apscheduler.scheduler import Scheduler + +scheduler = Scheduler() +scheduler.start() From e8f62874dd74223f748f64a8924a2d8790168f64 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 21 Nov 2011 05:38:35 -0400 Subject: [PATCH 16/78] Updated generic_config template to store the views' previous url --- apps/common/templates/generic_confirm.html | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/apps/common/templates/generic_confirm.html b/apps/common/templates/generic_confirm.html index 10a6e927d6..b020583e2e 100644 --- a/apps/common/templates/generic_confirm.html +++ b/apps/common/templates/generic_confirm.html @@ -22,7 +22,11 @@