Browse Source

mv SeminarModelBase a OverwriteStorage

pull/65/head
Jonas Havelka 1 week ago
parent
commit
62a42675f8
  1. 8
      odevzdavatko/models.py
  2. 2
      personalni/models.py
  3. 5
      seminar/migrations/0100_auto_20211129_2354.py
  4. 3
      seminar/models/__init__.py
  5. 23
      seminar/models/base.py
  6. 14
      seminar/models/tvorba.py
  7. 2
      soustredeni/models.py
  8. 2
      treenode/models.py
  9. 4
      tvorba/migrations/0001_tvorba_create.py
  10. 3
      tvorba/models.py
  11. 36
      various/models.py

8
odevzdavatko/models.py

@ -10,13 +10,13 @@ from django.utils import timezone
from django.conf import settings from django.conf import settings
from tvorba.models import Problem, Deadline, Cislo, Uloha, aux_generate_filename from tvorba.models import Problem, Deadline, Cislo, Uloha, aux_generate_filename
from seminar.models import base as bm from various.models import SeminarModelBase
from odevzdavatko.utils import vzorecek_na_prepocet, inverze_vzorecku_na_prepocet from odevzdavatko.utils import vzorecek_na_prepocet, inverze_vzorecku_na_prepocet
from personalni.models import Resitel from personalni.models import Resitel
@reversion.register(ignore_duplicates=True) @reversion.register(ignore_duplicates=True)
class Reseni(bm.SeminarModelBase): class Reseni(SeminarModelBase):
class Meta: class Meta:
db_table = 'seminar_reseni' db_table = 'seminar_reseni'
@ -88,7 +88,7 @@ class Reseni(bm.SeminarModelBase):
# self.cislo_body = self.problem.cislo_reseni # self.cislo_body = self.problem.cislo_reseni
# super(Reseni, self).save(*args, **kwargs) # super(Reseni, self).save(*args, **kwargs)
class Hodnoceni(bm.SeminarModelBase): class Hodnoceni(SeminarModelBase):
class Meta: class Meta:
db_table = 'seminar_hodnoceni' db_table = 'seminar_hodnoceni'
verbose_name = 'Hodnocení' verbose_name = 'Hodnocení'
@ -181,7 +181,7 @@ def generate_filename(self, filename):
@reversion.register(ignore_duplicates=True) @reversion.register(ignore_duplicates=True)
class PrilohaReseni(bm.SeminarModelBase): class PrilohaReseni(SeminarModelBase):
class Meta: class Meta:
db_table = 'seminar_priloha_reseni' db_table = 'seminar_priloha_reseni'

2
personalni/models.py

@ -11,7 +11,7 @@ from django_countries.fields import CountryField
from reversion import revisions as reversion from reversion import revisions as reversion
from seminar.models.base import SeminarModelBase from various.models import SeminarModelBase
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

5
seminar/migrations/0100_auto_20211129_2354.py

@ -1,7 +1,8 @@
# Generated by Django 2.2.24 on 2021-11-29 22:54 # Generated by Django 2.2.24 on 2021-11-29 22:54
from django.db import migrations, models from django.db import migrations, models
import seminar.models.tvorba import various.models
import seminar.models
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -14,6 +15,6 @@ class Migration(migrations.Migration):
migrations.AlterField( migrations.AlterField(
model_name='cislo', model_name='cislo',
name='pdf', name='pdf',
field=models.FileField(blank=True, help_text='PDF čísla, které si mohou řešitelé stáhnout', null=True, storage=seminar.models.tvorba.OverwriteStorage(), upload_to=seminar.models.tvorba.cislo_pdf_filename, verbose_name='pdf'), field=models.FileField(blank=True, help_text='PDF čísla, které si mohou řešitelé stáhnout', null=True, storage=various.models.OverwriteStorage(), upload_to=seminar.models.cislo_pdf_filename, verbose_name='pdf'),
), ),
] ]

3
seminar/models/__init__.py

@ -1,6 +1,3 @@
from .tvorba import *
from .base import *
from various.models import Nastaveni from various.models import Nastaveni
from personalni.models import Organizator, Resitel, Skola, Prijemce, Osoba from personalni.models import Organizator, Resitel, Skola, Prijemce, Osoba
from soustredeni.models import Soustredeni, Soustredeni_Ucastnici, Soustredeni_Organizatori, Konfera, Konfery_Ucastnici from soustredeni.models import Soustredeni, Soustredeni_Ucastnici, Soustredeni_Organizatori, Konfera, Konfery_Ucastnici

23
seminar/models/base.py

@ -1,23 +0,0 @@
from django.urls import reverse
from django.db import models
class SeminarModelBase(models.Model):
class Meta:
abstract = True
def verejne(self):
return False
# def get_absolute_url(self):
# return "https://" + str(get_current_site(None)) + self.verejne_url()
def admin_url(self):
app_name = self._meta.app_label
model_name = self._meta.model_name
return reverse('admin:{}_{}_change'.format(app_name, model_name), args=(self.id, ))
# def verejne_url(self):
# return None

14
seminar/models/tvorba.py

@ -1,14 +0,0 @@
import os
import logging
from django.core.files.storage import FileSystemStorage
logger = logging.getLogger(__name__)
class OverwriteStorage(FileSystemStorage):
""" Varianta FileSystemStorage, která v případě, že soubor cílového
jména již existuje, ho smaže a místo něj uloží soubor nový"""
def get_available_name(self,name, max_length=None):
if self.exists(name):
os.remove(os.path.join(self.location,name))
return super().get_available_name(name,max_length)

2
soustredeni/models.py

@ -9,7 +9,7 @@ from django.conf import settings
from personalni.models import Resitel, Organizator from personalni.models import Resitel, Organizator
from seminar.models.base import SeminarModelBase from various.models import SeminarModelBase
from tvorba.models import Rocnik, Problem, aux_generate_filename from tvorba.models import Rocnik, Problem, aux_generate_filename
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

2
treenode/models.py

@ -8,7 +8,7 @@ from unidecode import unidecode # Používám pro získání ID odkazu (ještě
from polymorphic.models import PolymorphicModel from polymorphic.models import PolymorphicModel
from seminar.models import SeminarModelBase from various.models import SeminarModelBase
from personalni.models import Organizator from personalni.models import Organizator
from odevzdavatko.models import Reseni from odevzdavatko.models import Reseni

4
tvorba/migrations/0001_tvorba_create.py

@ -4,7 +4,7 @@ import datetime
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
import django.utils.timezone import django.utils.timezone
import seminar.models.tvorba import various.models
import tvorba.models import tvorba.models
import taggit.managers import taggit.managers
@ -35,7 +35,7 @@ class Migration(migrations.Migration):
('datum_vydani', models.DateField(blank=True, help_text='Datum vydání finální verze', null=True, verbose_name='datum vydání')), ('datum_vydani', models.DateField(blank=True, help_text='Datum vydání finální verze', null=True, verbose_name='datum vydání')),
('verejne_db', models.BooleanField(db_column='verejne', default=False, verbose_name='číslo zveřejněno')), ('verejne_db', models.BooleanField(db_column='verejne', default=False, verbose_name='číslo zveřejněno')),
('poznamka', models.TextField(blank=True, help_text='Neveřejná poznámka k číslu (plain text)', verbose_name='neveřejná poznámka')), ('poznamka', models.TextField(blank=True, help_text='Neveřejná poznámka k číslu (plain text)', verbose_name='neveřejná poznámka')),
('pdf', models.FileField(blank=True, help_text='PDF čísla, které si mohou řešitelé stáhnout', null=True, storage=seminar.models.tvorba.OverwriteStorage(), upload_to=tvorba.models.cislo_pdf_filename, verbose_name='pdf')), ('pdf', models.FileField(blank=True, help_text='PDF čísla, které si mohou řešitelé stáhnout', null=True, storage=various.models.OverwriteStorage(), upload_to=tvorba.models.cislo_pdf_filename, verbose_name='pdf')),
('titulka_nahled', models.ImageField(blank=True, help_text='Obrázek titulní strany, generuje se automaticky', null=True, upload_to=tvorba.models.cislo_png_filename, verbose_name='Obrázek titulní strany')), ('titulka_nahled', models.ImageField(blank=True, help_text='Obrázek titulní strany, generuje se automaticky', null=True, upload_to=tvorba.models.cislo_png_filename, verbose_name='Obrázek titulní strany')),
('rocnik', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='cisla', to='tvorba.rocnik', verbose_name='ročník')), ('rocnik', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='cisla', to='tvorba.rocnik', verbose_name='ročník')),
], ],

3
tvorba/models.py

@ -31,8 +31,7 @@ from polymorphic.models import PolymorphicModel
from django.core.mail import EmailMessage from django.core.mail import EmailMessage
from seminar.models.base import SeminarModelBase from various.models import SeminarModelBase, OverwriteStorage
from seminar.models.tvorba import OverwriteStorage
from personalni.models import Prijemce, Organizator from personalni.models import Prijemce, Organizator
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

36
various/models.py

@ -1,11 +1,12 @@
import os
from django.db import models from django.db import models
from django.core.files.storage import FileSystemStorage
from django.urls import reverse
from reversion import revisions as reversion from reversion import revisions as reversion
from solo.models import SingletonModel from solo.models import SingletonModel
from tvorba.models import Cislo
from django.urls import reverse
@reversion.register(ignore_duplicates=True) @reversion.register(ignore_duplicates=True)
class Nastaveni(SingletonModel): class Nastaveni(SingletonModel):
@ -14,10 +15,10 @@ class Nastaveni(SingletonModel):
db_table = 'seminar_nastaveni' db_table = 'seminar_nastaveni'
verbose_name = 'Nastavení semináře' verbose_name = 'Nastavení semináře'
# aktualni_rocnik = models.ForeignKey(Rocnik, verbose_name='aktuální ročník', # aktualni_rocnik = models.ForeignKey("tvorba.Rocnik", verbose_name='aktuální ročník',
# null=False, on_delete=models.PROTECT) # null=False, on_delete=models.PROTECT)
aktualni_cislo = models.ForeignKey(Cislo, verbose_name='Aktuální číslo', aktualni_cislo = models.ForeignKey("tvorba.Cislo", verbose_name='Aktuální číslo',
null=True, blank=False, on_delete=models.PROTECT, null=True, blank=False, on_delete=models.PROTECT,
) )
@ -37,3 +38,28 @@ class Nastaveni(SingletonModel):
def verejne(self): def verejne(self):
return False return False
class OverwriteStorage(FileSystemStorage):
""" Varianta FileSystemStorage, která v případě, že soubor cílového
jména již existuje, ho smaže a místo něj uloží soubor nový"""
def get_available_name(self,name, max_length=None):
if self.exists(name):
os.remove(os.path.join(self.location,name))
return super().get_available_name(name,max_length)
class SeminarModelBase(models.Model):
class Meta:
abstract = True
def verejne(self):
return False
# def get_absolute_url(self):
# return "https://" + str(get_current_site(None)) + self.verejne_url()
def admin_url(self):
app_name = self._meta.app_label
model_name = self._meta.model_name
return reverse('admin:{}_{}_change'.format(app_name, model_name), args=(self.id, ))

Loading…
Cancel
Save