2020-12-15 21:32:20 +00:00
|
|
|
from django.db import models
|
2020-12-16 17:36:29 +00:00
|
|
|
from django.utils.text import slugify
|
2020-12-15 21:32:20 +00:00
|
|
|
|
2020-12-16 14:26:15 +00:00
|
|
|
class Branch(models.Model):
|
|
|
|
name = models.CharField(max_length=255,blank=True)
|
2020-12-16 17:36:29 +00:00
|
|
|
acronym = models.CharField(max_length=20)
|
|
|
|
description = models.TextField(blank=True)
|
|
|
|
logo = models.ImageField(upload_to="logo",blank=True)
|
2020-12-16 14:26:15 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name+" ["+self.acronym+"]"
|
|
|
|
|
2020-12-15 21:32:20 +00:00
|
|
|
# Create your models here.
|
2020-12-16 07:29:08 +00:00
|
|
|
class CategoryManager(models.Manager):
|
|
|
|
def create_or_new(self, name):
|
|
|
|
name = name.strip()
|
|
|
|
qs = self.get_queryset().filter(name__iexact=name)
|
|
|
|
if qs.exists():
|
|
|
|
return qs.first(), False
|
|
|
|
return Category.objects.create(name=name), True
|
|
|
|
|
|
|
|
def comma_to_qs(self, categorys_str):
|
|
|
|
final_ids = []
|
|
|
|
for category in categories_str.split(','):
|
|
|
|
obj, created = self.create_or_new(category)
|
|
|
|
final_ids.append(obj.id)
|
|
|
|
qs = self.get_queryset().filter(id__in=final_ids).distinct()
|
|
|
|
return qs
|
2020-12-15 21:32:20 +00:00
|
|
|
|
|
|
|
class Category(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
2020-12-16 17:36:29 +00:00
|
|
|
slug = models.SlugField(max_length=255)
|
2020-12-15 21:32:20 +00:00
|
|
|
description = models.TextField(blank=True)
|
|
|
|
|
2020-12-16 07:29:08 +00:00
|
|
|
objects = CategoryManager()
|
|
|
|
|
2020-12-15 21:32:20 +00:00
|
|
|
class Meta:
|
2020-12-16 08:51:48 +00:00
|
|
|
ordering = [ 'name' ]
|
2020-12-15 21:32:20 +00:00
|
|
|
verbose_name_plural = "Categories"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2020-12-16 17:36:29 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
self.slug = slugify(self.name)
|
|
|
|
super().save(*args,**kwargs)
|
|
|
|
|