diff options
Diffstat (limited to 'equations/models.py')
-rw-r--r-- | equations/models.py | 119 |
1 files changed, 118 insertions, 1 deletions
diff --git a/equations/models.py b/equations/models.py index 71a8362..6791eb2 100644 --- a/equations/models.py +++ b/equations/models.py @@ -1,3 +1,120 @@ from django.db import models +from django.utils.translation import gettext_lazy as _ +import django.contrib.auth.models as auth +from django.conf import settings +from django.dispatch import dispatcher +from django.db.models import signals +from signals import update_render -# Create your models here. +class Symbol(models.Model): + name = models.CharField(_("Name"), maxlength=256) + latex = models.TextField(_("Raw LaTeX"), unique=True) + unicode = models.TextField(_("Unicode Representation"), blank=True) + renderdir = "symbolrenders/" + render = models.ImageField(_("Rendered Image"), + upload_to=renderdir, editable=False) + + class Meta: + get_latest_by = 'name' + class Admin: + ordering = ['name'] + + def __str__(self): + return self.name + def get_absolute_url(self): + return "/symbol/%s/" % self.id + def get_admin_url(self): + return "/admin/symbols/%s/" % self.id + #def save(self): + # super(Symbol, self) + # self.update_generic_variable() + def save(self): + super(Symbol, self).save() + self.render = self.renderdir + "%s.png" % self.id + super(Symbol, self).save() + +def update_generic_variable(sender, instance, signal, *args, **kwargs): + """Checks if there is a generic variable associated with this + symbol; create one if there isn't""" + for v in Variable.objects.filter(isgeneric=True): + if v.latex == instance.latex: + return + genericv = Variable(name="Generic %s" % instance.name, + latex=instance.latex, + unicode=instance.unicode, + symbol=instance, + isgeneric=True) + genericv.save() + +dispatcher.connect(update_render, signal=signals.pre_save, sender=Symbol) +dispatcher.connect(update_generic_variable, signal=signals.post_save, + sender=Symbol) + +class Variable(models.Model): + name = models.CharField(_("Name"), maxlength=256) + latex = models.TextField(_("Raw LaTeX"), unique=True) + unicode = models.TextField(_("Unicode Representation"), blank=True) + description = models.TextField(_("Description"), blank=True) + renderdir = "variablerenders/" + render = models.ImageField(_("Rendered Image"), + upload_to=renderdir, editable=False) + reference = models.URLField(_("Reference URL"), blank=True) + symbol = models.ForeignKey(Symbol, verbose_name=_("Symbol")) + isgeneric = models.BooleanField(_("Is Generic?"), default=False) + + class Meta: + get_latest_by = 'name' + class Admin: + ordering = ['name'] + + def __str__(self): + return self.name + def get_absolute_url(self): + return "/variable/%s/" % self.id + def get_admin_url(self): + return "/admin/variables/%s/" % self.id + def save(self): + super(Variable, self).save() + self.render = self.renderdir + "%s.png" % self.id + super(Variable, self).save() + +dispatcher.connect(update_render, signal=signals.pre_save, sender=Variable) + +class Equation(models.Model): + name = models.CharField(_("Name"), maxlength=256) + latex = models.TextField(_("Raw LaTeX"), unique=True) + unicode = models.TextField(_("Unicode Representation"), blank=True) + description = models.TextField(_("Description"), blank=True) + created = models.DateField(_("Created"), auto_now_add=True) + updated = models.DateField(_("Last Updated"), auto_now_add=True) + owner = models.ForeignKey(auth.User, verbose_name=_("Owner")) + variables = models.ManyToManyField(Variable, verbose_name="Variables", + editable=True) + renderdir = "equationrenders/" + render = models.ImageField(_("Rendered Image"), + upload_to=renderdir, editable=False) + reference = models.URLField(_("Reference URL"), blank=True) + + class Meta: + get_latest_by = 'created' + class Admin: + ordering = ['created'] + + def __str__(self): + return self.name + def get_absolute_url(self): + return "/equation/%s/" % self.id + def get_admin_url(self): + return "/admin/equations/%s/" % self.id + def update_variables(self): + """Updates the variables field with all Variable objects found + in the LaTeX representation.""" + pass # TODO: write + def save(self): + self.update_variables() + super(Equation, self).save() + self.render = self.renderdir + "%s.png" % self.id + super(Equation, self).save() + + +dispatcher.connect(update_render, signal=signals.post_save, sender=Equation) |