Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def recent_documents(request, **kwargs):
u"""
Renders a list of all documents sorted by creation date.
"""
queryset = Document.objects.all().order_by('-added_at')
return document_list(request, queryset, "briefcase/recent_documents.html", **kwargs)
def my_documents(request, **kwargs):
u"""
Renders a list of all documents added by current user.
We need a User here, not an AnonymousUser, so the view is wrapped with
login_required decorator.
"""
queryset = Document.objects.filter(added_by=request.user)
return document_list(request, queryset, "briefcase/my_documents.html", **kwargs)
def documents_for_user(request, username=None, user_id=None, **kwargs):
u"""
Renders a list of documents added by a specific user.
The user can be identified by his username or user_id. If both are
specified, username takes precedence.
"""
if username is not None:
queryset = Document.objects.filter(added_by__username=username)
elif user_id is not None:
queryset = Document.objects.filter(added_by_id=user_id)
else:
raise AttributeError(_("documents_for_user requires either username or user_id."))
return document_list(request, queryset, "briefcase/documents_for_user.html", **kwargs)
def save(self, *args, **kwargs):
u"""
Attaches a guessed DocumentType to the Document object.
The check for id is a standard way to determine whether the object
is created (no row in the database yet, hence no id) or updated.
"""
if not self.id:
self.type = DocumentType.type_for_file(self.file)
super(Document, self).save(*args, **kwargs)
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)
def download_document(request, document_id):
u"""
Sends the document to the browser.
Needs some kind of access control. Is the current user authorized to look at
these (possibly classified...?) data? For now, just allow everybody. Or,
maybe, set up an access policy somewhere else and check it here.
"""
document = get_object_or_404(Document, pk=document_id)
response = HttpResponse(content=document.file, mimetype=document.type.mimetype)
response['Content-Disposition'] = 'attachment; filename=%s' % document.get_filename()
return response