Skip to content

Commit

Permalink
Added data_upload module
Browse files Browse the repository at this point in the history
  • Loading branch information
kleisb committed May 25, 2016
1 parent 67016d6 commit f968b80
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 0 deletions.
Empty file added data_upload/__init__.py
Empty file.
80 changes: 80 additions & 0 deletions data_upload/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import data_upload.models
from django.conf import settings
import google_helpers.cloud_file_storage


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='ControlledFileField',
fields=[
('id', models.AutoField(serialize=False, primary_key=True)),
('name', models.CharField(max_length=120)),
],
options={
},
bases=(models.Model,),
),
migrations.CreateModel(
name='FieldDataType',
fields=[
('id', models.AutoField(serialize=False, primary_key=True)),
('name', models.CharField(max_length=120)),
('dbString', models.CharField(max_length=64)),
],
options={
},
bases=(models.Model,),
),
migrations.CreateModel(
name='FileDataType',
fields=[
('id', models.AutoField(serialize=False, primary_key=True)),
('name', models.CharField(max_length=200)),
('fields', models.ManyToManyField(to='data_upload.ControlledFileField')),
],
options={
},
bases=(models.Model,),
),
migrations.CreateModel(
name='UserUpload',
fields=[
('id', models.AutoField(serialize=False, primary_key=True)),
('status', models.CharField(default=b'Pending', max_length=50)),
('jobURL', models.CharField(max_length=250, null=True)),
('key', models.CharField(default=data_upload.models.generate_upload_key, max_length=64)),
('owner', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
],
options={
},
bases=(models.Model,),
),
migrations.CreateModel(
name='UserUploadedFile',
fields=[
('id', models.AutoField(serialize=False, primary_key=True)),
('bucket', models.CharField(max_length=155, null=True)),
('file', models.FileField(upload_to=data_upload.models.get_user_bucket)),
('upload', models.ForeignKey(to='data_upload.UserUpload')),
],
options={
},
bases=(models.Model,),
),
migrations.AddField(
model_name='controlledfilefield',
name='type',
field=models.ForeignKey(to='data_upload.FieldDataType'),
preserve_default=True,
),
]
Empty file.
67 changes: 67 additions & 0 deletions data_upload/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from django.db import models
from django.contrib import admin
from django.contrib.auth.models import User
from google_helpers import cloud_file_storage
from django.core.files.storage import FileSystemStorage
from django.core.exceptions import ValidationError
from django.conf import settings

import random
import string

storage_system = FileSystemStorage()
if settings.USE_CLOUD_STORAGE is not 'False':
storage_system = cloud_file_storage.CloudFileStorage()

def generate_upload_key():
return ''.join(random.SystemRandom().choice(string.hexdigits) for _ in range(16))

def get_user_bucket(instance, filename):
bucket = ''
if settings.USE_CLOUD_STORAGE is not 'False':
if not instance.bucket:
bucket = settings.GCLOUD_BUCKET
else:
bucket = instance.bucket
bucket += '/'

return bucket + filename

class UserUpload(models.Model):
id = models.AutoField(primary_key=True)
owner = models.ForeignKey(User, null=False, blank=False)
status = models.CharField(max_length=50,default="Pending")
jobURL = models.CharField(max_length=250,null=True)
key = models.CharField(max_length=64,default=generate_upload_key)

class UserUploadedFile(models.Model):
id = models.AutoField(primary_key=True)
upload = models.ForeignKey(UserUpload)
bucket = models.CharField(max_length=155, null=True)
file = models.FileField(storage=storage_system, upload_to=get_user_bucket)

class FieldDataType(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=120)
dbString = models.CharField(max_length=64)

def __str__(self):
return self.name

class ControlledFileField(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=120)
type = models.ForeignKey(FieldDataType)

class FileDataType(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=200)
fields = models.ManyToManyField(ControlledFileField)

@admin.register(ControlledFileField)
class ControlledFileFieldAdmin(admin.ModelAdmin):
list_display = ('name','type')

@admin.register(FieldDataType)
class FieldDataTypeAdmin(admin.ModelAdmin):
list_display = ('name',)
43 changes: 43 additions & 0 deletions data_upload/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Data Upload Service
Uploads user or other data to either the cloud or the local environment
depending on the environment.
"""

from google_helpers import storage_service
from googleapiclient import http
from django.conf import settings

class CloudUploadService():

def __init__(self):
self.storage = storage_service.get_storage_resource()

def upload(self, file, filename=None):
media = http.MediaInMemoryUpload(file)
self.storage.objects().insert(
bucket=settings.GCLOUD_BUCKET,
name=filename,
media_body=media
).execute()

class LocalUploadService():

def __init__(self):
pass

def upload(self, file, filename=None):
pass


class UploadService():

def __init__(self):
if settings.GCLOUD_BUCKET is not None:
self.service = CloudUploadService()
else:
self.service = LocalUploadService()

def upload(self, file, filename=None):
return self.service.upload
6 changes: 6 additions & 0 deletions data_upload/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.conf.urls import patterns, include, url
from django.contrib import admin

admin.autodiscover()

urlpatterns = []

0 comments on commit f968b80

Please sign in to comment.