add token model

This commit is contained in:
Jake Howard 2016-09-06 08:48:48 +01:00
parent 6f49141f54
commit 81783ad025
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 53 additions and 14 deletions

View file

@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import admin_resumable.fields
import uuid
import shortuuidfield.fields
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.CreateModel(
name='FileToken',
fields=[
('id', models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, serialize=False)),
('token', shortuuidfield.fields.ShortUUIDField(max_length=22, blank=True, editable=False)),
('created', models.DateTimeField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='SharedFile',
fields=[
('id', models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False, serialize=False)),
('short_id', shortuuidfield.fields.ShortUUIDField(max_length=22, blank=True, editable=False)),
('created', models.DateTimeField(auto_now_add=True)),
('file', admin_resumable.fields.ModelAdminResumableFileField(upload_to='unused')),
('hotlink', models.BooleanField(default=False)),
('published', models.BooleanField(default=True)),
],
),
migrations.AddField(
model_name='filetoken',
name='file',
field=models.ForeignKey(to='files.SharedFile', related_name='tokens'),
),
]

View file

@ -3,28 +3,27 @@ import os
import uuid
from admin_resumable.fields import ModelAdminResumableFileField
from shortuuidfield import ShortUUIDField
def build_save_path(obj, filename):
return obj.get_save_path(filename)
from datetime import timedelta
class SharedFile(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
short_id = ShortUUIDField()
created = models.DateTimeField(auto_now_add=True)
file = ModelAdminResumableFileField(upload_to=build_save_path)
file = ModelAdminResumableFileField()
hotlink = models.BooleanField(default=False)
published = models.BooleanField(default=True)
filename = ""
def get_save_path(self, filename):
self.filename = filename
return os.path.join(str(self.id), str(self.short_id), filename)
def get_private_path(self):
return self.get_save_path(self.filename)
def get_original_filename(self):
return "_".join(self.file.name.split('_')[1:])
class FileToken(models.Model):
valid_time = timedelta(minutes=5)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
token = ShortUUIDField()
file = models.ForeignKey(SharedFile, null=False, blank=False, related_name="tokens")
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "Token for {0} created at {1}".format(self.file.file.name, self.created)