add token model
This commit is contained in:
parent
6f49141f54
commit
81783ad025
2 changed files with 53 additions and 14 deletions
40
project/files/migrations/0001_initial.py
Normal file
40
project/files/migrations/0001_initial.py
Normal 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'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -3,28 +3,27 @@ import os
|
||||||
import uuid
|
import uuid
|
||||||
from admin_resumable.fields import ModelAdminResumableFileField
|
from admin_resumable.fields import ModelAdminResumableFileField
|
||||||
from shortuuidfield import ShortUUIDField
|
from shortuuidfield import ShortUUIDField
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
def build_save_path(obj, filename):
|
|
||||||
return obj.get_save_path(filename)
|
|
||||||
|
|
||||||
|
|
||||||
class SharedFile(models.Model):
|
class SharedFile(models.Model):
|
||||||
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||||
short_id = ShortUUIDField()
|
short_id = ShortUUIDField()
|
||||||
created = models.DateTimeField(auto_now_add=True)
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
file = ModelAdminResumableFileField(upload_to=build_save_path)
|
file = ModelAdminResumableFileField()
|
||||||
hotlink = models.BooleanField(default=False)
|
hotlink = models.BooleanField(default=False)
|
||||||
published = models.BooleanField(default=True)
|
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):
|
def get_original_filename(self):
|
||||||
return "_".join(self.file.name.split('_')[1:])
|
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)
|
||||||
|
|
Reference in a new issue