models.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. from django.db import models
  2. from django.core.validators import MinValueValidator, MaxValueValidator
  3. from django.contrib.auth.models import User
  4. from imagekit.models import ImageSpecField
  5. from imagekit.processors import ResizeToFill
  6. from imagekit.processors import ResizeToCover
  7. from imagekit.processors import Thumbnail
  8. from imagekit.processors import ResizeToCover
  9. from imagekit.models import ProcessedImageField
  10. from django.core.files.storage import FileSystemStorage
  11. from django_countries.fields import CountryField
  12. import os
  13. import uuid
  14. from django.db import models
  15. from django.dispatch import receiver
  16. from django.db.models.signals import post_delete, post_save
  17. from django.conf import settings
  18. import datetime
  19. import shutil
  20. class Credit(models.Model):
  21. owner = models.CharField(verbose_name='Building or Product owner',blank=True,max_length = 300)
  22. architecture = models.CharField(verbose_name='Architecture',blank=True,max_length = 300)
  23. concept = models.CharField(verbose_name='Product artist/ concept/ design/ planning',blank=True,max_length = 300)
  24. structural_engeneering = models.CharField(verbose_name='Structural engineering',blank=True,max_length = 300)
  25. facade_design = models.CharField(verbose_name='Facade design',blank=True,max_length = 300)
  26. face_construction = models.CharField(verbose_name='Facade construction',blank=True,max_length = 300)
  27. kinetic_design = models.CharField(verbose_name='Kinetic engineering',blank = True,max_length = 300)
  28. light_design = models.CharField(verbose_name='Light design',blank = True,max_length = 300)
  29. tecnical_layout = models.CharField(verbose_name='Technical layout light',blank = True,max_length = 300)
  30. display_content = models.CharField(verbose_name='Display content/ visuals/ showreel',blank = True,max_length = 300)
  31. light_hardware = models.CharField(verbose_name='Light hardware (LED hardware)',blank = True,max_length = 300)
  32. lightning_software = models.CharField(verbose_name='Lighting control software',blank = True,max_length = 300)
  33. Product_coordination = models.CharField(verbose_name='Product co-ordination',blank = True,max_length = 300)
  34. membrane_skin = models.CharField(verbose_name='Membrane skin',blank = True,max_length = 300)
  35. interaction_design = models.CharField(verbose_name='Interaction design/ programming',blank = True,max_length = 300)
  36. sponsor = models.CharField(verbose_name='Product sponsor/ support',blank = True,max_length = 500)
  37. module_elems = models.CharField(verbose_name='Pixel or other basic module/ elements',blank = True,max_length = 300)
  38. def __iter__(self):
  39. for field in self._meta.fields:
  40. if field.value_to_string(self) is not '-' and field.value_to_string(self) is not 'N/A':
  41. yield (field.verbose_name, field.value_to_string(self))
  42. class Description(models.Model):
  43. facade = models.TextField(verbose_name='Facade type and geometry (structure)',blank = True,max_length = 900)
  44. light_creation = models.TextField(verbose_name='Kind of light creation',blank = True,max_length = 900)
  45. resolution = models.TextField(verbose_name='Resolution and transmitting behaviour',blank = True,max_length = 900)
  46. pixel_distance = models.CharField(verbose_name='Pixel distance',blank = True,max_length = 300)
  47. luminance = models.TextField(verbose_name='Luminace',blank = True,max_length = 900)
  48. urban_situation = models.TextField(verbose_name='Urban situation',blank = True,max_length = 900)
  49. description_showreel = models.TextField(verbose_name='Description of showreel',blank = True,max_length = 900)
  50. def __iter__(self):
  51. for field in self._meta.fields:
  52. if (field.value_to_string(self) is not '-') and (field.value_to_string(self) is not 'N/A'):
  53. yield (field.verbose_name, field.value_to_string(self))
  54. class Interaction(models.Model):
  55. communtity = models.TextField(verbose_name='Community or communities involved',blank = True,max_length = 900)
  56. host = models.CharField(verbose_name='Host organization',blank = True,max_length = 900)
  57. legal_form = models.CharField(verbose_name='Legal form',blank=True,max_length = 900)
  58. issues = models.TextField(verbose_name='Issues addressed',blank = True,max_length = 900)
  59. impact = models.TextField(verbose_name='Impact',blank = True,max_length = 900)
  60. tools = models.TextField(verbose_name='Tools developed',blank = True,max_length = 900)
  61. tools_used = models.TextField(verbose_name='Tools used',blank = True,max_length = 900)
  62. next_steps = models.TextField(verbose_name='Next steps',blank = True,max_length = 900)
  63. def __iter__(self):
  64. for field in self._meta.fields:
  65. if (field.value_to_string(self) is not '-') and (field.value_to_string(self) is not 'N/A'):
  66. yield (field.verbose_name, field.value_to_string(self))
  67. class Contact(models.Model):
  68. user = models.OneToOneField(User, on_delete=models.CASCADE,blank=True)
  69. first_name = models.CharField(verbose_name="Vorname", max_length = 100)
  70. last_name = models.CharField(verbose_name="Nachname", max_length = 100)
  71. adress = models.CharField(verbose_name="Adresse", blank=True,max_length = 100)
  72. postcode = models.CharField(verbose_name="Postleitzahl", blank=True, max_length = 100)
  73. city = models.CharField(verbose_name="Stadt", blank=True,max_length = 100)
  74. country = models.CharField(verbose_name="Land", blank=True,max_length = 100)
  75. email = models.EmailField(verbose_name="E-Mail-Addresse", unique=True)
  76. phonenumber = models.CharField(verbose_name="1. Telefonnummer", blank=True,max_length = 100)
  77. alternate_phonenumber = models.CharField(verbose_name="2. Telefonnummer", blank = True,max_length = 100)
  78. skype_name = models.CharField(verbose_name="Skype", blank=True,max_length = 300)
  79. website = models.URLField(verbose_name="Website", blank=True)
  80. def __str__(self):
  81. return self.first_name + ' ' + self.last_name
  82. def __iter__(self):
  83. for field in self._meta.fields:
  84. yield (field.verbose_name, field.value_to_string(self))
  85. class Category(models.Model):
  86. name = models.CharField(max_length=100)
  87. short_name = models.CharField(max_length=10)
  88. def __str__(self):
  89. return self.name
  90. class Product(models.Model):
  91. STATUS = [
  92. ('ENT', 'Entwicklung'),
  93. ('BET', 'In Betrieb'),
  94. ('ABG', 'Abgeschlossen'),
  95. ]
  96. FREI = [
  97. ('JAJA', 'Ja, bitte melden'),
  98. ('WART', 'Nein, aber es gibt eine Warteliste'),
  99. ('NEIN', 'Nein und es gibt keine Warteliste'),
  100. ]
  101. RECHTSFORM = [
  102. ('GMBH', 'GmbH'),
  103. ('VERE', 'Verein'),
  104. ('ANDE', 'Adere'),
  105. ]
  106. ORT = [
  107. ('1010', '1., Innere Stadt'),
  108. ('1020', '2., Leopoldstadt'),
  109. ('1030', '3., Landstraße'),
  110. ('1040', '4., Wieden'),
  111. ('1050', '5., Margareten'),
  112. ('1060', '6., Mariahilf'),
  113. ('1070', '7., Neubau'),
  114. ('1080', '8., Josefstadt'),
  115. ('1090', '9., Alsergrund'),
  116. ('1100', '10., Favoriten'),
  117. ('1110', '11., Simmering'),
  118. ('1120', '12., Meidling'),
  119. ('1130', '13., Hietzing'),
  120. ('1140', '14., Penzing'),
  121. ('1150', '15., Rudolfsheim-Fünfhaus'),
  122. ('1160', '16., Ottakring'),
  123. ('1170', '17., Hernals'),
  124. ('1180', '18., Währing'),
  125. ('1190', '19., Döbling'),
  126. ('1200', '20., Brigittenau'),
  127. ('1210', '21., Floridsdorf'),
  128. ('1220', '22., Donaustadt'),
  129. ('1230', '23., Liesing'),
  130. ('GERM', 'Deutschland'),
  131. ('SCHW', 'Schweiz'),
  132. ('GERM', 'Germany'),
  133. ('GERM', 'Germany'),
  134. ('VORA', 'Vorarlberg'),
  135. ('TIRO', 'Tirol'),
  136. ('SALZ', 'Salzburg'),
  137. ('KAER', 'Kärnten'),
  138. ('STEI', 'Steiermark'),
  139. ('OBER', 'Oberösterreich'),
  140. ('NIER', 'Niederösterreich'),
  141. ('BURG', 'Burgenland'),
  142. ('WELT', 'Welt'),
  143. ]
  144. ORGANIZATION = [
  145. ('SOZ', 'Soziokratie'),
  146. ('SON', 'Sonstiges'),
  147. ]
  148. name = models.CharField( 'Name', max_length = 128, help_text="Name des Projekts", null = True, blank=True )
  149. claim = models.CharField( 'Claim / Untertitel', max_length = 256, help_text="Claim / Untertitel", null = True, blank=True )
  150. beschreibung = models.CharField( 'Beschreibung', max_length = 256, help_text="Beschreibung des Projektes", null = True, blank=True )
  151. learning = models.CharField( 'Learning', max_length = 256, help_text="Learning", null = True, blank=True )
  152. gruendungsjahr = models.IntegerField('Gründungsjahr', help_text="Gründungsjahr des Projekts", validators=[MinValueValidator(0), MaxValueValidator(9999)], null = True, blank=True )
  153. betriebgenommen = models.IntegerField('in Betrieb genommen in Jahr', help_text="Wann wurde das Projekt in Betrieb genommen?", validators=[MinValueValidator(0), MaxValueValidator(9999)], null = True, blank=True )
  154. status = models.CharField( 'Status', max_length = 3, choices=STATUS, default='ENT', help_text="Was ist der Status des Projektes" , null = True, blank=True )
  155. adresse = models.CharField( 'Adresse', max_length = 256, help_text="Adresse des Projektes", null = True, blank=True )
  156. website = models.URLField ( 'Website', max_length = 2048, help_text="Website des des Projektes", null = True, blank=True )
  157. frei = models.CharField( 'Platz frei', max_length = 4, choices=FREI, default='NEIN', help_text="Gibt es freie Plätze?", null = True, blank=True )
  158. mitmachen = models.BooleanField( 'Mitmachen möglich', default=False, help_text="Kann jemand mitmachen?", null = True, blank=True )
  159. rechtsform = models.CharField( 'Rechtsform', max_length = 4, choices=RECHTSFORM, default='ANDE', help_text="Rechtsform des Projektes", null = True, blank=True )
  160. ort = models.CharField( 'Ort', max_length = 4, choices=ORT, default='WELT', help_text="Ort des Projektes", null = True, blank=True )
  161. orga = models.CharField( 'Organisationsform', max_length = 3, choices=ORGANIZATION, default='SON', help_text="Organisationsform des Projektes", null = True, blank=True )
  162. title = models.CharField('Product Title',max_length = 100)
  163. country = CountryField(blank=True,multiple=True, help_text="")
  164. city = models.CharField(max_length = 100,null = True, blank=True, help_text="")
  165. year = models.IntegerField('Year of Completion',null = True, blank=True, help_text="")
  166. owner = models.CharField(max_length = 300,null = True,blank=True, help_text="")
  167. teaser_txt = models.TextField(blank = True, max_length = 1050, help_text="")
  168. header = models.CharField(max_length = 900,null = True, help_text="")
  169. description_txt = models.TextField(max_length = 3000,null = True, help_text="")
  170. cid = models.IntegerField(null = True,blank=True, help_text="")
  171. class_cid = models.CharField(null = True,max_length = 3, blank=True, help_text="")
  172. sumbitted = models.CharField(null = True,max_length = 10, blank=True, help_text="")
  173. date_submitted = models.DateField(auto_now_add=True, help_text="")
  174. terms = models.NullBooleanField(help_text="")
  175. edit = models.NullBooleanField(help_text="")
  176. public = models.NullBooleanField(help_text="")
  177. photo = models.CharField(max_length=500, null=True, blank=True, help_text="")
  178. videocts = models.CharField(max_length=500, null=True, blank=True, help_text="")
  179. category = models.ManyToManyField(Category, null=True, help_text="")
  180. credits = models.ForeignKey(Credit, null = True,on_delete=models.SET_NULL, help_text="")
  181. description = models.ForeignKey(Description,null = True, on_delete=models.SET_NULL, help_text="")
  182. contact = models.ForeignKey(Contact, null = True,on_delete=models.SET_NULL, help_text="")
  183. interaction = models.ForeignKey(Interaction, null = True,on_delete = models.SET_NULL, help_text="")
  184. def __str__(self):
  185. return str(self.title)
  186. def __iter__(self):
  187. for field in self._meta.fields:
  188. yield (field.verbose_name, field.value_to_string(self))
  189. class Wohnprojekt(Product):
  190. BAUWEISE = [
  191. ('HOLZ', 'Holzbau'),
  192. ('ALTB', 'Altbau'),
  193. ('NEUB', 'Neubau'),
  194. ]
  195. EIGENTUM = [
  196. ('GEMIET', 'Generalmiete'),
  197. ('WOHNHM', 'Wohnheim'),
  198. ('EIGENT', 'Eigentum'),
  199. ('GTMIET', 'Getrennte Mietvertäge (Verein mietet GR)'),
  200. ]
  201. UNTERST = [
  202. ('GEFOR', 'Gefördert'),
  203. ('NICHT', 'Nicht Gefördert'),
  204. ]
  205. anzahlmitglieder = models.IntegerField('Anzahl der Mitglieder', help_text="Anzahl der Mitglieder", null = True, blank=True )
  206. eigentum = models.CharField( 'Rechtliche Konstruktion', max_length = 6, choices=EIGENTUM, default='GEMIET', help_text="Rechtliche Konstruktion", null = True, blank=True )
  207. @receiver(post_delete, sender=Product)
  208. def auto_delete_reverse_keys(sender, instance, **kwargs):
  209. if instance.credits:
  210. instance.credits.delete()
  211. if instance.description:
  212. instance.description.delete()
  213. if instance.interaction:
  214. instance.interaction.delete()
  215. class Link(models.Model):
  216. Product = models.ForeignKey(Product, on_delete=models.CASCADE)
  217. link_description = models.CharField(null=True, blank=True, max_length = 2048)
  218. link = models.URLField(null=True, blank=True, max_length = 2048)
  219. def Product_path(instance, filename):
  220. return 'marktplatz/media/{0}/{1}'.format(instance.Product.id, filename)
  221. class Media(models.Model):
  222. fs = FileSystemStorage(location=settings.MEDIA_ROOT)
  223. Product = models.ForeignKey(Product, on_delete=models.CASCADE)
  224. name_for = models.CharField(blank = True,max_length = 256)
  225. filename = models.CharField(max_length = 100)
  226. copyright = models.CharField(blank=True, max_length = 100)
  227. image = models.ImageField(upload_to=Product_path,storage=fs)
  228. image_small = ProcessedImageField(upload_to=Product_path,
  229. processors=[ResizeToCover(640, 360)],
  230. format='JPEG',
  231. options={'quality': 90})
  232. image_medium= ProcessedImageField(upload_to=Product_path,
  233. processors=[ResizeToCover(960, 540)],
  234. format='JPEG',
  235. options={'quality': 90})
  236. image_big = ProcessedImageField(upload_to=Product_path,
  237. processors=[ResizeToCover(1920, 1080)],
  238. format='JPEG',
  239. options={'quality': 90})
  240. image_norm = ProcessedImageField(upload_to=Product_path,
  241. processors=[Thumbnail(640, 360)],
  242. format='JPEG',
  243. options={'quality': 90},
  244. blank = True,
  245. null = True)
  246. def filename(self):
  247. return os.path.basename(self.image.name).split('.')[0]
  248. # These two auto-delete files from filesystem when they are unneeded:
  249. #
  250. @receiver(models.signals.post_delete, sender=Media)
  251. def auto_delete_file_on_delete(sender, instance, **kwargs):
  252. """
  253. Deletes file from filesystem
  254. when corresponding `MediaFile` object is deleted.
  255. """
  256. if instance.image:
  257. if os.path.isfile(instance.image.path):
  258. os.remove(instance.image.path)
  259. #
  260. if instance.image_small:
  261. if os.path.isfile(instance.image_small.path):
  262. os.remove(instance.image_small.path)
  263. #
  264. if instance.image_medium:
  265. if os.path.isfile(instance.image_medium.path):
  266. os.remove(instance.image_medium.path)
  267. #
  268. if instance.image_big:
  269. if os.path.isfile(instance.image_big.path):
  270. os.remove(instance.image_big.path)
  271. #
  272. if instance.image_norm:
  273. if os.path.isfile(instance.image_norm.path):
  274. os.remove(instance.image_norm.path)
  275. #
  276. #print(settings.MEDIA_ROOT+'/marktplatz/media/{0}/{1}'.format(instance.Product.id, instance.image.name))
  277. #print(instance.Product.id, instance.image.path)
  278. #os.remove(settings.MEDIA_ROOT+'/marktplatz/media/{0}/{1}_big'.format(instance.Product.id, instance.filename))
  279. #os.remove(settings.MEDIA_ROOT+'/marktplatz/media/{0}/{1}.jpg'.format(instance.Product.id, instance.image.name))
  280. #@receiver(models.signals.pre_save, sender=Media)
  281. #def auto_delete_file_on_change(sender, instance, **kwargs):
  282. # """
  283. # Deletes old file from filesystem
  284. # when corresponding `MediaFile` object is updated
  285. # with new file.
  286. # """
  287. # if not instance.pk:
  288. # return False
  289. #
  290. # try:
  291. # old_file = Media.objects.get(pk=instance.pk).image
  292. # except Media.DoesNotExist:
  293. # return False
  294. #
  295. # new_file = instance.image
  296. # if not old_file == new_file:
  297. # if os.path.isfile(old_file.path):
  298. # os.remove(old_file.path)
  299. #
  300. # try:
  301. # old_file = Media.objects.get(pk=instance.pk).image_small
  302. # except Media.DoesNotExist:
  303. # return False
  304. #
  305. # new_file = instance.image_small
  306. # if not old_file == new_file:
  307. # if os.path.isfile(old_file.path):
  308. # os.remove(old_file.path)
  309. #
  310. # try:
  311. # old_file = Media.objects.get(pk=instance.pk).image_medium
  312. # except Media.DoesNotExist:
  313. # return False
  314. #
  315. # new_file = instance.image_medium
  316. # if not old_file == new_file:
  317. # if os.path.isfile(old_file.path):
  318. # os.remove(old_file.path)
  319. #
  320. # try:
  321. # old_file = Media.objects.get(pk=instance.pk).image_big
  322. # except Media.DoesNotExist:
  323. # return False
  324. #
  325. # new_file = instance.image_big
  326. # if not old_file == new_file:
  327. # if os.path.isfile(old_file.path):
  328. # os.remove(old_file.path)
  329. #
  330. # try:
  331. # old_file = Media.objects.get(pk=instance.pk).image_norm
  332. # except Media.DoesNotExist:
  333. # return False
  334. #
  335. # new_file = instance.image_norm
  336. # if not old_file == new_file:
  337. # if os.path.isfile(old_file.path):
  338. # os.remove(old_file.path)
  339. #
  340. class Video(models.Model):
  341. Product = models.ForeignKey(Product, on_delete=models.CASCADE)
  342. name_for = models.CharField(blank=True, max_length=256)
  343. filename = models.CharField(max_length=100)
  344. copyright = models.CharField(blank=True, max_length=100)
  345. image = models.FileField(upload_to=Product_path)
  346. @receiver(models.signals.post_delete, sender=Video)
  347. def auto_delete_video_on_delete(sender, instance, **kwargs):
  348. """
  349. Deletes file from filesystem
  350. when corresponding `MediaFile` object is deleted.
  351. """
  352. if instance.image:
  353. if os.path.isfile(instance.image.path):
  354. os.remove(instance.image.path)
  355. #@receiver(models.signals.pre_save, sender=Video)
  356. #def auto_delete_video_on_change(sender, instance, **kwargs):
  357. # """
  358. # Deletes old file from filesystem
  359. # when corresponding `MediaFile` object is updated
  360. # with new file.
  361. # """
  362. # if not instance.pk:
  363. # return False
  364. #
  365. # try:
  366. # old_file = Video.objects.get(pk=instance.pk).image
  367. # except Media.DoesNotExist:
  368. # return False
  369. #
  370. # new_file = instance.image
  371. # if not old_file == new_file:
  372. # if os.path.isfile(old_file.path):
  373. # os.remove(old_file.path)
  374. class Vote(models.Model):
  375. Product = models.ForeignKey(Product, on_delete=models.CASCADE)
  376. juryMember = models.ForeignKey(User, on_delete = models.CASCADE)
  377. vote = models.PositiveSmallIntegerField(default=0)
  378. comment = models.CharField(blank=True, max_length =300)
  379. def __iter__(self):
  380. for field in self._meta.fields:
  381. yield (field.verbose_name, field.value_to_string(self))