-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add document exporting to the application.
- Loading branch information
Showing
8 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.7 on 2018-01-16 00:36 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('kala_auth', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='permissions', | ||
name='permission', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING, to='auth.Permission'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
django_kala/documents/migrations/0002_auto_20180116_0036.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.7 on 2018-01-16 00:36 | ||
from __future__ import unicode_literals | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('documents', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='document', | ||
name='category', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='projects.Category'), | ||
), | ||
migrations.AlterField( | ||
model_name='documentversion', | ||
name='user', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.7 on 2018-01-16 00:36 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('projects', '0002_export'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='export', | ||
name='name', | ||
field=models.CharField(default='foo', max_length=255), | ||
preserve_default=False, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
from django.utils.crypto import get_random_string | ||
|
||
from documents.models import Document | ||
from projects.models import Export | ||
|
||
import celery | ||
import os | ||
import requests | ||
import tempfile | ||
import shutil | ||
|
||
User = get_user_model() | ||
|
||
|
||
class ExportDocumentTask(celery.Task): | ||
|
||
def run(self, *args, **kwargs): | ||
document = Document.objects.get(pk=args[0]) | ||
user = User.objects.get(pk=args[1]) | ||
manager = settings.PLATFORM_MANAGER() | ||
|
||
# If the user does not have change or create priviliages, fail. | ||
if not document.has_change(user) and not document.has_create(user): | ||
# TODO: this should be logged | ||
return False | ||
|
||
# Create a temp dir and documents directory inside. | ||
path = tempfile.mkdtemp() | ||
os.mkdir(path + '/documents') | ||
|
||
# Save the path on the task for later cleanup | ||
self.path = path | ||
|
||
# Download all the documents into the documents dir. | ||
for version in document.documentversion_set.all(): | ||
response = requests.get(manager.get_document_url(version), stream=True) | ||
with open('{0}/{1}/{2}'.format(path, 'documents', version.file.name), 'wb') as f: | ||
for chunk in response.iter_content(chunk_size=1024): | ||
if chunk: # filter out keep-alive new chunks | ||
f.write(chunk) | ||
|
||
# Make a zip file from all the documents | ||
export_path = shutil.make_archive( | ||
'{0}/{1}'.format(path, document.name), | ||
'zip', | ||
'{0}/{1}'.format(path, '/documents') | ||
) | ||
|
||
# Upload the document somewhere. | ||
export_details = manager.upload_export(export_path) | ||
export = Export.objects.create( | ||
name=export_path.split('/')[-1], | ||
user=user, | ||
details=export_details, | ||
key=get_random_string(length=255) | ||
) | ||
|
||
# Cleanup the temp files. | ||
self.cleanup() | ||
|
||
def cleanup(self): | ||
shutil.rmtree(self.path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters