From b8aa5fa974862fae25345931c7504cbd6cd53e56 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 19 Jul 2012 02:39:54 -0400 Subject: [PATCH] Initial import of the database bootstrap app --- apps/bootstrap/__init__.py | 10 +++ apps/bootstrap/api.py | 79 ++++++++++++++++++ apps/bootstrap/links.py | 9 ++ apps/bootstrap/models.py | 12 +++ .../images/icons/database_lightning.png | Bin 0 -> 1880 bytes apps/bootstrap/urls.py | 6 ++ apps/bootstrap/views.py | 59 +++++++++++++ settings.py | 1 + urls.py | 1 + 9 files changed, 177 insertions(+) create mode 100644 apps/bootstrap/__init__.py create mode 100644 apps/bootstrap/api.py create mode 100644 apps/bootstrap/links.py create mode 100644 apps/bootstrap/models.py create mode 100755 apps/bootstrap/static/images/icons/database_lightning.png create mode 100644 apps/bootstrap/urls.py create mode 100644 apps/bootstrap/views.py diff --git a/apps/bootstrap/__init__.py b/apps/bootstrap/__init__.py new file mode 100644 index 0000000000..d583de1073 --- /dev/null +++ b/apps/bootstrap/__init__.py @@ -0,0 +1,10 @@ +from __future__ import absolute_import + +from project_setup.api import register_setup +from navigation.api import register_links#, register_sidebar_template + +from .links import database_bootstrap, bootstrap_execute +from .api import BootstrapSimple + +register_setup(database_bootstrap) +register_links(BootstrapSimple, [bootstrap_execute]) diff --git a/apps/bootstrap/api.py b/apps/bootstrap/api.py new file mode 100644 index 0000000000..a5e6c40143 --- /dev/null +++ b/apps/bootstrap/api.py @@ -0,0 +1,79 @@ +from __future__ import absolute_import + +from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ugettext + +from metadata.models import MetadataType, MetadataSet +from document_indexing.models import Index, IndexTemplateNode + +bootstrap_options = {} + + +class BootstrapBase(object): + name = None + label = '' + description = '' + + def __unicode__(self): + return unicode(self.label) + + +class BootstrapSimple(BootstrapBase): + name = 'simple' + label = _(u'Simple') + description = _(u'A simple setup providing an uploaded date metadata and index plus an alphabetic index based on document filenames.') + + def execute(self): + # Create metadata types + date = MetadataType.objects.create(name='upload_date', title=ugettext(u'Upload date'), default='current_date()') + + # Create a segmented date index + index = Index.objects.create(name='date_tree', title=ugettext(u'Segmented date index'), enabled=True) + + # Create index template + #node1 = IndexTemplateNode + + +for bootstrap in [BootstrapSimple()]: + bootstrap_options[bootstrap.name] = bootstrap + +""" + +class Index(models.Model): + name = models.CharField(unique=True, max_length=64, verbose_name=_(u'name'), help_text=_(u'Internal name used to reference this index.')) + title = models.CharField(unique=True, max_length=128, verbose_name=_(u'title'), help_text=_(u'The name that will be visible to users.')) + enabled = models.BooleanField(default=True, verbose_name=_(u'enabled'), help_text=_(u'Causes this index to be visible and updated when document data changes.')) + + @property + def template_root(self): + return self.indextemplatenode_set.get(parent=None) + + @property + def instance_root(self): + return self.template_root.indexinstancenode_set.get() + + def __unicode__(self): + return self.title + + @models.permalink + def get_absolute_url(self): + return ('index_instance_node_view', [self.instance_root.pk]) + + def save(self, *args, **kwargs): + super(Index, self).save(*args, **kwargs) + index_template_node_root, created = IndexTemplateNode.objects.get_or_create(parent=None, index=self) + + class Meta: + verbose_name = _(u'index') + verbose_name_plural = _(u'indexes') + + +class IndexTemplateNode(MPTTModel): + parent = TreeForeignKey('self', null=True, blank=True, related_name='index_template_node') + index = models.ForeignKey(Index, verbose_name=_(u'index')) + expression = models.CharField(max_length=128, verbose_name=_(u'indexing expression'), help_text=_(u'Enter a python string expression to be evaluated.')) + # % available_indexing_functions_string) + enabled = models.BooleanField(default=True, verbose_name=_(u'enabled'), help_text=_(u'Causes this node to be visible and updated when document data changes.')) + link_documents = models.BooleanField(default=False, verbose_name=_(u'link documents'), help_text=_(u'Check this option to have this node act as a container for documents and not as a parent for further nodes.')) +""" + diff --git a/apps/bootstrap/links.py b/apps/bootstrap/links.py new file mode 100644 index 0000000000..e2af43dbaa --- /dev/null +++ b/apps/bootstrap/links.py @@ -0,0 +1,9 @@ +from django.utils.translation import ugettext_lazy as _ + + +def is_superuser(context): + return context['request'].user.is_staff or context['request'].user.is_superuser + + +database_bootstrap = {'text': _(u'bootstrap database'), 'view': 'bootstrap_type_list', 'icon': 'database_lightning.png', 'condition': is_superuser}#, 'children_view_regex': [r'statistics']} +bootstrap_execute = {'text': _(u'execute'), 'view': 'bootstrap_execute', 'args': 'object.name', 'sprite': 'database_lightning.png', 'condition': is_superuser}#, 'children_view_regex': [r'statistics']} diff --git a/apps/bootstrap/models.py b/apps/bootstrap/models.py new file mode 100644 index 0000000000..1f13d1559f --- /dev/null +++ b/apps/bootstrap/models.py @@ -0,0 +1,12 @@ +from __future__ import absolute_import + +#import logging + +#from django.db import models +#from django.utils.translation import ugettext_lazy as _ + +#logger = logging.getLogger(__name__) + + +#class CompanyType(models.Model): +# name = diff --git a/apps/bootstrap/static/images/icons/database_lightning.png b/apps/bootstrap/static/images/icons/database_lightning.png new file mode 100755 index 0000000000000000000000000000000000000000..373590bef648df5db70adb216c94ac181727cc39 GIT binary patch literal 1880 zcmV-e2dDUnP)(=%41OcIN7@DS`w>OGtG>Swr0Yle6eeI0A~l}XAZSz1y*qt41#GMO6H;HYWk zjxe1kPoG@EY>*^|bHJ(VI{ZF2I=gyqMZ}bh)39l$t3QA#lLKJ7nT?Sg5ZKD9d$sKLe8kRmfy>i1x*h z%jS_tB?(;mhz`iI1dq!N)v2Ju??ahWMKYbTbmj|%k=OE^Ba_bJ#2-!Yy4|G251-Eq zkGmYTwKcFZYO2*CpQ56M&-1!6&WFo-^KK9NZBd0})11hOECHhQxZbubi0IMN4M zk|0U46(OS}dzNJf=@cm|1!%fPHP%ol=+<4MdK(5CGiaS;*Jczv%SDJ3R$`DBu(9zSoli3(41DYrZx=9S^?g?TNbv_3nw-~V{C63)DG|$vK;+GIO`yu^h zDkZB-Y<{j!dghfc7e8Z)G6X6oDa6#3+f{lg-(Lcc*T4Aj1&^Zf=rLUWr`4hn1ESMe z4zJg3;lM-S6uDzeIZM4rrDNdBY6e`9R`?%Sgvy7WMsbqLBtDoGix{Hwc9!l9>@CrzQ3|KeG{fuhppv4m ztE+~CkneJpq1c&CvlQ06L9x{Y_^yU6P^<1nu4fn3!)28LrVh3GS){IRM&f1`+m2jY zOA}#|>_8Tj^zTe2i>_Rdvd}w#hLYqcp`pNONc5u=#x7; zqKtGOLZVB@ij5~(?IXq7529hnrlB}q8ZvJvJMmVO2PVPDUa_JU2ooYXA_!Ro&voL| zKatNmBbghtAT_u(Su`Y6vs|LPduSRCM?t&IQVj7ns8cIp7NQiqYU6{3gXdZh?}%Y` zt$S7T#s#Y^)Uc*TrDXK2dv(8R<;v^zE?&5hXn16J8nWkG4)G313iAajL5$Y;?3l+2PhZ%2#Oc=_?(rDdx3Zs3u}N>9kv79Q`5Hny}} zvJQ~!;2~M5mxiQaF&nq-h>`-wjeT%d1t6$7G9JUl-M?aQY<}bFhSu+R%~ON7W{Js> zL%$rqXWI12bS6_D>5U+p$ylA9op`(+AN2*9M6nK-?9QCDovc)*7ViI1e3WWvHlPu(}YuKtm#K=oYuJaah|rK*U2 z6NCB~MQ>8u)dUrldPjbt{)19kKO3DFT0$Q*Tv~PTT>LK{d3?AJ6QLv%tb?#^tehPY z_+905;wYeyep)^C?b=DMMd^YWnYX5S8IPLv{`5$nX17iac74)(OyM#X<8V1e-(3OM zqP&hI8DC06?R?uUjCL#)@c&$_gm-eMyGTOZ__+D>UoVB13_O#Cu`c{qfB^unGa~y0 S1J<7a0000\w+)/execute/$', 'bootstrap_execute', (), 'bootstrap_execute'), +) diff --git a/apps/bootstrap/views.py b/apps/bootstrap/views.py new file mode 100644 index 0000000000..3be20b1dc1 --- /dev/null +++ b/apps/bootstrap/views.py @@ -0,0 +1,59 @@ +from __future__ import absolute_import + +from django.utils.translation import ugettext_lazy as _ +from django.http import HttpResponseRedirect +from django.shortcuts import render_to_response, get_object_or_404 +from django.template import RequestContext +from django.contrib import messages +from django.core.urlresolvers import reverse + +from .api import bootstrap_options + + +def bootstrap_type_list(request): + # TODO: Check for superadmin + + context = { + 'object_list': bootstrap_options.values(), + 'title': _(u'database bootstrap setups'), + 'hide_link': True, + 'extra_columns': [ + {'name': _(u'description'), 'attribute': 'description'}, + #{'name': _(u'label'), 'attribute': encapsulate(lambda document: document.checkout_info().expiration_datetime)}, + ], + } + + return render_to_response('generic_list.html', context, + context_instance=RequestContext(request)) + + +def bootstrap_execute(request, bootstrap_name): + bootstrap = bootstrap_options[bootstrap_name] + + post_action_redirect = reverse('bootstrap_type_list') + + previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', '/'))) + next = request.POST.get('next', request.GET.get('next', post_action_redirect if post_action_redirect else request.META.get('HTTP_REFERER', '/'))) + + if request.method == 'POST': + try: + bootstrap.execute() + except Exception, exc: + messages.error(request, _(u'Error executing bootstrap setup; %s') % exc) + else: + messages.success(request, _(u'Bootstrap setup "%s" executed successfully.') % bootstrap) + return HttpResponseRedirect(next) + + context = { + 'object_name': _(u'bootstrap setup'), + 'delete_view': False, + 'previous': previous, + 'next': next, + #'form_icon': u'basket_remove.png', + 'object': bootstrap, + } + + context['title'] = _(u'Are you sure you wish to execute the database bootstrap named: %s?') % bootstrap.label + + return render_to_response('generic_confirm.html', context, + context_instance=RequestContext(request)) diff --git a/settings.py b/settings.py index b4fb25d35d..026e444c5f 100644 --- a/settings.py +++ b/settings.py @@ -168,6 +168,7 @@ INSTALLED_APPS = ( 'documents', 'linking', 'document_indexing', + 'bootstrap', 'document_acls', 'ocr', 'sources', diff --git a/urls.py b/urls.py index 79bc1fab54..b2d5ccd4f3 100644 --- a/urls.py +++ b/urls.py @@ -34,6 +34,7 @@ urlpatterns = patterns('', (r'^checkouts/', include('checkouts.urls')), (r'^installation/', include('installation.urls')), (r'^scheduler/', include('scheduler.urls')), + (r'^bootstrap/', include('bootstrap.urls')), )