Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_unknown_type(self):
document_type = DocumentType.unknown_type()
self.assertEqual(document_type.mimetype, "application/octet-stream")
self.assertEqual(document_type.name, "Unknown type")
def render(self, context):
document_types = DocumentType.objects.exclude(extension__exact='').order_by('extension')
context[self.as_varname] = document_types
return ''
Forces a JOIN to DocumentType and User models.
Cannot be achieved by setting list_select_related=True, because
the foreign key fields have null=True. We have an OUTER JOIN here.
"""
qs = super(DocumentAdmin, self).queryset(request)
return qs.select_related('type', 'added_by')
def save_model(self, request, obj, form, change):
if not change:
obj.added_by = request.user
super(DocumentAdmin, self).save_model(request, obj, form, change)
admin.site.register(DocumentStatus, DocumentStatusAdmin)
admin.site.register(DocumentType, DocumentTypeAdmin)
admin.site.register(Document, DocumentAdmin)
mimetype = mimetype or "application/octet-stream"
obj, created = cls.objects.get_or_create(extension=extension,
mimetype=mimetype)
if created:
obj.name = extension.upper() + ' Document'
obj.save()
return obj
class Document(models.Model):
u"""
The document itself.
"""
# Basic document data.
file = models.FileField(verbose_name=_("file"), upload_to='uploads/%Y/%m/%d/', help_text=_("This is the document itself - well, just a file."))
type = models.ForeignKey(DocumentType, verbose_name=_("document type"), blank=True, null=True, help_text=_("Document type, for example 'Microsoft Word Document' or 'PDF File'."))
# Meta-information.
status = models.ForeignKey(DocumentStatus, verbose_name=_("document status"), null=True, blank=True)
added_by = models.ForeignKey(User, verbose_name=_("added by"), null=True, blank=True, editable=False)
added_at = models.DateTimeField(_("added at"), auto_now_add=True)
updated_at = models.DateTimeField(_("recently changed at"), auto_now=True)
class Meta:
verbose_name = _("Document")
verbose_name_plural = _("Documents")
ordering = ['-added_at']
def __unicode__(self):
return os.path.basename(self.file.name)
def save(self, *args, **kwargs):
u"""
A very thin wrapper for the generic ``object_list`` view.
Currently we supply here only the ``template_object_name`` argument, but
more functionality may come and it is good to keep the generic document
list in one place.
This also allows for a greater customization by setting some view arguments
in the URLconf.
Template receives a ``document_list`` context variable which is a QuerySet
instance.
"""
extra_context = kwargs.pop('extra_context', {})
if extension is not None:
# we could get around without this query for DocumentType,
# but we want the type object to be accessible in the template
document_type = get_object_or_404(DocumentType, extension__exact=extension.lower())
extra_context['document_type'] = document_type
queryset = queryset.filter(type=document_type)
if 'template_object_name' not in kwargs:
kwargs['template_object_name'] = 'document'
return object_list(request, queryset, template_name=template_name, extra_context=extra_context, **kwargs)