diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2015-01-26 00:50:58 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2015-01-26 00:50:58 +0100 |
commit | 4b1211e355370b0904dbc6a7fa9e23bb02622034 (patch) | |
tree | fd3ef2bf34cff849c77c23ced9cc1ab774a9b98d /ishtar_common/models.py | |
parent | eae7d043a96387310cc8f7ee88a1a2568692b021 (diff) | |
download | Ishtar-4b1211e355370b0904dbc6a7fa9e23bb02622034.tar.bz2 Ishtar-4b1211e355370b0904dbc6a7fa9e23bb02622034.zip |
Add a generic relation model to list general types keys. Reorganize imports.
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index a89a32991..90232159c 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (C) 2010-2014 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2010-2015 Étienne Loks <etienne.loks_AT_peacefrogsDOTnet> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -41,6 +41,8 @@ from django.utils.safestring import SafeUnicode, mark_safe from django.template.defaultfilters import slugify from django.contrib.auth.models import User, Group +from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes import generic from django.contrib.gis.db import models from django.contrib import admin @@ -351,6 +353,34 @@ class GeneralType(models.Model): self.txt_idx = slugify(self.label) return super(GeneralType, self).save(*args, **kwargs) + def get_keys(self): + keys = [] + content_type = ContentType.objects.get_for_model(self.__class__) + for ik in ItemKey.objects.filter(content_type=content_type, + object_id=ik.pk).all(): + keys.append(ik.key) + return keys + + @classmethod + def generate_keys(cls): + content_type = ContentType.objects.get_for_model(cls) + for item in cls.objects.all(): + for key in (slugify(item.label), item.txt_idx): + if not ItemKey.objects.filter(object_id=item.pk, key=key, + content_type=content_type).count(): + ik = ItemKey(object_id=item.pk, key=key, + content_type=content_type) + ik.save() + +class ItemKey(models.Model): + key = models.CharField(_(u"Key"), max_length=100) + content_type = models.ForeignKey(ContentType) + object_id = models.PositiveIntegerField() + content_object = generic.GenericForeignKey('content_type', 'object_id') + + def __unicode__(self): + return self.key + class ImageModel(models.Model): image = models.ImageField(upload_to="upload/", blank=True, null=True) thumbnail = models.ImageField(upload_to='upload/thumbs/', blank=True, |