"Fossies" - the Fresh Open Source Software Archive

Member "taiga-back-5.0.6/taiga/projects/attachments/migrations/0002_add_size_and_name_fields.py" (15 Jan 2020, 1119 Bytes) of package /linux/www/taiga-back-5.0.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "0002_add_size_and_name_fields.py" see the Fossies "Dox" file reference documentation.

    1 # -*- coding: utf-8 -*-
    2 from __future__ import unicode_literals
    3 
    4 import os.path as path
    5 from django.db import models, migrations
    6 
    7 
    8 def parse_filenames_and_sizes(apps, schema_editor):
    9     Attachment = apps.get_model("attachments", "Attachment")
   10 
   11     for item in Attachment.objects.all():
   12         try:
   13             item.size = item.attached_file.size
   14         except Exception as e:
   15             item.size = 0
   16 
   17         item.name = path.basename(item.attached_file.name)
   18         item.save()
   19 
   20 
   21 class Migration(migrations.Migration):
   22 
   23     dependencies = [
   24         ('attachments', '0001_initial'),
   25     ]
   26 
   27     operations = [
   28         migrations.AddField(
   29             model_name='attachment',
   30             name='name',
   31             field=models.CharField(default='', blank=True, max_length=500),
   32             preserve_default=True,
   33         ),
   34         migrations.AddField(
   35             model_name='attachment',
   36             name='size',
   37             field=models.IntegerField(editable=False, null=True, blank=True, default=None),
   38             preserve_default=True,
   39         ),
   40         migrations.RunPython(parse_filenames_and_sizes)
   41     ]