Coverage for lino/modlib/uploads/models.py : 57%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: UTF-8 -*- # Copyright 2008-2015 Luc Saffre # License: BSD (see file COPYING for details) Database models for `lino.modlib.uploads`. """
"""The type of an upload.
.. attribute:: shortcut
Optional pointer to a virtual **upload shortcut** field. If this is not empty, then the given shortcut field will manage uploads of this type. See also :class:`Shortcuts <lino.modlib.uploads.choicelists.Shortcuts>`.
"""
_("Max. number"), default=-1, help_text=string_concat( _("No need to upload more uploads than N of this type."), "\n", _("-1 means no limit."))) _("Wanted"), default=False, help_text=_("Add a (+) button when there is no upload of this type."))
"""The table with all existing upload types.
This usually is accessible via the `Configure` menu. """
name upload_area """
id upload_area wanted max_number shortcut name uploads.UploadsByType """
i = name.rfind('/') if i != -1: return name[i + 1:] return name
"""Represents an uploaded file."""
"uploads.UploadType", blank=True, null=True)
_("Description"), max_length=200, blank=True)
if self.description: s = self.description elif self.file: s = filename_leaf(self.file.name) else: s = str(self.id) if self.type: s = str(self.type) + ' ' + s return s
def type_choices(self, upload_area): M = dd.resolve_model('uploads.UploadType') logger.info("20140430 type_choices %s", upload_area) if upload_area is None: return M.objects.all() return M.objects.filter(upload_area=upload_area)
if self.type is not None: self.upload_area = self.type.upload_area super(Upload, self).save(*args, **kw)
"Shows all Uploads"
file user upload_area type description owner """, window_size=(80, 'auto'))
type description file user """
# user=models.ForeignKey( # 'users.User', blank=True, null=True, # verbose_name=_("Uploaded by")), upload_type=models.ForeignKey( 'uploads.UploadType', blank=True, null=True))
# simple_parameters = ['user']
def get_request_queryset(cls, ar): qs = super(Uploads, cls).get_request_queryset(ar) pv = ar.param_values
if pv.user: qs = qs.filter(user=pv.user)
if pv.upload_type: qs = qs.filter(type=pv.upload_type)
return qs
"""Shows only my Uploads (i.e. those whose author is current user).""" # order_by = ["modified"]
# @classmethod # def get_actor_label(self): # return _("My %s") % _("Uploads")
# @classmethod # def param_defaults(self, ar, **kw): # kw = super(MyUploads, self).param_defaults(ar, **kw) # kw.update(user=ar.get_user()) # return kw
def get_known_values(self): return dict(upload_area=self._upload_area)
def get_actor_label(self): if self._upload_area is not None: return self._upload_area.text return self._label or self.__name__
def format_row_in_slave_summary(self, ar, obj): """almost as unicode, but without the type """ return obj.description or filename_leaf(obj.file.name) \ or str(obj.id)
def get_slave_summary(self, obj, ar): """Displays the uploads related to this controller as a list grouped by uploads type.
Note that this also works on :class:`lino_welfare.modlib.uploads.models.UploadsByClient` and their subclasses for the different `_upload_area`.
""" UploadType = rt.modules.uploads.UploadType # Upload = rt.modules.uploads.Upload elems = [] types = []
perm = ar.get_user().profile.has_required_roles(self.required_roles)
for ut in UploadType.objects.filter(upload_area=self._upload_area): sar = ar.spawn( self, master_instance=obj, known_values=dict(type_id=ut.id)) # logger.info("20140430 %s", sar.data_iterator.query) files = [] for m in sar: text = self.format_row_in_slave_summary(ar, m) if text is None: continue edit = ar.obj2html( m, text, # _("Edit"), # icon_name='application_form', title=_("Edit metadata of the uploaded file.")) if m.file.name: show = ar.renderer.href_button( settings.SITE.build_media_url(m.file.name), # u"\u21A7", # DOWNWARDS ARROW FROM BAR (↧) # u"\u21E8", u"\u21f2", # SOUTH EAST ARROW TO CORNER (⇲) style="text-decoration:none;", # _(" [show]"), # fmt(m), target='_blank', # icon_name=settings.SITE.build_static_url( # 'images/xsite/link'), # icon_name='page_go', # style="vertical-align:-30%;", title=_("Open the uploaded file in a new browser window")) # logger.info("20140430 %s", E.tostring(e)) files.append(E.span(edit, ' ', show)) else: files.append(edit) if perm and ut.wanted \ and (ut.max_number < 0 or len(files) < ut.max_number): btn = self.insert_action.request_from( sar, master_instance=obj, known_values=dict(type_id=ut.id)).ar2button() if btn is not None: files.append(btn) if len(files) > 0: e = E.p(str(ut), ': ', *join_elems(files, ', ')) types.append(e) # logger.info("20140430 %s", [E.tostring(e) for e in types]) if len(types) == 0: elems.append(E.ul(E.li(ar.no_data_text))) else: elems.append(E.ul(*[E.li(e) for e in types])) return E.div(*elems)
"UploadsByController"
file type description """
def format_upload(self, obj): return str(obj.type)
|