models.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 Product owner',blank=True,max_length = 300)
  21. architecture = models.CharField(verbose_name='Architecture',blank=True,max_length = 300)
  22. concept = models.CharField(verbose_name='Product 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. Product_coordination = models.CharField(verbose_name='Product 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='Product 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 Product(models.Model):
  90. title = models.CharField('Product 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. class BuildingProduct(Product):
  118. additionalInfo = models.CharField('Product Title',max_length = 100)
  119. @receiver(post_delete, sender=Product)
  120. def auto_delete_reverse_keys(sender, instance, **kwargs):
  121. if instance.credits:
  122. instance.credits.delete()
  123. if instance.description:
  124. instance.description.delete()
  125. if instance.interaction:
  126. instance.interaction.delete()
  127. class Link(models.Model):
  128. Product = models.ForeignKey(Product, on_delete=models.CASCADE)
  129. link_description = models.CharField(null=True, blank=True, max_length = 2048)
  130. link = models.URLField(null=True, blank=True, max_length = 2048)
  131. def Product_path(instance, filename):
  132. return 'marktplatz/media/{0}/{1}'.format(instance.Product.id, filename)
  133. class Media(models.Model):
  134. fs = FileSystemStorage(location=settings.MEDIA_ROOT)
  135. Product = models.ForeignKey(Product, on_delete=models.CASCADE)
  136. name_for = models.CharField(blank = True,max_length = 256)
  137. filename = models.CharField(max_length = 100)
  138. copyright = models.CharField(blank=True, max_length = 100)
  139. image = models.ImageField(upload_to=Product_path,storage=fs)
  140. image_small = ProcessedImageField(upload_to=Product_path,
  141. processors=[ResizeToCover(640, 360)],
  142. format='JPEG',
  143. options={'quality': 90})
  144. image_medium= ProcessedImageField(upload_to=Product_path,
  145. processors=[ResizeToCover(960, 540)],
  146. format='JPEG',
  147. options={'quality': 90})
  148. image_big = ProcessedImageField(upload_to=Product_path,
  149. processors=[ResizeToCover(1920, 1080)],
  150. format='JPEG',
  151. options={'quality': 90})
  152. image_norm = ProcessedImageField(upload_to=Product_path,
  153. processors=[Thumbnail(640, 360)],
  154. format='JPEG',
  155. options={'quality': 90},
  156. blank = True,
  157. null = True)
  158. def filename(self):
  159. return os.path.basename(self.image.name).split('.')[0]
  160. # These two auto-delete files from filesystem when they are unneeded:
  161. #
  162. @receiver(models.signals.post_delete, sender=Media)
  163. def auto_delete_file_on_delete(sender, instance, **kwargs):
  164. """
  165. Deletes file from filesystem
  166. when corresponding `MediaFile` object is deleted.
  167. """
  168. if instance.image:
  169. if os.path.isfile(instance.image.path):
  170. os.remove(instance.image.path)
  171. #
  172. if instance.image_small:
  173. if os.path.isfile(instance.image_small.path):
  174. os.remove(instance.image_small.path)
  175. #
  176. if instance.image_medium:
  177. if os.path.isfile(instance.image_medium.path):
  178. os.remove(instance.image_medium.path)
  179. #
  180. if instance.image_big:
  181. if os.path.isfile(instance.image_big.path):
  182. os.remove(instance.image_big.path)
  183. #
  184. if instance.image_norm:
  185. if os.path.isfile(instance.image_norm.path):
  186. os.remove(instance.image_norm.path)
  187. #
  188. #print(settings.MEDIA_ROOT+'/marktplatz/media/{0}/{1}'.format(instance.Product.id, instance.image.name))
  189. #print(instance.Product.id, instance.image.path)
  190. #os.remove(settings.MEDIA_ROOT+'/marktplatz/media/{0}/{1}_big'.format(instance.Product.id, instance.filename))
  191. #os.remove(settings.MEDIA_ROOT+'/marktplatz/media/{0}/{1}.jpg'.format(instance.Product.id, instance.image.name))
  192. #@receiver(models.signals.pre_save, sender=Media)
  193. #def auto_delete_file_on_change(sender, instance, **kwargs):
  194. # """
  195. # Deletes old file from filesystem
  196. # when corresponding `MediaFile` object is updated
  197. # with new file.
  198. # """
  199. # if not instance.pk:
  200. # return False
  201. #
  202. # try:
  203. # old_file = Media.objects.get(pk=instance.pk).image
  204. # except Media.DoesNotExist:
  205. # return False
  206. #
  207. # new_file = instance.image
  208. # if not old_file == new_file:
  209. # if os.path.isfile(old_file.path):
  210. # os.remove(old_file.path)
  211. #
  212. # try:
  213. # old_file = Media.objects.get(pk=instance.pk).image_small
  214. # except Media.DoesNotExist:
  215. # return False
  216. #
  217. # new_file = instance.image_small
  218. # if not old_file == new_file:
  219. # if os.path.isfile(old_file.path):
  220. # os.remove(old_file.path)
  221. #
  222. # try:
  223. # old_file = Media.objects.get(pk=instance.pk).image_medium
  224. # except Media.DoesNotExist:
  225. # return False
  226. #
  227. # new_file = instance.image_medium
  228. # if not old_file == new_file:
  229. # if os.path.isfile(old_file.path):
  230. # os.remove(old_file.path)
  231. #
  232. # try:
  233. # old_file = Media.objects.get(pk=instance.pk).image_big
  234. # except Media.DoesNotExist:
  235. # return False
  236. #
  237. # new_file = instance.image_big
  238. # if not old_file == new_file:
  239. # if os.path.isfile(old_file.path):
  240. # os.remove(old_file.path)
  241. #
  242. # try:
  243. # old_file = Media.objects.get(pk=instance.pk).image_norm
  244. # except Media.DoesNotExist:
  245. # return False
  246. #
  247. # new_file = instance.image_norm
  248. # if not old_file == new_file:
  249. # if os.path.isfile(old_file.path):
  250. # os.remove(old_file.path)
  251. #
  252. class Video(models.Model):
  253. Product = models.ForeignKey(Product, on_delete=models.CASCADE)
  254. name_for = models.CharField(blank=True, max_length=256)
  255. filename = models.CharField(max_length=100)
  256. copyright = models.CharField(blank=True, max_length=100)
  257. image = models.FileField(upload_to=Product_path)
  258. @receiver(models.signals.post_delete, sender=Video)
  259. def auto_delete_video_on_delete(sender, instance, **kwargs):
  260. """
  261. Deletes file from filesystem
  262. when corresponding `MediaFile` object is deleted.
  263. """
  264. if instance.image:
  265. if os.path.isfile(instance.image.path):
  266. os.remove(instance.image.path)
  267. #@receiver(models.signals.pre_save, sender=Video)
  268. #def auto_delete_video_on_change(sender, instance, **kwargs):
  269. # """
  270. # Deletes old file from filesystem
  271. # when corresponding `MediaFile` object is updated
  272. # with new file.
  273. # """
  274. # if not instance.pk:
  275. # return False
  276. #
  277. # try:
  278. # old_file = Video.objects.get(pk=instance.pk).image
  279. # except Media.DoesNotExist:
  280. # return False
  281. #
  282. # new_file = instance.image
  283. # if not old_file == new_file:
  284. # if os.path.isfile(old_file.path):
  285. # os.remove(old_file.path)
  286. class Vote(models.Model):
  287. Product = models.ForeignKey(Product, on_delete=models.CASCADE)
  288. juryMember = models.ForeignKey(User, on_delete = models.CASCADE)
  289. vote = models.PositiveSmallIntegerField(default=0)
  290. comment = models.CharField(blank=True, max_length =300)
  291. def __iter__(self):
  292. for field in self._meta.fields:
  293. yield (field.verbose_name, field.value_to_string(self))