2014-01-22 14:43:33 +08:00
|
|
|
from django.contrib.contenttypes.fields import (
|
2015-01-28 20:35:27 +08:00
|
|
|
GenericForeignKey, GenericRelation,
|
2014-01-22 14:43:33 +08:00
|
|
|
)
|
2009-04-04 03:52:14 +08:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2011-10-14 05:34:56 +08:00
|
|
|
from django.db import models
|
2014-07-15 00:42:14 +08:00
|
|
|
from django.db.models.deletion import ProtectedError
|
2009-04-04 03:52:14 +08:00
|
|
|
|
2011-10-14 05:34:56 +08:00
|
|
|
__all__ = ('Link', 'Place', 'Restaurant', 'Person', 'Address',
|
|
|
|
'CharLink', 'TextLink', 'OddRelation1', 'OddRelation2',
|
2013-02-02 09:10:16 +08:00
|
|
|
'Contact', 'Organization', 'Note', 'Company')
|
2010-02-11 03:21:52 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2009-04-04 03:52:14 +08:00
|
|
|
class Link(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2009-04-04 03:52:14 +08:00
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 14:43:33 +08:00
|
|
|
content_object = GenericForeignKey()
|
2009-04-04 03:52:14 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-04-04 03:52:14 +08:00
|
|
|
return "Link to %s id=%s" % (self.content_type, self.object_id)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2017-07-22 06:21:13 +08:00
|
|
|
class LinkProxy(Link):
|
|
|
|
class Meta:
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
|
2009-04-04 03:52:14 +08:00
|
|
|
class Place(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2018-03-13 10:42:48 +08:00
|
|
|
links = GenericRelation(Link, related_query_name='places')
|
2017-07-22 06:21:13 +08:00
|
|
|
link_proxy = GenericRelation(LinkProxy)
|
2010-01-23 02:39:48 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2009-04-04 03:52:14 +08:00
|
|
|
return "Place: %s" % self.name
|
2010-01-23 02:39:48 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-01-23 02:39:48 +08:00
|
|
|
class Restaurant(Place):
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2010-01-23 02:39:48 +08:00
|
|
|
return "Restaurant: %s" % self.name
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2017-12-06 05:08:50 +08:00
|
|
|
class Cafe(Restaurant):
|
|
|
|
def __str__(self):
|
|
|
|
return "Cafe: %s" % self.name
|
|
|
|
|
|
|
|
|
2010-01-23 02:39:48 +08:00
|
|
|
class Address(models.Model):
|
|
|
|
street = models.CharField(max_length=80)
|
|
|
|
city = models.CharField(max_length=50)
|
|
|
|
state = models.CharField(max_length=2)
|
|
|
|
zipcode = models.CharField(max_length=5)
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2010-01-23 02:39:48 +08:00
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 14:43:33 +08:00
|
|
|
content_object = GenericForeignKey()
|
2010-01-23 02:39:48 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2010-01-23 02:39:48 +08:00
|
|
|
return '%s %s, %s %s' % (self.street, self.city, self.state, self.zipcode)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-01-23 02:39:48 +08:00
|
|
|
class Person(models.Model):
|
|
|
|
account = models.IntegerField(primary_key=True)
|
|
|
|
name = models.CharField(max_length=128)
|
2014-01-22 14:43:33 +08:00
|
|
|
addresses = GenericRelation(Address)
|
2010-01-23 02:39:48 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
def __str__(self):
|
2010-01-23 02:39:48 +08:00
|
|
|
return self.name
|
2010-01-30 01:58:22 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-01-30 01:58:22 +08:00
|
|
|
class CharLink(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2010-01-30 01:58:22 +08:00
|
|
|
object_id = models.CharField(max_length=100)
|
2014-01-22 14:43:33 +08:00
|
|
|
content_object = GenericForeignKey()
|
2010-01-30 01:58:22 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-01-30 01:58:22 +08:00
|
|
|
class TextLink(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2010-01-30 01:58:22 +08:00
|
|
|
object_id = models.TextField()
|
2014-01-22 14:43:33 +08:00
|
|
|
content_object = GenericForeignKey()
|
2010-01-30 01:58:22 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-01-30 01:58:22 +08:00
|
|
|
class OddRelation1(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2014-01-22 14:43:33 +08:00
|
|
|
clinks = GenericRelation(CharLink)
|
2010-01-30 01:58:22 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-01-30 01:58:22 +08:00
|
|
|
class OddRelation2(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2014-01-22 14:43:33 +08:00
|
|
|
tlinks = GenericRelation(TextLink)
|
2010-01-30 01:58:22 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-02-11 03:21:52 +08:00
|
|
|
# models for test_q_object_or:
|
|
|
|
class Note(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2010-02-11 03:21:52 +08:00
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 14:43:33 +08:00
|
|
|
content_object = GenericForeignKey()
|
2010-02-11 03:21:52 +08:00
|
|
|
note = models.TextField()
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-02-11 03:21:52 +08:00
|
|
|
class Contact(models.Model):
|
2014-01-22 14:43:33 +08:00
|
|
|
notes = GenericRelation(Note)
|
2010-02-11 03:21:52 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2010-02-11 03:21:52 +08:00
|
|
|
class Organization(models.Model):
|
|
|
|
name = models.CharField(max_length=255)
|
|
|
|
contacts = models.ManyToManyField(Contact, related_name='organizations')
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-02-02 09:10:16 +08:00
|
|
|
class Company(models.Model):
|
|
|
|
name = models.CharField(max_length=100)
|
2014-01-22 14:43:33 +08:00
|
|
|
links = GenericRelation(Link)
|
2013-02-02 09:10:16 +08:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "Company: %s" % self.name
|
2013-02-06 10:35:10 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-02-06 10:35:10 +08:00
|
|
|
# For testing #13085 fix, we also use Note model defined above
|
|
|
|
class Developer(models.Model):
|
|
|
|
name = models.CharField(max_length=15)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-02-06 10:35:10 +08:00
|
|
|
class Team(models.Model):
|
|
|
|
name = models.CharField(max_length=15)
|
|
|
|
members = models.ManyToManyField(Developer)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s team" % self.name
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return self.members.count()
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-02-06 10:35:10 +08:00
|
|
|
class Guild(models.Model):
|
|
|
|
name = models.CharField(max_length=15)
|
|
|
|
members = models.ManyToManyField(Developer)
|
|
|
|
|
2017-01-19 22:55:03 +08:00
|
|
|
def __bool__(self):
|
|
|
|
return False
|
2013-02-06 10:35:10 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-02-06 10:35:10 +08:00
|
|
|
class Tag(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE, related_name='g_r_r_tags')
|
2013-02-06 10:35:10 +08:00
|
|
|
object_id = models.CharField(max_length=15)
|
2014-01-22 14:43:33 +08:00
|
|
|
content_object = GenericForeignKey()
|
2013-02-06 10:35:10 +08:00
|
|
|
label = models.CharField(max_length=15)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-02-06 10:35:10 +08:00
|
|
|
class Board(models.Model):
|
|
|
|
name = models.CharField(primary_key=True, max_length=15)
|
2013-05-20 22:40:34 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2014-01-22 14:43:33 +08:00
|
|
|
class SpecialGenericRelation(GenericRelation):
|
2013-11-13 03:35:52 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2013-11-13 03:35:52 +08:00
|
|
|
self.editable = True
|
|
|
|
self.save_form_data_calls = 0
|
|
|
|
|
|
|
|
def save_form_data(self, *args, **kwargs):
|
|
|
|
self.save_form_data_calls += 1
|
|
|
|
|
|
|
|
|
2013-05-20 22:40:34 +08:00
|
|
|
class HasLinks(models.Model):
|
2018-08-09 09:25:18 +08:00
|
|
|
links = SpecialGenericRelation(Link, related_query_name='targets')
|
2013-05-20 22:40:34 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-05-20 22:40:34 +08:00
|
|
|
class HasLinkThing(HasLinks):
|
|
|
|
pass
|
2013-06-06 05:29:44 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-06-06 05:29:44 +08:00
|
|
|
class A(models.Model):
|
2017-05-06 22:56:28 +08:00
|
|
|
flag = models.BooleanField(null=True)
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2013-06-06 05:29:44 +08:00
|
|
|
object_id = models.PositiveIntegerField()
|
2014-01-22 14:43:33 +08:00
|
|
|
content_object = GenericForeignKey('content_type', 'object_id')
|
2013-06-06 05:29:44 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-06-06 05:29:44 +08:00
|
|
|
class B(models.Model):
|
2014-01-22 14:43:33 +08:00
|
|
|
a = GenericRelation(A)
|
2013-06-06 05:29:44 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-06-06 05:29:44 +08:00
|
|
|
class C(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
b = models.ForeignKey(B, models.CASCADE)
|
2013-06-06 05:29:44 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2013-06-06 05:29:44 +08:00
|
|
|
class D(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
b = models.ForeignKey(B, models.SET_NULL, null=True)
|
2013-06-06 05:29:44 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('id',)
|
2014-07-15 00:42:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Ticket #22998
|
|
|
|
|
|
|
|
class Node(models.Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
content_type = models.ForeignKey(ContentType, models.CASCADE)
|
2014-07-15 00:42:14 +08:00
|
|
|
object_id = models.PositiveIntegerField()
|
|
|
|
content = GenericForeignKey('content_type', 'object_id')
|
|
|
|
|
|
|
|
|
|
|
|
class Content(models.Model):
|
|
|
|
nodes = GenericRelation(Node)
|
2015-07-22 22:43:21 +08:00
|
|
|
related_obj = models.ForeignKey('Related', models.CASCADE)
|
2014-07-15 00:42:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Related(models.Model):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def prevent_deletes(sender, instance, **kwargs):
|
|
|
|
raise ProtectedError("Not allowed to delete.", [instance])
|
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2014-07-15 00:42:14 +08:00
|
|
|
models.signals.pre_delete.connect(prevent_deletes, sender=Node)
|