zmiany mariusza
This commit is contained in:
9
backend/formulas/models/__init__.py
Normal file
9
backend/formulas/models/__init__.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from .formula import Formula
|
||||
from .symbol import Symbol
|
||||
from .category import FormulaCategory
|
||||
|
||||
__all__ = [
|
||||
"Formula",
|
||||
"Symbol",
|
||||
"FormulaCategory",
|
||||
]
|
||||
BIN
backend/formulas/models/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
backend/formulas/models/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/formulas/models/__pycache__/category.cpython-312.pyc
Normal file
BIN
backend/formulas/models/__pycache__/category.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/formulas/models/__pycache__/favorite.cpython-312.pyc
Normal file
BIN
backend/formulas/models/__pycache__/favorite.cpython-312.pyc
Normal file
Binary file not shown.
BIN
backend/formulas/models/__pycache__/formula.cpython-312.pyc
Normal file
BIN
backend/formulas/models/__pycache__/formula.cpython-312.pyc
Normal file
Binary file not shown.
Binary file not shown.
BIN
backend/formulas/models/__pycache__/symbol.cpython-312.pyc
Normal file
BIN
backend/formulas/models/__pycache__/symbol.cpython-312.pyc
Normal file
Binary file not shown.
12
backend/formulas/models/category.py
Normal file
12
backend/formulas/models/category.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from django.db import models
|
||||
|
||||
class FormulaCategory(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
prefix = models.PositiveIntegerField(unique=True) # cyfra `x` w kodzie xyz
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Kategoria wzoru"
|
||||
verbose_name_plural = "Kategorie wzorów"
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.prefix} - {self.name}"
|
||||
17
backend/formulas/models/favorite.py
Normal file
17
backend/formulas/models/favorite.py
Normal file
@@ -0,0 +1,17 @@
|
||||
# backend/formulas/models/favorite_formula.py
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
from formulas.models.formula import Formula
|
||||
|
||||
class FavoriteFormula(models.Model):
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||||
formula = models.ForeignKey(Formula, on_delete=models.CASCADE)
|
||||
added_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
unique_together = ('user', 'formula')
|
||||
verbose_name = "Ulubiony wzór"
|
||||
verbose_name_plural = "Ulubione wzory"
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.user.username} ❤️ {self.formula.code}"
|
||||
38
backend/formulas/models/formula.py
Normal file
38
backend/formulas/models/formula.py
Normal file
@@ -0,0 +1,38 @@
|
||||
from django.db import models
|
||||
from .category import FormulaCategory
|
||||
from users.models import User
|
||||
from django.utils import timezone
|
||||
from .symbol import Symbol
|
||||
|
||||
class Formula(models.Model):
|
||||
formula_id = models.CharField(max_length=100)
|
||||
title = models.CharField(max_length=255)
|
||||
description = models.TextField(blank=True)
|
||||
meta = models.TextField(blank=True)
|
||||
category = models.ForeignKey(FormulaCategory, on_delete=models.PROTECT)
|
||||
tags = models.JSONField(default=list)
|
||||
latex = models.TextField()
|
||||
raw_latex = models.TextField()
|
||||
symbols = models.ManyToManyField(Symbol, related_name='formulas')
|
||||
calculator = models.CharField(max_length=100)
|
||||
revision = models.PositiveIntegerField(default=1)
|
||||
code = models.CharField(max_length=10, unique=True)
|
||||
created_by = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
|
||||
created_at = models.DateTimeField(default=timezone.now)
|
||||
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.code:
|
||||
base_prefix = f"{self.category.prefix:02d}"
|
||||
existing_codes = Formula.objects.filter(category=self.category).values_list('code', flat=True)
|
||||
index = 1
|
||||
while True:
|
||||
proposed_code = f"{base_prefix}{index:02d}{self.revision:02d}"
|
||||
if proposed_code not in existing_codes:
|
||||
self.code = proposed_code
|
||||
break
|
||||
index += 1
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.title} ({self.code})"
|
||||
25
backend/formulas/models/symbol.py
Normal file
25
backend/formulas/models/symbol.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from django.db import models
|
||||
from .category import FormulaCategory
|
||||
|
||||
class Symbol(models.Model):
|
||||
symbol_id = models.CharField(max_length=10) # np. F
|
||||
name = models.CharField(max_length=255)
|
||||
unit = models.CharField(max_length=50)
|
||||
description = models.TextField(blank=True)
|
||||
category = models.ForeignKey(FormulaCategory, on_delete=models.PROTECT)
|
||||
tags = models.JSONField(default=list, blank=True)
|
||||
revision = models.PositiveIntegerField(default=1)
|
||||
code = models.CharField(max_length=10, unique=True, editable=False)
|
||||
meta = models.TextField(blank=True) # np. meta-opis dla SEO / edytora
|
||||
|
||||
|
||||
#Zaspis zmiennej z code
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.code:
|
||||
count = Symbol.objects.filter(category=self.category).count() + 1
|
||||
self.code = f"{self.category.prefix:02d}{count:02d}{self.revision:02d}"
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
#Definiowanie czym jest "self"
|
||||
def __str__(self):
|
||||
return f"{self.name} ({self.symbol_id}) [{self.code}]"
|
||||
Reference in New Issue
Block a user