From 62a42675f80dc286ec59859f0adad2ed0b27b769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=C3=A1=C5=A1=20Havelka?= Date: Tue, 5 Nov 2024 18:53:38 +0100 Subject: [PATCH] mv SeminarModelBase a OverwriteStorage --- odevzdavatko/models.py | 8 ++--- personalni/models.py | 2 +- seminar/migrations/0100_auto_20211129_2354.py | 5 +-- seminar/models/__init__.py | 3 -- seminar/models/base.py | 23 ------------ seminar/models/tvorba.py | 14 -------- soustredeni/models.py | 2 +- treenode/models.py | 2 +- tvorba/migrations/0001_tvorba_create.py | 4 +-- tvorba/models.py | 3 +- various/models.py | 36 ++++++++++++++++--- 11 files changed, 44 insertions(+), 58 deletions(-) delete mode 100644 seminar/models/base.py delete mode 100644 seminar/models/tvorba.py diff --git a/odevzdavatko/models.py b/odevzdavatko/models.py index dea32f83..3b510fe7 100644 --- a/odevzdavatko/models.py +++ b/odevzdavatko/models.py @@ -10,13 +10,13 @@ from django.utils import timezone from django.conf import settings 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 personalni.models import Resitel @reversion.register(ignore_duplicates=True) -class Reseni(bm.SeminarModelBase): +class Reseni(SeminarModelBase): class Meta: db_table = 'seminar_reseni' @@ -88,7 +88,7 @@ class Reseni(bm.SeminarModelBase): # self.cislo_body = self.problem.cislo_reseni # super(Reseni, self).save(*args, **kwargs) -class Hodnoceni(bm.SeminarModelBase): +class Hodnoceni(SeminarModelBase): class Meta: db_table = 'seminar_hodnoceni' verbose_name = 'Hodnocení' @@ -181,7 +181,7 @@ def generate_filename(self, filename): @reversion.register(ignore_duplicates=True) -class PrilohaReseni(bm.SeminarModelBase): +class PrilohaReseni(SeminarModelBase): class Meta: db_table = 'seminar_priloha_reseni' diff --git a/personalni/models.py b/personalni/models.py index b8bcbdec..636b132e 100644 --- a/personalni/models.py +++ b/personalni/models.py @@ -11,7 +11,7 @@ from django_countries.fields import CountryField from reversion import revisions as reversion -from seminar.models.base import SeminarModelBase +from various.models import SeminarModelBase logger = logging.getLogger(__name__) diff --git a/seminar/migrations/0100_auto_20211129_2354.py b/seminar/migrations/0100_auto_20211129_2354.py index 80906d4e..87f0b824 100644 --- a/seminar/migrations/0100_auto_20211129_2354.py +++ b/seminar/migrations/0100_auto_20211129_2354.py @@ -1,7 +1,8 @@ # Generated by Django 2.2.24 on 2021-11-29 22:54 from django.db import migrations, models -import seminar.models.tvorba +import various.models +import seminar.models class Migration(migrations.Migration): @@ -14,6 +15,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='cislo', 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'), ), ] diff --git a/seminar/models/__init__.py b/seminar/models/__init__.py index e0377ff0..bd660e0c 100644 --- a/seminar/models/__init__.py +++ b/seminar/models/__init__.py @@ -1,6 +1,3 @@ -from .tvorba import * -from .base import * - from various.models import Nastaveni from personalni.models import Organizator, Resitel, Skola, Prijemce, Osoba from soustredeni.models import Soustredeni, Soustredeni_Ucastnici, Soustredeni_Organizatori, Konfera, Konfery_Ucastnici diff --git a/seminar/models/base.py b/seminar/models/base.py deleted file mode 100644 index 6ea34242..00000000 --- a/seminar/models/base.py +++ /dev/null @@ -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 - diff --git a/seminar/models/tvorba.py b/seminar/models/tvorba.py deleted file mode 100644 index c1f91196..00000000 --- a/seminar/models/tvorba.py +++ /dev/null @@ -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) diff --git a/soustredeni/models.py b/soustredeni/models.py index 8a6f6853..57e3741f 100644 --- a/soustredeni/models.py +++ b/soustredeni/models.py @@ -9,7 +9,7 @@ from django.conf import settings 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 logger = logging.getLogger(__name__) diff --git a/treenode/models.py b/treenode/models.py index e5f91c4d..54e9b896 100644 --- a/treenode/models.py +++ b/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 seminar.models import SeminarModelBase +from various.models import SeminarModelBase from personalni.models import Organizator from odevzdavatko.models import Reseni diff --git a/tvorba/migrations/0001_tvorba_create.py b/tvorba/migrations/0001_tvorba_create.py index 085cb5b4..e8a6b027 100644 --- a/tvorba/migrations/0001_tvorba_create.py +++ b/tvorba/migrations/0001_tvorba_create.py @@ -4,7 +4,7 @@ import datetime from django.db import migrations, models import django.db.models.deletion import django.utils.timezone -import seminar.models.tvorba +import various.models import tvorba.models 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í')), ('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')), - ('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')), ('rocnik', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='cisla', to='tvorba.rocnik', verbose_name='ročník')), ], diff --git a/tvorba/models.py b/tvorba/models.py index c00d0935..3a3a6b4e 100644 --- a/tvorba/models.py +++ b/tvorba/models.py @@ -31,8 +31,7 @@ from polymorphic.models import PolymorphicModel from django.core.mail import EmailMessage -from seminar.models.base import SeminarModelBase -from seminar.models.tvorba import OverwriteStorage +from various.models import SeminarModelBase, OverwriteStorage from personalni.models import Prijemce, Organizator logger = logging.getLogger(__name__) diff --git a/various/models.py b/various/models.py index 85ba4702..363c3137 100644 --- a/various/models.py +++ b/various/models.py @@ -1,11 +1,12 @@ +import os + from django.db import models +from django.core.files.storage import FileSystemStorage +from django.urls import reverse from reversion import revisions as reversion from solo.models import SingletonModel -from tvorba.models import Cislo - -from django.urls import reverse @reversion.register(ignore_duplicates=True) class Nastaveni(SingletonModel): @@ -14,10 +15,10 @@ class Nastaveni(SingletonModel): db_table = 'seminar_nastaveni' 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) - 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, ) @@ -37,3 +38,28 @@ class Nastaveni(SingletonModel): def verejne(self): 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, ))