Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Django Models.py

By: a guest on Jun 22nd, 2013  |  syntax: Python  |  size: 3.51 KB  |  views: 59  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3.  
  4. from django.db.models.signals import post_save
  5. """For now, all the models are just shoved into one file.
  6. Will seperate eventually."""
  7.  
  8. max_name_length = 25
  9. max_location_length = 20
  10. max_title_length = 30
  11. max_post_length = 65000
  12.  
  13. class Player(models.Model):
  14.     #name ia covered by the django.auth User object
  15.     user = models.OneToOneField(User)
  16.     friends = models.ManyToManyField("self", symmetrical=True)
  17.     post_count = models.PositiveIntegerField(default=0)
  18.     #Date joined also covered by user
  19.     #date_joined = models.DateField()
  20.     location = models.CharField(max_length=max_location_length)
  21.     #Again, subsumed by user
  22.     #groups = models.ManyToManyField("Group", related_name="groups", db_table=u"PLAYERS_TO_GROUPS")
  23.     #size?
  24.     #size = models.BigIntegerField()
  25.     #Implemented by django.auth User
  26.     #permissions = models.ForeignKey("Permission_Group")
  27.  
  28. def create_user_profile(sender, instance, created, **kwargs):
  29.     if created:
  30.         Player.objects.create(user=instance)
  31.  
  32. post_save.connect(create_user_profile, sender=User)
  33.  
  34. class ManyToManyField_NoSyncdb(models.ManyToManyField):
  35.     def __init__(self, *args, **kwargs):
  36.         super(ManyToManyField_NoSyncdb, self).__init__(*args, **kwargs)
  37.         self.creates_table = False
  38.        
  39. class Group(models.Model):
  40.     name = models.CharField(max_length=max_name_length)
  41.     admins = models.ManyToManyField(Player, symmetrical=True)
  42.     members = ManyToManyField_NoSyncdb(Player, related_name="members", db_table="PLAYERS_TO_GROUPS")
  43.     description = models.TextField(max_length=max_post_length)
  44.  
  45. class Section(models.Model):
  46.     title = models.CharField(max_length=max_title_length)
  47.  
  48. #Could have used inheritance.
  49. #Decided not to so all data is in one table. (Only one field is
  50. #shared anyway) Removed here because lots of forum data is expected
  51. class Subsection(models.Model):
  52.     title = models.CharField(max_length=max_title_length)
  53.     parent = models.ForeignKey(Section, related_name="children")
  54.  
  55. class Thread(models.Model):
  56.     description = models.CharField(max_length=max_title_length)
  57.     author = models.ForeignKey(Player, related_name="threads")
  58.     post_date = models.DateField()
  59.     parent = models.ForeignKey(Subsection, related_name="threads")
  60.     closed = models.BooleanField()
  61.  
  62. #Message-based classes
  63.    
  64. class Message(models.Model):
  65.     title = models.CharField(max_length=max_title_length)
  66.     author = models.ForeignKey(Player, related_name="messages")
  67.     post_date = models.DateField()
  68.     text = models.TextField(max_length=max_post_length)
  69.  
  70. class Post(Message):
  71.     flagged = models.BooleanField()
  72.  
  73. class Pm(Message):
  74.     read = models.BooleanField()
  75.     starred = models.BooleanField()
  76.  
  77. #Douibly implemented to avoid generic relation ugliness
  78. class Friend_Request(models.Model):
  79.     author = models.ForeignKey(Player, related_name="sent_friend_requests")  
  80.     recipient = models.ForeignKey(Player, related_name="friend_requests")
  81.     #None represents not-yet-answered
  82.     #False transforms this into a block
  83.     accepted = models.NullBooleanField()
  84.  
  85. #Douibly implemented to avoid generic relation ugliness
  86. class Join_Group_Request(models.Model):
  87.     author = models.ForeignKey(Player, related_name="sent_group_requests")  
  88.     recipient = models.ForeignKey(Group, related_name="membership_requests")
  89.     #None represents not-yet-answered
  90.     #False transforms this into a block
  91.     accepted = models.NullBooleanField()