Skip to content
Snippets Groups Projects
Commit 87940ebd authored by Alexander Pace's avatar Alexander Pace
Browse files

created nickname model object

uses a genericforeignkey so it can be "to" events or superevents.
name of the nickname is unique, and can only be applied to one event
or superevent at a time.

next up, add some logic for creating GWs and populating nicknames.
parent afc67989
No related branches found
No related tags found
No related merge requests found
# Generated by Django 2.2.10 on 2020-09-28 20:15
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('events', '0055_add_catalog_search'),
]
operations = [
migrations.CreateModel(
name='Nickname',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=50, unique=True)),
('description', models.TextField()),
('object_id', models.PositiveIntegerField()),
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
],
),
]
......@@ -13,6 +13,8 @@ from model_utils.managers import InheritanceManager
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.fields import GenericRelation
from guardian.models import GroupObjectPermission
import logging; log = logging.getLogger(__name__)
......@@ -158,6 +160,23 @@ class Label(models.Model):
# applied, but a user tries to apply 'ADVREQ')
pass
# Creating "Nickname" object class. Set up a generic foreign key so that a
# nickname object can be linked to either an event or a superevent.
#
# https://medium.com/@bhrigu/django-how-to-add-foreignkey-to-multiple-models-394596f06e84
@python_2_unicode_compatible
class Nickname(models.Model):
name = models.CharField(max_length=50, unique=True)
description = models.TextField(blank=False)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
def __str__(self):
return self.name
@python_2_unicode_compatible
class Event(models.Model):
......@@ -234,6 +253,14 @@ class Event(models.Model):
gw_id = models.CharField(max_length=25, blank=True, null=True, unique=True)
# Update: new development. many-to-one relationship between nickname(s) and
# events and superevents. Some GWs were "confirmed" before the advent of
# superevents and so the nickname object should be applied to either events or
# superevents. That's why there's all this genericforeignkey business instead of
# just a foreignkey.
gw_ids = GenericRelation(Nickname)
class Meta:
ordering = ["-id"]
......
......@@ -14,6 +14,7 @@ from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group as AuthGroup
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericRelation
from django.core.exceptions import ValidationError
from django.db import models, IntegrityError
from django.urls import reverse
......@@ -28,7 +29,7 @@ from core.models import CleanSaveModel, AutoIncrementModel, LogBase, \
from core.time_utils import posixToGpsTime, gpsToUtc
from core.utils import int_to_letters, letters_to_int
from events.models import Event, SignoffBase, VOEventBase, EMObservationBase, \
EMFootprintBase
EMFootprintBase, Nickname
# AEP experimental: try computedfields stuff:
# https://django-computedfields.readthedocs.io/en/
......@@ -119,6 +120,9 @@ class Superevent(CleanSaveModel, AutoIncrementModel, ComputedFieldsModel):
# Cannibalizing gw_id field, putting into DB as a user-defined parameter.
gw_id = models.CharField(max_length=25, blank=True, null=True, unique=True)
# Adding gw_ids generic relation.
gw_ids = GenericRelation(Nickname)
# Booleans
is_gw = models.BooleanField(default=False)
# Because there are multiple actions/permissions involved with exposing a
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment