models.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. from imagekit.models import ImageSpecField
  4. from imagekit.processors import ResizeToFill
  5. from imagekit.processors import ResizeToCover
  6. from imagekit.processors import Thumbnail
  7. from imagekit.processors import ResizeToCover
  8. from imagekit.models import ProcessedImageField
  9. from django.core.files.storage import FileSystemStorage
  10. from django_countries.fields import CountryField
  11. import os
  12. import uuid
  13. from django.db import models
  14. from django.dispatch import receiver
  15. from django.db.models.signals import post_delete, post_save
  16. from django.conf import settings
  17. import datetime
  18. import shutil
  19. class Credit(models.Model):
  20. owner = models.CharField(verbose_name='Building or project owner',blank=True,max_length = 300)
  21. architecture = models.CharField(verbose_name='Architecture',blank=True,max_length = 300)
  22. concept = models.CharField(verbose_name='Project artist/ concept/ design/ planning',blank=True,max_length = 300)
  23. structural_engeneering = models.CharField(verbose_name='Structural engineering',blank=True,max_length = 300)
  24. facade_design = models.CharField(verbose_name='Facade design',blank=True,max_length = 300)
  25. face_construction = models.CharField(verbose_name='Facade construction',blank=True,max_length = 300)
  26. kinetic_design = models.CharField(verbose_name='Kinetic engineering',blank = True,max_length = 300)
  27. light_design = models.CharField(verbose_name='Light design',blank = True,max_length = 300)
  28. tecnical_layout = models.CharField(verbose_name='Technical layout light',blank = True,max_length = 300)
  29. display_content = models.CharField(verbose_name='Display content/ visuals/ showreel',blank = True,max_length = 300)
  30. light_hardware = models.CharField(verbose_name='Light hardware (LED hardware)',blank = True,max_length = 300)
  31. lightning_software = models.CharField(verbose_name='Lighting control software',blank = True,max_length = 300)
  32. project_coordination = models.CharField(verbose_name='Project co-ordination',blank = True,max_length = 300)
  33. membrane_skin = models.CharField(verbose_name='Membrane skin',blank = True,max_length = 300)
  34. interaction_design = models.CharField(verbose_name='Interaction design/ programming',blank = True,max_length = 300)
  35. sponsor = models.CharField(verbose_name='Project sponsor/ support',blank = True,max_length = 500)
  36. module_elems = models.CharField(verbose_name='Pixel or other basic module/ elements',blank = True,max_length = 300)
  37. def __iter__(self):
  38. for field in self._meta.fields:
  39. if field.value_to_string(self) is not '-' and field.value_to_string(self) is not 'N/A':
  40. yield (field.verbose_name, field.value_to_string(self))
  41. class Description(models.Model):
  42. facade = models.TextField(verbose_name='Facade type and geometry (structure)',blank = True,max_length = 900)
  43. light_creation = models.TextField(verbose_name='Kind of light creation',blank = True,max_length = 900)
  44. resolution = models.TextField(verbose_name='Resolution and transmitting behaviour',blank = True,max_length = 900)
  45. pixel_distance = models.CharField(verbose_name='Pixel distance',blank = True,max_length = 300)
  46. luminance = models.TextField(verbose_name='Luminace',blank = True,max_length = 900)
  47. urban_situation = models.TextField(verbose_name='Urban situation',blank = True,max_length = 900)
  48. description_showreel = models.TextField(verbose_name='Description of showreel',blank = True,max_length = 900)
  49. def __iter__(self):
  50. for field in self._meta.fields:
  51. if (field.value_to_string(self) is not '-') and (field.value_to_string(self) is not 'N/A'):
  52. yield (field.verbose_name, field.value_to_string(self))
  53. class Interaction(models.Model):
  54. communtity = models.TextField(verbose_name='Community or communities involved',blank = True,max_length = 900)
  55. host = models.CharField(verbose_name='Host organization',blank = True,max_length = 900)
  56. legal_form = models.CharField(verbose_name='Legal form',blank=True,max_length = 900)
  57. issues = models.TextField(verbose_name='Issues addressed',blank = True,max_length = 900)
  58. impact = models.TextField(verbose_name='Impact',blank = True,max_length = 900)
  59. tools = models.TextField(verbose_name='Tools developed',blank = True,max_length = 900)
  60. tools_used = models.TextField(verbose_name='Tools used',blank = True,max_length = 900)
  61. next_steps = models.TextField(verbose_name='Next steps',blank = True,max_length = 900)
  62. def __iter__(self):
  63. for field in self._meta.fields:
  64. if (field.value_to_string(self) is not '-') and (field.value_to_string(self) is not 'N/A'):
  65. yield (field.verbose_name, field.value_to_string(self))
  66. class Contact(models.Model):
  67. user = models.OneToOneField(User, on_delete=models.CASCADE,blank=True)
  68. first_name = models.CharField(max_length = 100)
  69. last_name = models.CharField(max_length = 100)
  70. adress = models.CharField(blank=True,max_length = 100)
  71. postcode = models.CharField(blank=True, max_length = 100)
  72. city = models.CharField(blank=True,max_length = 100)
  73. country = models.CharField(blank=True,max_length = 100)
  74. email = models.EmailField(unique=True)
  75. phonenumber = models.CharField(blank=True,max_length = 100)
  76. alternate_phonenumber = models.CharField(blank = True,max_length = 100)
  77. skype_name = models.CharField(blank=True,max_length = 300)
  78. website = models.URLField(blank=True)
  79. def __str__(self):
  80. return self.first_name + ' ' + self.last_name
  81. def __iter__(self):
  82. for field in self._meta.fields:
  83. yield (field.verbose_name, field.value_to_string(self))
  84. class Category(models.Model):
  85. name = models.CharField(max_length=100)
  86. short_name = models.CharField(max_length=10)
  87. def __str__(self):
  88. return self.name
  89. class Project(models.Model):
  90. title = models.CharField('Project Title',max_length = 100)
  91. country = CountryField(blank=True,multiple=True)
  92. city = models.CharField(max_length = 100,null = True, blank=True)
  93. year = models.IntegerField('Year of Completion',null = True, blank=True)
  94. owner = models.CharField(max_length = 300,null = True,blank=True)
  95. teaser_txt = models.TextField(blank = True, max_length = 1050)
  96. header = models.CharField(max_length = 900,null = True)
  97. description_txt = models.TextField(max_length = 3000,null = True)
  98. cid = models.IntegerField(null = True,blank=True)
  99. class_cid = models.CharField(null = True,max_length = 3, blank=True)
  100. sumbitted = models.CharField(null = True,max_length = 10, blank=True)
  101. date_submitted = models.DateField(auto_now_add=True)
  102. terms = models.NullBooleanField()
  103. edit = models.NullBooleanField()
  104. public = models.NullBooleanField()
  105. photo = models.CharField(max_length=500, null=True, blank=True)
  106. videocts = models.CharField(max_length=500, null=True, blank=True)
  107. category = models.ManyToManyField(Category, null=True)
  108. credits = models.ForeignKey(Credit, null = True,on_delete=models.SET_NULL)
  109. description = models.ForeignKey(Description,null = True, on_delete=models.SET_NULL)
  110. contact = models.ForeignKey(Contact, null = True,on_delete=models.SET_NULL)
  111. interaction = models.ForeignKey(Interaction, null = True,on_delete = models.SET_NULL)
  112. def __str__(self):
  113. return str(self.title)
  114. def __iter__(self):
  115. for field in self._meta.fields:
  116. yield (field.verbose_name, field.value_to_string(self))
  117. @receiver(post_delete, sender=Project)
  118. def auto_delete_reverse_keys(sender, instance, **kwargs):
  119. if instance.credits:
  120. instance.credits.delete()
  121. if instance.description:
  122. instance.description.delete()
  123. if instance.interaction:
  124. instance.interaction.delete()
  125. class Link(models.Model):
  126. project = models.ForeignKey(Project, on_delete=models.CASCADE)
  127. link_description = models.CharField(null=True, blank=True, max_length = 2048)
  128. link = models.URLField(null=True, blank=True, max_length = 2048)
  129. def project_path(instance, filename):
  130. return 'jurysys/media/{0}/{1}'.format(instance.project.id, filename)
  131. class Media(models.Model):
  132. fs = FileSystemStorage(location=settings.MEDIA_ROOT)
  133. project = models.ForeignKey(Project, on_delete=models.CASCADE)
  134. name_for = models.CharField(blank = True,max_length = 256)
  135. filename = models.CharField(max_length = 100)
  136. copyright = models.CharField(blank=True, max_length = 100)
  137. image = models.ImageField(upload_to=project_path,storage=fs)
  138. image_small = ProcessedImageField(upload_to=project_path,
  139. processors=[ResizeToCover(640, 360)],
  140. format='JPEG',
  141. options={'quality': 90})
  142. image_medium= ProcessedImageField(upload_to=project_path,
  143. processors=[ResizeToCover(960, 540)],
  144. format='JPEG',
  145. options={'quality': 90})
  146. image_big = ProcessedImageField(upload_to=project_path,
  147. processors=[ResizeToCover(1920, 1080)],
  148. format='JPEG',
  149. options={'quality': 90})
  150. image_norm = ProcessedImageField(upload_to=project_path,
  151. processors=[Thumbnail(640, 360)],
  152. format='JPEG',
  153. options={'quality': 90},
  154. blank = True,
  155. null = True)
  156. def filename(self):
  157. return os.path.basename(self.image.name).split('.')[0]
  158. # These two auto-delete files from filesystem when they are unneeded:
  159. #
  160. @receiver(models.signals.post_delete, sender=Media)
  161. def auto_delete_file_on_delete(sender, instance, **kwargs):
  162. """
  163. Deletes file from filesystem
  164. when corresponding `MediaFile` object is deleted.
  165. """
  166. if instance.image:
  167. if os.path.isfile(instance.image.path):
  168. os.remove(instance.image.path)
  169. #
  170. if instance.image_small:
  171. if os.path.isfile(instance.image_small.path):
  172. os.remove(instance.image_small.path)
  173. #
  174. if instance.image_medium:
  175. if os.path.isfile(instance.image_medium.path):
  176. os.remove(instance.image_medium.path)
  177. #
  178. if instance.image_big:
  179. if os.path.isfile(instance.image_big.path):
  180. os.remove(instance.image_big.path)
  181. #
  182. if instance.image_norm:
  183. if os.path.isfile(instance.image_norm.path):
  184. os.remove(instance.image_norm.path)
  185. #
  186. #print(settings.MEDIA_ROOT+'/jurysys/media/{0}/{1}'.format(instance.project.id, instance.image.name))
  187. #print(instance.project.id, instance.image.path)
  188. #os.remove(settings.MEDIA_ROOT+'/jurysys/media/{0}/{1}_big'.format(instance.project.id, instance.filename))
  189. #os.remove(settings.MEDIA_ROOT+'/jurysys/media/{0}/{1}.jpg'.format(instance.project.id, instance.image.name))
  190. #@receiver(models.signals.pre_save, sender=Media)
  191. #def auto_delete_file_on_change(sender, instance, **kwargs):
  192. # """
  193. # Deletes old file from filesystem
  194. # when corresponding `MediaFile` object is updated
  195. # with new file.
  196. # """
  197. # if not instance.pk:
  198. # return False
  199. #
  200. # try:
  201. # old_file = Media.objects.get(pk=instance.pk).image
  202. # except Media.DoesNotExist:
  203. # return False
  204. #
  205. # new_file = instance.image
  206. # if not old_file == new_file:
  207. # if os.path.isfile(old_file.path):
  208. # os.remove(old_file.path)
  209. #
  210. # try:
  211. # old_file = Media.objects.get(pk=instance.pk).image_small
  212. # except Media.DoesNotExist:
  213. # return False
  214. #
  215. # new_file = instance.image_small
  216. # if not old_file == new_file:
  217. # if os.path.isfile(old_file.path):
  218. # os.remove(old_file.path)
  219. #
  220. # try:
  221. # old_file = Media.objects.get(pk=instance.pk).image_medium
  222. # except Media.DoesNotExist:
  223. # return False
  224. #
  225. # new_file = instance.image_medium
  226. # if not old_file == new_file:
  227. # if os.path.isfile(old_file.path):
  228. # os.remove(old_file.path)
  229. #
  230. # try:
  231. # old_file = Media.objects.get(pk=instance.pk).image_big
  232. # except Media.DoesNotExist:
  233. # return False
  234. #
  235. # new_file = instance.image_big
  236. # if not old_file == new_file:
  237. # if os.path.isfile(old_file.path):
  238. # os.remove(old_file.path)
  239. #
  240. # try:
  241. # old_file = Media.objects.get(pk=instance.pk).image_norm
  242. # except Media.DoesNotExist:
  243. # return False
  244. #
  245. # new_file = instance.image_norm
  246. # if not old_file == new_file:
  247. # if os.path.isfile(old_file.path):
  248. # os.remove(old_file.path)
  249. #
  250. class Video(models.Model):
  251. project = models.ForeignKey(Project, on_delete=models.CASCADE)
  252. name_for = models.CharField(blank=True, max_length=256)
  253. filename = models.CharField(max_length=100)
  254. copyright = models.CharField(blank=True, max_length=100)
  255. image = models.FileField(upload_to=project_path)
  256. @receiver(models.signals.post_delete, sender=Video)
  257. def auto_delete_video_on_delete(sender, instance, **kwargs):
  258. """
  259. Deletes file from filesystem
  260. when corresponding `MediaFile` object is deleted.
  261. """
  262. if instance.image:
  263. if os.path.isfile(instance.image.path):
  264. os.remove(instance.image.path)
  265. #@receiver(models.signals.pre_save, sender=Video)
  266. #def auto_delete_video_on_change(sender, instance, **kwargs):
  267. # """
  268. # Deletes old file from filesystem
  269. # when corresponding `MediaFile` object is updated
  270. # with new file.
  271. # """
  272. # if not instance.pk:
  273. # return False
  274. #
  275. # try:
  276. # old_file = Video.objects.get(pk=instance.pk).image
  277. # except Media.DoesNotExist:
  278. # return False
  279. #
  280. # new_file = instance.image
  281. # if not old_file == new_file:
  282. # if os.path.isfile(old_file.path):
  283. # os.remove(old_file.path)
  284. class Vote(models.Model):
  285. project = models.ForeignKey(Project, on_delete=models.CASCADE)
  286. juryMember = models.ForeignKey(User, on_delete = models.CASCADE)
  287. vote = models.PositiveSmallIntegerField(default=0)
  288. comment = models.CharField(blank=True, max_length =300)
  289. def __iter__(self):
  290. for field in self._meta.fields:
  291. yield (field.verbose_name, field.value_to_string(self))