Advertisement
Guest User

models

a guest
Jan 7th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. from __future__ import unicode_literals
  2.  
  3. import datetime
  4. from django.db import models
  5. from django.utils import timezone
  6. from django.contrib.auth.models import User, AbstractUser
  7. from django.contrib.auth.admin import UserAdmin
  8. from django.db.models.signals import post_save
  9. from django.dispatch import receiver
  10. from datetime import date
  11. from itertools import groupby
  12. from django.utils.encoding import python_2_unicode_compatible
  13.  
  14.  
  15. @python_2_unicode_compatible
  16. class Profile(models.Model):
  17. STUDENT = 1
  18. TEACHER = 2
  19. SUPERVISOR = 3
  20. ROLE_CHOICES = (
  21. (STUDENT, 'Student'),
  22. (TEACHER, 'Teacher'),
  23. (SUPERVISOR, 'Supervisor'),
  24. )
  25. user = models.OneToOneField(User, on_delete=models.CASCADE)
  26. location = models.CharField(max_length=30, blank=True)
  27. birthdate = models.DateField(null=True, blank=True)
  28. role = models.PositiveSmallIntegerField(choices=ROLE_CHOICES, null=True, blank=True)
  29.  
  30. class Meta:
  31. verbose_name = 'profile'
  32. verbose_name_plural = 'profiles'
  33.  
  34. def __str__(self):
  35. return self.user.username
  36.  
  37.  
  38. @receiver(post_save, sender=User)
  39. def create_or_update_user_profile(sender, instance, created, **kwargs):
  40. if created:
  41. Profile.objects.create(user=instance)
  42. instance.profile.save()
  43.  
  44. class CustomUserAdmin(UserAdmin):
  45. inlines = (ProfileInline, )
  46. list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'get_location')
  47. list_select_related = ('profile', )
  48.  
  49. def get_location(self, instance):
  50. return instance.profile.location
  51. get_location.short_description = 'Location'
  52.  
  53. def get_inline_instances(self, request, obj=None):
  54. if not obj:
  55. return list()
  56. return super(CustomUserAdmin, self).get_inline_instances(request, obj)
  57.  
  58. class Question(models.Model):
  59. question_text = models.CharField(max_length=200)
  60. pub_date = models.DateTimeField('date published')
  61. def __str__(self):
  62. return self.question_text
  63. def was_published_recently(self):
  64. return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
  65.  
  66. class Choice(models.Model):
  67. question = models.ForeignKey(Question, on_delete=models.CASCADE)
  68. choice_text = models.CharField(max_length=200)
  69. votes = models.IntegerField(default=0)
  70. def __str__(self):
  71. return self.choice_text
  72.  
  73. class Post(models.Model):
  74. title = models.CharField(max_length = 20)
  75. body = models.TextField()
  76. timestamp = models.DateTimeField()
  77.  
  78. class Groups(models.Model):
  79. groupID = models.CharField(primary_key = True, max_length = 15)
  80. permissions = models.CharField(max_length = 15)
  81. description = models.TextField()
  82.  
  83. class Users(models.Model):
  84. userID = models.CharField(primary_key = True, max_length = 16)
  85. groupID = models.ForeignKey(Groups)
  86. password = models.CharField(max_length = 16)
  87. email = models.CharField(max_length = 20)
  88. fname = models.CharField(max_length = 16)
  89. sname = models.CharField(max_length = 16)
  90. description = models.TextField()
  91. picture = models.ImageField()
  92.  
  93. class Positions(models.Model):
  94. positionID = models.CharField(primary_key = True, max_length = 16)
  95. description = models.TextField()
  96.  
  97. class Venues(models.Model):
  98. venuesID = models.CharField(primary_key = True, max_length = 16)
  99. address = models.CharField(max_length = 30)
  100. website = models.CharField(max_length = 30)
  101. description = models.TextField()
  102.  
  103. class Bands(models.Model):
  104. bandID = models.CharField(primary_key = True, max_length = 16)
  105. description = models.TextField()
  106. picture = models.ImageField()
  107.  
  108. class Events(models.Model):
  109. eventID = models.CharField(primary_key = True, max_length = 16)
  110. bandID = models.ForeignKey(Bands)
  111. venuesID = models.ForeignKey(Venues)
  112. datetime = models.DateTimeField()
  113. cost = models.CharField(max_length = 4)
  114. description = models.TextField()
  115.  
  116. class BandMembers(models.Model):
  117. userID = models.ForeignKey(Users)
  118. bandID = models.ForeignKey(Bands)
  119. bandManager = models.BooleanField()
  120. positionID = models.ForeignKey(Positions)
  121.  
  122. class Overview(models.Model):
  123. ID = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')
  124. day = models.DateTimeField()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement