From 1c4284366f0f82240e57a7a45fd9c4bd63941390 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Thu, 13 May 2021 07:52:47 +0300 Subject: Bring back contenttypes --- servo/forms/product.py | 2 +- servo/models/common.py | 38 +++++++++++++++++++++++--------------- servo/models/device.py | 2 +- servo/views/order.py | 8 +++----- servo/views/product.py | 2 +- 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/servo/forms/product.py b/servo/forms/product.py index 480240d..7e0f52e 100644 --- a/servo/forms/product.py +++ b/servo/forms/product.py @@ -31,7 +31,7 @@ class ProductSearchForm(forms.Form): def __init__(self, *args, **kwargs): super(ProductSearchForm, self).__init__(*args, **kwargs) - tags = TaggedItem.objects.filter(ref="product").distinct("tag") + tags = TaggedItem.objects.filter(content_type__model="product").distinct("tag") self.fields['tag'].queryset = tags diff --git a/servo/models/common.py b/servo/models/common.py index 7339f4f..faad673 100644 --- a/servo/models/common.py +++ b/servo/models/common.py @@ -16,6 +16,8 @@ from mptt.models import MPTTModel, TreeForeignKey from django.utils.translation import ugettext_lazy as _ from django.dispatch import receiver +from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes.fields import GenericForeignKey from django.db.models.signals import post_save from django.core.cache import cache @@ -62,7 +64,7 @@ class CsvTable(object): def has_body(self): return self.body != '' - def __unicode__(self): + def __str__(self): self.table = self.header + "\n" + self.body return self.table @@ -75,8 +77,9 @@ class TaggedItem(models.Model): """ A generic tagged item """ - ref = models.CharField(max_length=32) - ref_id = models.IntegerField() + content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) + object_id = models.PositiveIntegerField() + content_object = GenericForeignKey('content_type', 'object_id') tag = models.CharField(max_length=128) slug = models.SlugField() @@ -86,17 +89,21 @@ class TaggedItem(models.Model): self.slug = slugify(self.tag) super(TaggedItem, self).save(*args, **kwargs) - def __unicode__(self): + def __str__(self): return self.tag class Meta: app_label = "servo" - unique_together = ("ref", "ref_id", "tag",) class FlaggedItem(models.Model): - ref = models.CharField(max_length=32) - ref_id = models.IntegerField() + """ + A flagged item. This could also be a tag, but how? + """ + content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) + object_id = models.PositiveIntegerField() + content_object = GenericForeignKey('content_type', 'object_id') + flagged_by = models.ForeignKey( settings.AUTH_USER_MODEL, null=True, @@ -108,8 +115,9 @@ class Event(models.Model): """ Something that happens """ - ref = models.CharField(max_length=32) - ref_id = models.IntegerField() + content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) + object_id = models.PositiveIntegerField() + content_object = GenericForeignKey('content_type', 'object_id') description = models.CharField(max_length=255) triggered_by = models.ForeignKey( @@ -147,12 +155,12 @@ class Event(models.Model): def get_class(self): return "disabled" if self.handled_at else "" - def __unicode__(self): + def __str__(self): return self.description class Meta: - ordering = ('priority', '-id',) app_label = "servo" + ordering = ('priority', '-id',) class GsxAccount(models.Model): @@ -281,7 +289,7 @@ class GsxAccount(models.Model): def get_admin_url(self): return reverse('admin-edit_gsx_account', args=[self.pk]) - def __unicode__(self): + def __str__(self): return u"%s (%s)" % (self.title, self.get_environment_display()) class Meta: @@ -352,7 +360,7 @@ class Tag(MPTTModel): def get_admin_url(self): return reverse('admin-edit_tag', args=[self.type, self.pk]) - def __unicode__(self): + def __str__(self): return self.title objects = TreeManager() @@ -671,7 +679,7 @@ class Property(models.Model): ) value = models.TextField(blank=True, default='', verbose_name=_('value')) - def __unicode__(self): + def __str__(self): return self.title def get_admin_url(self): @@ -787,7 +795,7 @@ class Attachment(models.Model): return super(Attachment, self).save(*args, **kwargs) - def __unicode__(self): + def __str__(self): return os.path.basename(self.content.name) def __str__(self): diff --git a/servo/models/device.py b/servo/models/device.py index 148684d..6d20173 100644 --- a/servo/models/device.py +++ b/servo/models/device.py @@ -206,7 +206,7 @@ class Device(models.Model): @property def tag_choices(self): - return TaggedItem.objects.filter(ref="device").distinct("tag") + return TaggedItem.objects.filter(content_type__model="device").distinct("tag") def add_tags(self, tags): tags = [x for x in tags if x != ''] # Filter out empty tags diff --git a/servo/views/order.py b/servo/views/order.py index 4db8c4a..b6fae0a 100644 --- a/servo/views/order.py +++ b/servo/views/order.py @@ -31,9 +31,7 @@ from servo.forms.orders import * from servo.forms.repairs import StatusForm from servo.models import Note, User, Device, Customer -from servo.models.common import (Tag, - FlaggedItem, - GsxAccount,) +from servo.models.common import (Tag, FlaggedItem, GsxAccount,) from servo.models.repair import (Checklist, ChecklistItem, Repair, @@ -93,7 +91,7 @@ def prepare_list_view(request, args): if args.get("spec"): spec = args['spec'][0] - if spec is "None": + if spec == "None": orders = orders.filter(devices=None) else: orders = orders.filter(devices__slug=spec) @@ -742,7 +740,7 @@ def add_part(request, pk, device, code): try: tag, created = TaggedItem.objects.get_or_create( - ref="product", + content_type__model="product", object_id=product.pk, tag=device.description ) diff --git a/servo/views/product.py b/servo/views/product.py index 47b9018..4c7f96f 100644 --- a/servo/views/product.py +++ b/servo/views/product.py @@ -82,7 +82,7 @@ def tags(request): Returns all product tags """ from servo.lib.utils import json_response - tags = TaggedItem.objects.filter(ref="product") + tags = TaggedItem.objects.filter(content_type__model="product") tags = tags.distinct("tag").values_list("tag", flat=True) return json_response(list(tags)) -- cgit v1.2.3