models.py 19 KB

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