|
1 | 1 | # -*- coding: utf-8 -*-
|
| 2 | +import datetime |
| 3 | +import slugify as unicodeslugify |
| 4 | + |
2 | 5 | from django.db import models, transaction
|
| 6 | +from django.conf import settings |
| 7 | +from django.contrib.auth import get_user_model |
| 8 | +from django.utils.translation import ugettext_lazy as _ |
| 9 | + |
3 | 10 |
|
4 | 11 | from asylum.models import AsylumModel
|
5 | 12 |
|
6 |
| -# Create your models here. |
| 13 | + |
| 14 | + |
| 15 | +def get_sentinel_user(): |
| 16 | + """Gets a "sentinel" user ("deleted") and for assigning as uploader""" |
| 17 | + return get_user_model().objects.get_or_create(username='deleted')[0] |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +def datestamped_and_normalized(instance, filename): |
| 22 | + """Normalized filename and places in datestamped path""" |
| 23 | + file_parts = filename.split('.') |
| 24 | + if len(file_parts) > 1: |
| 25 | + name = '.'.join(file_parts[:-1]) |
| 26 | + ext = '.' + file_parts[-1] |
| 27 | + else: |
| 28 | + ext = '' |
| 29 | + name = filename |
| 30 | + filename_normalized = unicodeslugify.slugify( |
| 31 | + name, only_ascii=True, lower=True, |
| 32 | + spaces=False, space_replacement='_' |
| 33 | + ) + ext |
| 34 | + return datetime.datetime.now().strftime("ndaparser/%Y/%m/%d/{}").format(filename_normalized) |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +class UploadedTransaction(AsylumModel): |
| 39 | + """Track uploaded transaction files""" |
| 40 | + user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET(get_sentinel_user)) |
| 41 | + file = models.FileField(upload_to=datestamped_and_normalized) |
| 42 | + stamp = models.DateTimeField(auto_now_add=True, editable=False) |
| 43 | + last_transaction = models.DateField() |
| 44 | + |
| 45 | + class Meta: |
| 46 | + verbose_name = _('Uploaded transaction') |
| 47 | + verbose_name_plural = _('Uploaded transaction') |
| 48 | + ordering = [ '-stamp' ] |
0 commit comments