PageRenderTime 126ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/djpcms/contrib/flowrepo/cms/__init__.py

https://github.com/strogo/djpcms
Python | 274 lines | 208 code | 48 blank | 18 comment | 35 complexity | cf3ebe1f5958c05ba13873f366faae3a MD5 | raw file
  1. from django.contrib.contenttypes.models import ContentType
  2. from django.utils.encoding import force_unicode
  3. from djpcms.views import appview, appsite
  4. from djpcms.utils.html import htmlwrap, Paginator
  5. from djpcms.apps.included.tagging import ArchiveTaggedApplication, TagApplication
  6. from djpcms.contrib.flowrepo.models import FlowRelated, FlowItem, Report
  7. from djpcms.contrib.flowrepo.models import Message, Category, Image, Attachment
  8. from djpcms.contrib.flowrepo.cms.forms import *
  9. class FlowMainView(appview.SearchView):
  10. pass
  11. def addContentModel(self, djp, **kwargs):
  12. try:
  13. djp.kwargs['content_model'] = self.appmodel.get_content_model(kwargs['content'])
  14. except:
  15. raise http.Http404
  16. def get_content_url(self, djp, **kwargs):
  17. addContentModel(self,djp,**kwargs)
  18. return super(self.__class__,self).get_url(djp, **kwargs)
  19. class ContentView(appview.SearchView):
  20. def get_url(self, djp, **kwargs):
  21. return get_content_url(self, djp, **kwargs)
  22. def title(self, page, content_model = None, **kwargs):
  23. if content_model:
  24. return force_unicode(content_model._meta.verbose_name_plural)
  25. else:
  26. return super(ContentView,self).title(page,**kwargs)
  27. def appquery(self, djp):
  28. qs = super(ContentView,self).appquery(djp)
  29. content_model = djp.getdata('content_model')
  30. if content_model:
  31. ctype = ContentType.objects.get_for_model(content_model)
  32. return qs.filter(content_type = ctype)
  33. else:
  34. return self.model.objects.none()
  35. class FlowAddView(appview.AddView):
  36. def get_url(self, djp, **kwargs):
  37. return get_content_url(self, djp, **kwargs)
  38. def title(self, page, content_model = None, **kwargs):
  39. t = self.appmodel.add_title.get(content_model,None)
  40. if not t:
  41. return super(FlowAddView,self).title(page, content = None, **kwargs)
  42. else:
  43. return t
  44. class FlowEditView(appview.EditView):
  45. def get_url(self, djp, **kwargs):
  46. url = super(FlowEditView,self).get_url(djp, **kwargs)
  47. djp.kwargs['content_model'] = djp.instance.object.__class__
  48. return url
  49. slug_regex = '(?P<id>[-\.\w]+)'
  50. class FlowItemApplication(ArchiveTaggedApplication):
  51. '''Mighty Flow Application!'''
  52. date_code = 'timestamp'
  53. form_withrequest = True
  54. form = FlowForm
  55. search_fields = ['name','description']
  56. form_ajax = False
  57. split_days = True
  58. inherit = True
  59. insitemap = True
  60. content_names = {Report:'writing'} # dictionary for mapping a model into a name used for url
  61. content_forms = {Report: NiceReportForm,
  62. Image: NiceUloaderForm,
  63. Attachment: NiceUloaderForm}
  64. form_templates = {Report: 'flowrepo/report-form.html'}
  65. add_title = {Report: 'write', Image: 'upload'}
  66. main = FlowMainView()
  67. search = appview.SearchView(regex = 'search', parent = 'main', in_navigation = 0)
  68. autocomplete = appview.AutocompleteView(display = 'name', parent = 'main')
  69. upload_file = appview.AddView(regex = 'upload', form = NiceUloaderForm, parent = 'main')
  70. applications = ContentView(regex = '(?P<content>[-\w]+)', parent = 'main')
  71. add = FlowAddView(parent = 'applications')
  72. view = appview.ViewView(regex = slug_regex, parent = 'applications')
  73. edit = FlowEditView()
  74. delete = appview.DeleteView()
  75. class Media:
  76. css = {'all': ('flowrepo/flowrepo.css',)}
  77. def __init__(self, *args, **kwargs):
  78. content_models = {}
  79. self.content_names = kwargs.pop('content_names',self.content_names)
  80. for cn,model in FlowItem.objects.models_by_name.items():
  81. cname = self.content_names.get(model,cn)
  82. self.content_names[model] = cname
  83. content_models[cname] = model
  84. self.content_models = content_models
  85. super(FlowItemApplication,self).__init__(*args, **kwargs)
  86. def get_content_model(self, content):
  87. if isinstance(content,basestring):
  88. model = self.content_models.get(content,None)
  89. return self.model.objects.models_by_name.get(model.__name__.lower(), None)
  90. else:
  91. return content
  92. def form_template(self, djp):
  93. content = self.get_content_model(djp.kwargs.get('content',None))
  94. return self.form_templates.get(content,None)
  95. def get_form(self, djp, form_class, **kwargs):
  96. content = djp.getdata('content_model')
  97. if not form_class:
  98. form_class = self.content_forms.get(content,form_class)
  99. uni = super(FlowItemApplication,self).get_form(djp,form_class,forceform=True,**kwargs)
  100. if content:
  101. uni.addClass(str(content._meta).replace('.','-'))
  102. return uni
  103. def objptr(self, object):
  104. try:
  105. return object.object
  106. except:
  107. return object
  108. def flowitem(self, object):
  109. try:
  110. return object.flowitem()
  111. except:
  112. return None
  113. def modelsearch(self):
  114. return FlowItem
  115. def basequery(self, djp):
  116. user = djp.request.user
  117. return FlowItem.objects.public(user = user, model = self.model)
  118. def get_search_fields(self):
  119. if self.search_fields:
  120. return self.search_fields
  121. else:
  122. from django.contrib.admin import site
  123. admin = site._registry.get(FlowItem,None)
  124. if admin:
  125. return admin.search_fields
  126. def has_permission(self, request = None, obj=None):
  127. '''
  128. evaluate only if obj is an instance of flowitem
  129. '''
  130. obj = self.flowitem(obj)
  131. if not obj:
  132. return True
  133. if not request:
  134. return obj.can_user_view()
  135. else:
  136. return obj.can_user_view(request.user)
  137. def has_edit_permission(self, request = None, obj=None):
  138. obj = self.flowitem(obj)
  139. if super(FlowItemApplication,self).has_edit_permission(request, obj):
  140. if obj:
  141. return request.user in obj.authors.all()
  142. else:
  143. return True
  144. else:
  145. return False
  146. def get_object_view_template(self, obj, wrapper):
  147. item = self.flowitem(obj)
  148. obj = item.object
  149. opts = obj._meta
  150. template_name = '%s.html' % opts.module_name
  151. return ['%s/%s' % (opts.app_label,template_name),
  152. 'djpcms/components/object.html']
  153. def get_item_template(self, obj, wrapper):
  154. model = self.objptr(obj)._meta.module_name
  155. return ['components/%s_list_item.html' % model,
  156. 'flowrepo/%s_list_item.html' % model,
  157. 'flowrepo/flowitem_list_item.html']
  158. def title_object(self, obj):
  159. object = self.objptr(obj)
  160. name = getattr(object,'name',None)
  161. if not name:
  162. return str(obj)
  163. else:
  164. return name
  165. def object_content(self, djp, obj):
  166. '''
  167. Utility function for getting more content out of an instance of a model
  168. '''
  169. c = super(FlowItemApplication,self).object_content(djp,obj)
  170. c['authors'] = obj.niceauthors()
  171. c['external'] = True
  172. return c
  173. def objectbits(self, instance):
  174. '''
  175. Get arguments from model instance used to construct url
  176. By default it is the object id
  177. @param obj: instance of self.model
  178. @return: dictionary of url bits
  179. '''
  180. obj = instance.object
  181. if not obj:
  182. instance.delete()
  183. return None
  184. rep = {'content':self.content_names[obj.__class__]}
  185. if hasattr(obj,'slug'):
  186. rep['id'] = obj.slug
  187. else:
  188. rep['id'] = obj.id
  189. return rep
  190. def get_object(self, *args, **kwargs):
  191. '''
  192. Retrive an instance of self.model for arguments.
  193. By default arguments is the object id,
  194. Reimplement for custom arguments
  195. '''
  196. try:
  197. model = self.get_content_model(kwargs['content'])
  198. if model:
  199. id = kwargs.get('id',None)
  200. try:
  201. obj = model.objects.get(slug = id)
  202. except:
  203. try:
  204. obj = model.objects.get(id = int(id))
  205. except:
  206. return None
  207. return FlowItem.objects.get_from_instance(obj)
  208. except:
  209. return None
  210. class WebAccountApplication(TagApplication):
  211. name = 'webaccount'
  212. form = NiceWebAccountForm
  213. form_withrequest = True
  214. inherit = True
  215. add = appview.AddView(regex = 'add', parent = None)
  216. edit = appview.EditView(regex = 'edit/(?P<id>\d+)', parent = None)
  217. delete = appview.DeleteView(regex = 'delete/(?P<id>\d+)', parent = None)
  218. def has_permission(self, request = None, obj = None):
  219. if not request:
  220. return False
  221. try:
  222. return request.user.id == getattr(request.site.settings,'FOR_USER_ID',None)
  223. except:
  224. False
  225. def permissionDenied(self, djp):
  226. raise djp.http.Http404('bad link')