Commit 2523da11fb7ff8a73f5872662e2888c8fd5e49c2
1 parent
ac55c658
Exists in
master
and in
2 other branches
Subject backup initials (some tests)
Showing
2 changed files
with
39 additions
and
1 deletions
Show diff stats
subjects/urls.py
... | ... | @@ -18,6 +18,7 @@ urlpatterns = [ |
18 | 18 | url(r'^view_log/(?P<subject>[\w_-]+)/$', views.subject_view_log, name = 'view_log'), |
19 | 19 | url(r'^most_accessed_subjects/$', views.most_acessed_subjects, name='most_acessed'), |
20 | 20 | url(r'^report/', include('reports.urls', namespace='reports')), |
21 | + url(r'^backup/$', views.backup, name='backup'), | |
21 | 22 | url(r'^(?P<option>[\w_-]+)/$', views.IndexView.as_view(), name='index'), |
22 | 23 | |
23 | 24 | ] |
24 | 25 | \ No newline at end of file | ... | ... |
subjects/views.py
1 | - | |
2 | 1 | from django.shortcuts import render, get_object_or_404, redirect |
3 | 2 | from django.views.generic import ListView, CreateView, DeleteView, UpdateView, TemplateView, DetailView |
4 | 3 | from categories.models import Category |
... | ... | @@ -34,6 +33,13 @@ from users.models import User |
34 | 33 | from topics.models import Topic, Resource |
35 | 34 | from news.models import News |
36 | 35 | |
36 | +import os | |
37 | +import zipfile | |
38 | +from io import BytesIO | |
39 | +from itertools import chain | |
40 | +from django.core import serializers | |
41 | +from django.contrib.admin.utils import NestedObjects | |
42 | + | |
37 | 43 | from amadeus.permissions import has_category_permissions, has_subject_permissions, has_subject_view_permissions, has_resource_permissions |
38 | 44 | |
39 | 45 | class HomeView(LoginRequiredMixin, ListView): |
... | ... | @@ -716,3 +722,34 @@ def most_acessed_subjects(request): |
716 | 722 | subjects = subjects[:30] |
717 | 723 | |
718 | 724 | return JsonResponse(subjects, safe=False) |
725 | + | |
726 | + | |
727 | +def backup(request): | |
728 | + #collector = NestedObjects(using="default") # database name | |
729 | + #collector.collect(list(Resource.objects.filter(visible = True))) | |
730 | + zip_subdir = "backup" | |
731 | + zip_filename = "%s.zip" % zip_subdir | |
732 | + | |
733 | + s = BytesIO() | |
734 | + | |
735 | + zf = zipfile.ZipFile(s, "w", compression = zipfile.ZIP_DEFLATED) | |
736 | + | |
737 | + resources = Resource.objects.all() | |
738 | + | |
739 | + for resource in resources: | |
740 | + if resource._my_subclass == "filelink": | |
741 | + fdir, fname = os.path.split(resource.filelink.file_content.path) | |
742 | + zip_path = os.path.join(zip_subdir, fname) | |
743 | + | |
744 | + # Add file, at correct path | |
745 | + zf.write(resource.filelink.file_content.path, zip_path) | |
746 | + | |
747 | + zf.close() | |
748 | + | |
749 | + # Grab ZIP file from in-memory, make response with correct MIME-type | |
750 | + resp = HttpResponse(s.getvalue(), content_type = "application/x-zip-compressed") | |
751 | + # ..and correct content-disposition | |
752 | + resp['Content-Disposition'] = 'attachment; filename=%s' % zip_filename | |
753 | + resp['Content-Length'] = s.tell() | |
754 | + | |
755 | + return resp | |
719 | 756 | \ No newline at end of file | ... | ... |