summaryrefslogtreecommitdiff
path: root/ishtar_common/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r--ishtar_common/models.py141
1 files changed, 95 insertions, 46 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index bf35e213e..6196e1efe 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -63,13 +63,13 @@ from django.core.exceptions import (
)
from django.core.files.base import ContentFile
from django.core.files.uploadedfile import SimpleUploadedFile
-from django.core.urlresolvers import reverse
from django.db import connection
from django.db.models import Q, Max, Count
from django.db.models.signals import post_save, post_delete, m2m_changed
from django.db.utils import DatabaseError
from django.template import Context, Template
from django.template.defaultfilters import slugify
+from django.urls import reverse
from django.utils.functional import lazy
from ishtar_common.utils import (
ugettext_lazy as _,
@@ -427,14 +427,22 @@ class TinyUrl(models.Model):
class ItemKey(models.Model):
key = models.TextField(_("Key"))
- content_type = models.ForeignKey(ContentType)
+ content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey("content_type", "object_id")
importer = models.ForeignKey(
- Import, null=True, blank=True, help_text=_("Specific key to an import")
+ Import,
+ null=True,
+ blank=True,
+ help_text=_("Specific key to an import"),
+ on_delete=models.SET_NULL,
+ )
+ user = models.ForeignKey(
+ "IshtarUser", blank=True, null=True, on_delete=models.SET_NULL
+ )
+ group = models.ForeignKey(
+ TargetKeyGroup, blank=True, null=True, on_delete=models.SET_NULL
)
- user = models.ForeignKey("IshtarUser", blank=True, null=True)
- group = models.ForeignKey(TargetKeyGroup, blank=True, null=True)
def __str__(self):
return self.key
@@ -658,7 +666,7 @@ class JsonDataSectionManager(models.Manager):
class JsonDataSection(models.Model):
- content_type = models.ForeignKey(ContentType)
+ content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
name = models.CharField(_("Name"), max_length=200)
order = models.IntegerField(_("Order"), default=10)
objects = JsonDataSectionManager()
@@ -699,7 +707,7 @@ class JsonDataFieldManager(models.Manager):
class JsonDataField(models.Model):
name = models.CharField(_("Name"), max_length=200)
- content_type = models.ForeignKey(ContentType)
+ content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
key = models.CharField(
_("Key"),
max_length=200,
@@ -760,7 +768,11 @@ class GeneralRelationType(GeneralType):
symmetrical = models.BooleanField(_("Symmetrical"))
tiny_label = models.CharField(_("Tiny label"), max_length=50, blank=True, null=True)
inverse_relation = models.ForeignKey(
- "self", verbose_name=_("Inverse relation"), blank=True, null=True
+ "self",
+ verbose_name=_("Inverse relation"),
+ blank=True,
+ null=True,
+ on_delete=models.SET_NULL,
)
logical_relation = models.CharField(
verbose_name=_("Logical relation"),
@@ -869,9 +881,7 @@ class RelationsViews(models.Model):
if not settings.USE_BACKGROUND_TASK:
return relation_view_update(cls, {"item_id": item_id})
else:
- sender, kwargs = serialize_args_for_tasks(
- cls, None, {"item_id": item_id}
- )
+ sender, kwargs = serialize_args_for_tasks(cls, None, {"item_id": item_id})
return relation_view_update.delay(sender, kwargs)
@classmethod
@@ -909,28 +919,30 @@ class RelationsViews(models.Model):
assert cls.DELETE_SQL
assert cls.CREATE_TABLE_SQL
profile = get_current_profile(force=True)
- table_type = ''
+ table_type = ""
with connection.cursor() as cursor:
- q = "select table_type from information_schema.tables WHERE " \
+ q = (
+ "select table_type from information_schema.tables WHERE "
"table_name=%s;"
+ )
cursor.execute(q, [cls._meta.db_table])
q = cursor.fetchall()
if q:
table_type = q[0][0]
if profile.parent_relations_engine == "V":
- if table_type == 'VIEW':
+ if table_type == "VIEW":
return
- elif 'TABLE' in table_type:
+ elif "TABLE" in table_type:
q = "DROP TABLE IF EXISTS %s" % cls._meta.db_table
cursor.execute(q)
cursor.execute(cls.CREATE_SQL)
return True
if profile.parent_relations_engine == "T":
- if 'TABLE' in table_type:
+ if "TABLE" in table_type:
return
- elif table_type == 'VIEW':
+ elif table_type == "VIEW":
cursor.execute(cls.DELETE_SQL)
cursor.execute(cls.CREATE_TABLE_SQL)
return True
@@ -939,8 +951,12 @@ class RelationsViews(models.Model):
class SearchQuery(models.Model):
label = models.TextField(_("Label"), blank=True, default="")
query = models.TextField(_("Query"), blank=True, default="")
- content_type = models.ForeignKey(ContentType, verbose_name=_("Content type"))
- profile = models.ForeignKey("UserProfile", verbose_name=_("Profile"))
+ content_type = models.ForeignKey(
+ ContentType, verbose_name=_("Content type"), on_delete=models.CASCADE
+ )
+ profile = models.ForeignKey(
+ "UserProfile", verbose_name=_("Profile"), on_delete=models.CASCADE
+ )
is_alert = models.BooleanField(_("Is an alert"), default=False)
class Meta:
@@ -1037,11 +1053,13 @@ class IshtarSiteProfile(models.Model, Cached):
),
default="V",
max_length=1,
- help_text=_("If you experience performance problems with complex relations "
- "(for instance: complex statigraphic relations), set it to "
- "\"Cache tables\" in order to use static cache tables. Do not "
- "forget to update theses table with the "
- "\"relations_update_cache_tables\" manage.py command.")
+ help_text=_(
+ "If you experience performance problems with complex relations "
+ "(for instance: complex statigraphic relations), set it to "
+ '"Cache tables" in order to use static cache tables. Do not '
+ "forget to update theses table with the "
+ '"relations_update_cache_tables" manage.py command.'
+ ),
)
config = models.CharField(
_("Alternate configuration"),
@@ -1104,8 +1122,9 @@ class IshtarSiteProfile(models.Model, Cached):
_("Use town to locate when coordinates are missing"), default=True
)
relation_graph = models.BooleanField(_("Generate relation graph"), default=False)
- preventive_operator = models.BooleanField(_("Preventive operator module"),
- default=False)
+ preventive_operator = models.BooleanField(
+ _("Preventive operator module"), default=False
+ )
underwater = models.BooleanField(_("Underwater module"), default=False)
parcel_mandatory = models.BooleanField(
_("Parcel are mandatory for context records"), default=True
@@ -1385,6 +1404,7 @@ class IshtarSiteProfile(models.Model, Cached):
help_text=_(
"Spatial Reference System used for display when no SRS is " "defined"
),
+ on_delete=models.SET_NULL,
)
default_language = models.ForeignKey(
Language,
@@ -1395,6 +1415,7 @@ class IshtarSiteProfile(models.Model, Cached):
"If set, by default the selected language will be set for "
"localized documents."
),
+ on_delete=models.SET_NULL,
)
objects = SlugModelManager()
@@ -1633,7 +1654,9 @@ class ExcludedFieldManager(models.Manager):
class ExcludedField(models.Model):
- custom_form = models.ForeignKey(CustomForm, related_name="excluded_fields")
+ custom_form = models.ForeignKey(
+ CustomForm, related_name="excluded_fields", on_delete=models.CASCADE
+ )
field = models.CharField(_("Field"), max_length=250)
objects = ExcludedFieldManager()
@@ -1665,8 +1688,12 @@ class CustomFormJsonFieldManager(models.Manager):
class CustomFormJsonField(models.Model):
- custom_form = models.ForeignKey(CustomForm, related_name="json_fields")
- json_field = models.ForeignKey(JsonDataField, related_name="custom_form_details")
+ custom_form = models.ForeignKey(
+ CustomForm, related_name="json_fields", on_delete=models.CASCADE
+ )
+ json_field = models.ForeignKey(
+ JsonDataField, related_name="custom_form_details", on_delete=models.CASCADE
+ )
label = models.CharField(_("Label"), max_length=200, blank=True, default="")
order = models.IntegerField(verbose_name=_("Order"), default=1)
help_text = models.TextField(_("Help"), blank=True, default="")
@@ -1878,7 +1905,7 @@ class Dashboard(object):
class DocumentTemplate(models.Model):
name = models.CharField(_("Name"), max_length=100)
slug = models.SlugField(_("Slug"), max_length=100, unique=True)
- associated_model = models.ForeignKey(ImporterModel)
+ associated_model = models.ForeignKey(ImporterModel, on_delete=models.CASCADE)
template = models.FileField(
_("Template"),
upload_to="templates/%Y/",
@@ -2358,7 +2385,9 @@ class Organization(Address, Merge, OwnPerms, BaseGenderedType, ValueGetter, Main
# fields
uuid = models.UUIDField(default=uuid.uuid4)
name = models.CharField(_("Name"), max_length=500)
- organization_type = models.ForeignKey(OrganizationType, verbose_name=_("Type"))
+ organization_type = models.ForeignKey(
+ OrganizationType, verbose_name=_("Type"), on_delete=models.PROTECT
+ )
url = models.URLField(verbose_name=_("Web address"), blank=True, null=True)
grammatical_gender = models.CharField(
_("Grammatical gender"),
@@ -2969,7 +2998,9 @@ class ProfileTypeSummary(ProfileType):
class UserProfile(models.Model):
name = models.CharField(_("Name"), blank=True, default="", max_length=100)
- profile_type = models.ForeignKey(ProfileType, verbose_name=_("Profile type"))
+ profile_type = models.ForeignKey(
+ ProfileType, verbose_name=_("Profile type"), on_delete=models.PROTECT
+ )
areas = models.ManyToManyField(
"Area", verbose_name=_("Areas"), blank=True, related_name="profiles"
)
@@ -2978,7 +3009,10 @@ class UserProfile(models.Model):
auto_pin = models.BooleanField(_("Automatically pin"), default=False)
display_pin_menu = models.BooleanField(_("Display pin menu"), default=False)
person = models.ForeignKey(
- Person, verbose_name=_("Person"), related_name="profiles"
+ Person,
+ verbose_name=_("Person"),
+ related_name="profiles",
+ on_delete=models.CASCADE,
)
class Meta:
@@ -3122,9 +3156,14 @@ class IshtarUser(FullSearch):
}
# fields
- user_ptr = models.OneToOneField(User, primary_key=True, related_name="ishtaruser")
+ user_ptr = models.OneToOneField(
+ User, primary_key=True, related_name="ishtaruser", on_delete=models.CASCADE
+ )
person = models.OneToOneField(
- Person, verbose_name=_("Person"), related_name="ishtaruser"
+ Person,
+ verbose_name=_("Person"),
+ related_name="ishtaruser",
+ on_delete=models.CASCADE,
)
advanced_shortcut_menu = models.BooleanField(
_("Advanced shortcut menu"), default=False
@@ -3343,8 +3382,15 @@ class Author(FullSearch):
PARENT_SEARCH_VECTORS = ["person"]
uuid = models.UUIDField(default=uuid.uuid4)
- person = models.ForeignKey(Person, verbose_name=_("Person"), related_name="author")
- author_type = models.ForeignKey(AuthorType, verbose_name=_("Author type"))
+ person = models.ForeignKey(
+ Person,
+ verbose_name=_("Person"),
+ related_name="author",
+ on_delete=models.CASCADE,
+ )
+ author_type = models.ForeignKey(
+ AuthorType, verbose_name=_("Author type"), on_delete=models.PROTECT
+ )
cached_label = models.TextField(
_("Cached name"), blank=True, default="", db_index=True
)
@@ -3896,6 +3942,7 @@ class Document(
blank=True,
null=True,
related_name="publish",
+ on_delete=models.SET_NULL,
)
publishing_year = models.PositiveIntegerField(
_("Year of publication"), blank=True, null=True
@@ -3905,7 +3952,11 @@ class Document(
)
tags = models.ManyToManyField(DocumentTag, verbose_name=_("Tags"), blank=True)
language = models.ForeignKey(
- Language, verbose_name=_("Language"), blank=True, null=True
+ Language,
+ verbose_name=_("Language"),
+ blank=True,
+ null=True,
+ on_delete=models.SET_NULL,
)
issn = models.CharField(_("ISSN"), blank=True, null=True, max_length=10)
isbn = models.CharField(_("ISBN"), blank=True, null=True, max_length=17)
@@ -3915,6 +3966,7 @@ class Document(
blank=True,
null=True,
related_name="children",
+ on_delete=models.SET_NULL,
)
source_free_input = models.CharField(
verbose_name=_("Source - free input"),
@@ -4050,13 +4102,10 @@ class Document(
if len(operations) != 1:
return
current_operation = operations[0]
- q = (
- Document.objects.exclude(pk=self.pk)
- .filter(
- Q(operations__id=current_operation)
- | Q(context_records__operation_id=current_operation)
- | Q(finds__base_finds__context_record__operation_id=current_operation)
- )
+ q = Document.objects.exclude(pk=self.pk).filter(
+ Q(operations__id=current_operation)
+ | Q(context_records__operation_id=current_operation)
+ | Q(finds__base_finds__context_record__operation_id=current_operation)
)
if extra_filters:
q = q.filter(**extra_filters)
@@ -4746,7 +4795,7 @@ SCRIPT_STATE_DCT = dict(SCRIPT_STATE)
class AdministrationTask(models.Model):
- script = models.ForeignKey(AdministrationScript)
+ script = models.ForeignKey(AdministrationScript, on_delete=models.CASCADE)
state = models.CharField(
_("State"), max_length=2, choices=SCRIPT_STATE, default="S"
)