Skip to content

Commit

Permalink
* Add document exporting to the application.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgroff committed Jan 16, 2018
1 parent 9f63fe4 commit c318cf7
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 4 deletions.
21 changes: 21 additions & 0 deletions django_kala/auth/migrations/0002_auto_20180116_0036.py
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'),
),
]
2 changes: 1 addition & 1 deletion django_kala/django_kala/platforms/aws/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_document_url(self, document):
}
)

def upload_project_export(self, export_path):
def upload_export(self, export_path):
key = 'exports/{0}'.format(uuid4())

s3 = boto3.client('s3')
Expand Down
2 changes: 1 addition & 1 deletion django_kala/django_kala/platforms/local/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class DocumentHandler():
def get_document_url(self, document):
return '{0}/media/documents/{1}'.format('http://localhost', document.file.name)

def upload_project_export(self, export_path):
def upload_export(self, export_path):
shutil.move(
export_path,
'{0}/{1}'.format(settings.EXPORT_DIRECTORY, export_path.split('/')[-1])
Expand Down
27 changes: 27 additions & 0 deletions django_kala/documents/migrations/0002_auto_20180116_0036.py
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),
),
]
21 changes: 21 additions & 0 deletions django_kala/projects/migrations/0003_export_name.py
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,
),
]
3 changes: 2 additions & 1 deletion django_kala/projects/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ def __str__(self):


class Export(models.Model):
details = JSONField(default={})
name = models.CharField(max_length=255)
user = models.ForeignKey(User, on_delete=models.CASCADE)
key = models.CharField(max_length=255)
details = JSONField(default={})
64 changes: 64 additions & 0 deletions django_kala/projects/tasks/export_document.py
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)
3 changes: 2 additions & 1 deletion django_kala/projects/tasks/export_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ def run(self, *args, **kwargs):
)

# Upload the document somewhere.
export_details = manager.upload_project_export(export_path)
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)
Expand Down

0 comments on commit c318cf7

Please sign in to comment.