models.py 13.4 KB
Newer Older
gijs's avatar
gijs committed
1
2
from django.db import models
from etherpadlite.models import Pad
gijs's avatar
gijs committed
3
from datetime import date, timedelta, datetime, time
gijs's avatar
gijs committed
4
5
from adminsortable.fields import SortableForeignKey
from adminsortable.models import SortableMixin
gijs's avatar
gijs committed
6
from collections import namedtuple
gijs's avatar
Typo  
gijs committed
7
from django.utils.translation import get_language as _get_language
8
9
from filer.fields.image import FilerImageField

gijs's avatar
gijs committed
10
11

GridCoordinates = namedtuple('GridCoordinates', ['start', 'span'])
gijs's avatar
gijs committed
12

gijs's avatar
?  
gijs committed
13
14
15
16
def get_language():
  code = _get_language()
  return code if code else 'en'

gijs's avatar
gijs committed
17
18
19
20
# Create your models here.
class Venue(models.Model):
  published = models.BooleanField(default=False)
  slug = models.SlugField(unique=True)
gijs's avatar
gijs committed
21
  name_nl = models.CharField(max_length=255)
gijs's avatar
gijs committed
22
  name_en = models.CharField(max_length=255,  verbose_name="Name (English)")
gijs's avatar
gijs committed
23
  notes_nl = models.TextField(null=True, blank=True)
gijs's avatar
gijs committed
24
  notes_en = models.TextField(verbose_name="Notes (english)", null=True, blank=True)
gijs's avatar
gijs committed
25
  address = models.TextField(null=True, blank=True)
gijs's avatar
gijs committed
26
27
  latitude = models.CharField(max_length=255, null=True, blank=True)
  longitude = models.CharField(max_length=255, null=True, blank=True)
gijs's avatar
gijs committed
28

gijs's avatar
gijs committed
29
  class Meta:
gijs's avatar
gijs committed
30
    ordering = ('name_nl',)
gijs's avatar
gijs committed
31

gijs's avatar
gijs committed
32
  def __str__ (self):
gijs's avatar
gijs committed
33
    return '{}'.format(self.name)
gijs's avatar
gijs committed
34

gijs's avatar
gijs committed
35
36
37
38
39
  def __getattr__ (self, attrname):
    if attrname in ['name', 'notes']:
      return getattr(self, attrname + '_' + get_language())
    
    return super().__getattr__(attrname)
gijs's avatar
gijs committed
40
41
42

class Image(models.Model):
  published = models.BooleanField(default=False)
gijs's avatar
gijs committed
43
  oldImage = models.ImageField(upload_to="images", null=True, blank=True)
gijs's avatar
gijs committed
44
  image = FilerImageField(null=True, blank=True, on_delete=models.CASCADE)
gijs's avatar
gijs committed
45
46
  title_nl = models.CharField(max_length=255)
  tagline_nl = models.CharField(max_length=255, null=True, blank=True)
gijs's avatar
gijs committed
47
48
  title_en = models.CharField(max_length=255, verbose_name="Title (English)", null=True, blank=True)
  tagline_en = models.CharField(max_length=255, verbose_name="Tagline (English)", null=True, blank=True)
gijs's avatar
gijs committed
49
  credit_nl = models.CharField(max_length=255, null=True, blank=True)
50
  credit_en = models.CharField(max_length=255, verbose_name="Credits (English)", null=True, blank=True)
gijs's avatar
gijs committed
51
52

  def __str__ (self):
gijs's avatar
gijs committed
53
    return '{}'.format(self.title)
gijs's avatar
gijs committed
54

gijs's avatar
gijs committed
55
56
57
58
  def __getattr__ (self, attrname):
    if attrname in ['title', 'tagline', 'credit']:
      return getattr(self, attrname + '_' + get_language())
    
59
    return super(Image, self).__getattr__(attrname)
gijs's avatar
gijs committed
60

61
62
63
64
65
66
# class ImageCredit(models.Model):
#   title = models.CharField(max_length=255, verbose_name="Credit Title")
#   value = models.CharField(max_length=255, verbose_name="Credit Value")
#   title_en = models.CharField(max_length=255, verbose_name="Credit Title (English)", null=True, blank=True)
#   value_en = models.CharField(max_length=255, verbose_name="Credit Value (English)", null=True, blank=True)
#   image = models.ForeignKey(Image, on_delete=models.CASCADE)
gijs's avatar
gijs committed
67

68
69
#   def __str__ (self):
#     return self.title
gijs's avatar
gijs committed
70
71
72
73
74


class Participant(models.Model):
  slug = models.SlugField(unique=True, max_length=255)
  published = models.BooleanField(default=False)
gijs's avatar
gijs committed
75
  projects = models.ManyToManyField('Project', through='ProjectParticipant')
gijs's avatar
gijs committed
76
77
78
  name_nl = models.CharField(max_length=255)
  sort_name_nl = models.CharField(max_length=255, null=True, blank=True)
  bio_nl = models.TextField(verbose_name="Biography")
gijs's avatar
gijs committed
79
80
81
  name_en = models.CharField(max_length=255, verbose_name="Name (English)", null=True, blank=True)
  sort_name_en = models.CharField(max_length=255, verbose_name="Sortname (English)", null=True, blank=True)
  bio_en = models.TextField(verbose_name="Biography (English)", null=True, blank=True)
gijs's avatar
gijs committed
82
83
 
  class Meta:
84
    ordering = ('sort_name_nl',)
gijs's avatar
gijs committed
85
  
gijs's avatar
gijs committed
86
  def __str__ (self):
gijs's avatar
gijs committed
87
    return '{}'.format(self.name)
gijs's avatar
gijs committed
88

gijs's avatar
gijs committed
89
90
91
92
93
  def __getattr__ (self, attrname):
    if attrname in ['name', 'sort_name', 'bio']:
      return getattr(self, attrname + '_' + get_language())
    
    return super().__getattr__(attrname)
gijs's avatar
gijs committed
94
95
96
97
98
99
100

class Project(models.Model):
  slug = models.SlugField(unique=True, max_length=255)
  published = models.BooleanField(default=False)
  participants = models.ManyToManyField(Participant, through='ProjectParticipant')
  pads = models.ManyToManyField(Pad, through='ProjectPad')
  images = models.ManyToManyField(Image, through='ProjectImage')
gijs's avatar
gijs committed
101
102
103
  title_nl = models.CharField(max_length=255, verbose_name="Title (Dutch)")
  summary_nl = models.TextField(null=True, blank=True, verbose_name="Summary (Dutch)")
  description_nl = models.TextField(verbose_name="Description (Dutch)")
gijs's avatar
gijs committed
104
105
106
107
108
  title_en = models.CharField(max_length=255, verbose_name="Title (English)", null=True, blank=True)
  summary_en = models.TextField(verbose_name="Summary (English)", null=True, blank=True)
  description_en = models.TextField(verbose_name="Description (English)", null=True, blank=True)

  def __str__ (self):
gijs's avatar
gijs committed
109
    return '{}'.format(self.title)
gijs's avatar
gijs committed
110

gijs's avatar
gijs committed
111
112
113
114
115
  def __getattr__ (self, attrname):
    if attrname in ['title', 'summary', 'description']:
      return getattr(self, attrname + '_' + get_language())
    
    return super().__getattr__(attrname)
gijs's avatar
gijs committed
116

gijs's avatar
gijs committed
117
118
class ProjectCredit(models.Model):
  project = models.ForeignKey(Project, on_delete=models.CASCADE)
gijs's avatar
gijs committed
119
120
  title_nl = models.CharField(max_length=255, verbose_name="Credit Title")
  value_nl = models.CharField(max_length=255, verbose_name="Credit Value")
gijs's avatar
gijs committed
121
122
123
  title_en = models.CharField(max_length=255, verbose_name="Credit Title (English)", null=True, blank=True)
  value_en = models.CharField(max_length=255, verbose_name="Credit Value (English)", null=True, blank=True)

gijs's avatar
gijs committed
124
125
126
127
128
129
130
131
132
  def __str__ (self):
    return '{}: {}'.format(self.title, self.value)

  def __getattr__ (self, attrname):
    if attrname in ['title', 'value']:
      return getattr(self, attrname + '_' + get_language())

    return super().__getattr__(attrname)

gijs's avatar
gijs committed
133
class EventType(models.Model):
gijs's avatar
gijs committed
134
135
136
  class Meta:
    ordering: ['label']

gijs's avatar
gijs committed
137
  label_nl = models.CharField(max_length=255)
gijs's avatar
gijs committed
138
139
140
  label_en = models.CharField(max_length=255, verbose_name="Label (english)", null=True, blank=True)

  def __str__ (self):
gijs's avatar
gijs committed
141
   return '{}'.format(self.label)
gijs's avatar
gijs committed
142

gijs's avatar
gijs committed
143
144
145
146
147
  def __getattr__ (self, attrname):
    if attrname in ['label']:
      return getattr(self, attrname + '_' + get_language())
    
    return super().__getattr__(attrname)
gijs's avatar
gijs committed
148
149

class Event(models.Model):
gijs's avatar
gijs committed
150
151
152
  class Meta:
    ordering: ['start']
  
153
154
  published = models.BooleanField(default=True)
  continuous = models.BooleanField(default=False)
gijs's avatar
gijs committed
155
  project = models.ForeignKey(Project, on_delete=models.CASCADE)
gijs's avatar
gijs committed
156
157
  event_type = models.ForeignKey(EventType, on_delete=models.SET_NULL, null=True)
  venue = models.ForeignKey(Venue, on_delete=models.SET_NULL, null=True)
gijs's avatar
gijs committed
158
  notes_nl = models.TextField(null=True, blank=True)
gijs's avatar
gijs committed
159
160
161
  notes_en = models.TextField(verbose_name="Notes (English)", null=True, blank=True)
  start = models.DateTimeField()
  end = models.DateTimeField(null=True, blank=True)
gijs's avatar
gijs committed
162
163
164
  show_ticket_link = models.BooleanField(default=True, verbose_name="Tickets sale open")
  ticket_link = models.URLField(null=True, blank=True, verbose_name="Ticket link")
  sold_out = models.BooleanField(default=False, verbose_name="Event is sold out")
165
166
167
168

  @property
  def grid_coord (self):
    return self.__grid_coord
gijs's avatar
gijs committed
169
  
gijs's avatar
gijs committed
170
171
172
173
174
175
176
177
  @property
  def grid_coordinates(self):
    if not hasattr(self, '__grid_coordinates'):
      resolution = 30 * 60
      daystart = time(9)

      gridstart = (self.start - datetime.combine(self.start.date(), daystart, tzinfo=self.start.tzinfo)).total_seconds() / resolution
      gridspan = max((self.end - self.start).total_seconds() / resolution, 1) if self.end else 1
178
      self.__grid_coordinates = GridCoordinates(start=max(2, int(gridstart)), span=int(gridspan))
gijs's avatar
gijs committed
179
180
181

    return self.__grid_coordinates

182
183
184
185
186
  def set_grid_coord (self, minimum):
    resolution = 30 * 60
    daystart = time(9)
    self.__grid_coord = int(max(minimum, (self.start - datetime.combine(self.start.date(), daystart, tzinfo=self.start.tzinfo)).total_seconds() / resolution))
    return self.__grid_coord
gijs's avatar
gijs committed
187

gijs's avatar
gijs committed
188
189
  def __str__ (self):
    # @FIXME: more elegant?
gijs's avatar
gijs committed
190
    return str(self.start)
gijs's avatar
gijs committed
191

gijs's avatar
gijs committed
192
193
194
195
196
197
  def __getattr__ (self, attrname):
    if attrname in ['notes']:
      return getattr(self, attrname + '_' + get_language())
    
    return super().__getattr__(attrname)

198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

class DocumentationImage(models.Model):
  class Meta:
    ordering = ('position',)

  def __str__ (self):
    return self.image.__str__()

  event = models.ForeignKey(Event, on_delete=models.CASCADE, null=True, blank=True, related_name='documentation_image')
  image = FilerImageField(on_delete=models.CASCADE)
  position = models.PositiveIntegerField(default=0, db_index=True)



class ProjectParticipant(models.Model):
gijs's avatar
gijs committed
213
  class Meta:
214
    ordering = ('position',)
gijs's avatar
gijs committed
215

gijs's avatar
gijs committed
216
217
218
  def __str__ (self):
    return self.participant.__str__()

gijs's avatar
gijs committed
219
220
  participant = models.ForeignKey(Participant, on_delete=models.CASCADE)
  project = SortableForeignKey(Project, on_delete=models.CASCADE)
gijs's avatar
gijs committed
221
  role_nl = models.CharField(max_length=255)
gijs's avatar
gijs committed
222
  role_en = models.CharField(max_length=255, verbose_name="Role (English)")
223
  position = models.PositiveIntegerField(default=0, editable=True, db_index=True)
gijs's avatar
gijs committed
224

225
226
227
228
229
230
  def __getattr__ (self, attrname):
    if attrname in ['role', 'title', 'content']:
      return getattr(self, attrname + '_' + get_language())
    
    return super().__getattr__(attrname)

gijs's avatar
gijs committed
231
232
233
# class ParticipantImage(SortableMixin):
#   class Meta:
#     ordering = ('image_order',)
gijs's avatar
gijs committed
234

gijs's avatar
gijs committed
235
236
237
#   participant = SortableForeignKey(Participant, on_delete=models.PROTECT)
#   image = models.ForeignKey(Image, on_delete=models.PROTECT)
#   image_order = models.PositiveIntegerField(default=0, editable=False, db_index=True)
gijs's avatar
gijs committed
238

gijs's avatar
gijs committed
239
240
#   def __str__ (self):
#     return self.image.title
gijs's avatar
gijs committed
241

242
class ProjectImage(models.Model):
gijs's avatar
gijs committed
243
  class Meta:
244
    ordering = ('position',)
gijs's avatar
gijs committed
245

gijs's avatar
gijs committed
246
247
  project = SortableForeignKey(Project, on_delete=models.CASCADE)
  image = models.ForeignKey(Image, on_delete=models.CASCADE)
248
  position = models.PositiveIntegerField(default=0, editable=True, db_index=True)
gijs's avatar
gijs committed
249
250
  cover_image = models.BooleanField(default=True)
  
gijs's avatar
gijs committed
251
  def __str__ (self):
gijs's avatar
gijs committed
252
    return self.image.__str__()
gijs's avatar
gijs committed
253

gijs's avatar
gijs committed
254

gijs's avatar
gijs committed
255
class ProjectPad(models.Model):
gijs's avatar
gijs committed
256
257
  project = models.ForeignKey(Project, on_delete=models.CASCADE)
  pad = models.ForeignKey(Pad, on_delete=models.CASCADE)
258
  position = models.PositiveIntegerField(default=0, editable=True, db_index=True)
gijs's avatar
gijs committed
259
  
260
261
262
263
264
265
  def __getattr__ (self, attrname):
    if attrname in ['label']:
      return getattr(self, attrname + '_' + get_language())
    
    return super().__getattr__(attrname)

gijs's avatar
gijs committed
266
  def __str__ (self):
gijs's avatar
gijs committed
267
    return self.pad.__str__()
gijs's avatar
gijs committed
268

269

gijs's avatar
gijs committed
270
class Page(models.Model):
gijs's avatar
gijs committed
271
272
273
274
  class Meta:
    ordering = ['position']

  position = models.PositiveIntegerField(default=0, editable=True, db_index=True)
gijs's avatar
gijs committed
275
276
  slug_nl = models.SlugField(unique=True, max_length=255, verbose_name="Slug (Dutch)")
  slug_en = models.SlugField(unique=True, max_length=255, verbose_name="Slug (English)")
gijs's avatar
gijs committed
277
  published = models.BooleanField(default=False)
gijs's avatar
gijs committed
278
279
280
  title_nl = models.CharField(max_length=255, verbose_name="Title (Dutch)")
  summary_nl = models.TextField(null=True, blank=True, verbose_name="Summary (Dutch) shown on parent page")
  content_nl = models.TextField(verbose_name="Content (Dutch)")
gijs's avatar
gijs committed
281
  title_en = models.CharField(max_length=255, verbose_name="Title (English)", null=True, blank=True)
gijs's avatar
gijs committed
282
  summary_en = models.TextField(null=True, blank=True, verbose_name="Summary (English) shown on parent page")
gijs's avatar
gijs committed
283
  content_en = models.TextField(verbose_name="Content (English)", null=True, blank=True)
gijs's avatar
gijs committed
284
  parent = models.ForeignKey('Page', on_delete=models.SET_NULL, related_name='subpage', null=True, blank=True)
gijs's avatar
gijs committed
285
286

  def __str__ (self):
gijs's avatar
gijs committed
287
    return '{}'.format(self.title)
gijs's avatar
gijs committed
288

gijs's avatar
gijs committed
289
  def __getattr__ (self, attrname):
290
    if attrname in ['slug', 'title', 'summary', 'content']:
gijs's avatar
gijs committed
291
292
293
294
      return getattr(self, attrname + '_' + get_language())
    
    return super().__getattr__(attrname)

295
296
297
298
299
300
301
302
303
304
305
306
307

class PageImage(models.Model):
  class Meta:
    ordering = ('position',)

  def __str__ (self):
    return self.image.__str__()

  page = models.ForeignKey(Page, on_delete=models.CASCADE, null=True, blank=True, related_name='image')
  image = FilerImageField(on_delete=models.CASCADE)
  position = models.PositiveIntegerField(default=0, db_index=True)


gijs's avatar
gijs committed
308
class Loop(models.Model):
gijs's avatar
gijs committed
309
310
311
  class Meta:
    ordering = ['start']

gijs's avatar
gijs committed
312
313
  name_nl = models.CharField(max_length=255)
  name_en = models.CharField(max_length=255)
314
315
  timeline_label_nl = models.CharField(max_length=255)
  timeline_label_en = models.CharField(max_length=255)
gijs's avatar
gijs committed
316
317
  start = models.DateField()
  end = models.DateField()
gijs's avatar
gijs committed
318
  published = models.BooleanField(default=False)
gijs's avatar
gijs committed
319
320
  description_nl = models.TextField(verbose_name="Description (Dutch)", null=True, blank=True)
  description_en = models.TextField(verbose_name="Description (English)", null=True, blank=True)
gijs's avatar
gijs committed
321
  page = models.ForeignKey(Page, on_delete=models.SET_NULL, related_name='loop', null=True, blank=True)
gijs's avatar
gijs committed
322
323

  def __str__ (self):
gijs's avatar
gijs committed
324
    return '{}'.format(self.name)
gijs's avatar
gijs committed
325
326

  def __getattr__ (self, attrname):
327
    if attrname in ['name', 'description', 'timeline_label']:
gijs's avatar
gijs committed
328
329
330
331
      return getattr(self, attrname + '_' + get_language())
    
    return super().__getattr__(attrname)
    
gijs's avatar
gijs committed
332
333
334
335
336
337

class MenuItem(SortableMixin):
  class Meta:
    ordering = ['item_order']

  def __str__ (self):
gijs's avatar
gijs committed
338
    return '{}'.format(self.label)
gijs's avatar
gijs committed
339
  
gijs's avatar
gijs committed
340
  label_nl = models.CharField(max_length=255, verbose_name="Menu Label")
gijs's avatar
gijs committed
341
  label_en = models.CharField(max_length=255, verbose_name="Menu Label (English)")
gijs's avatar
gijs committed
342
  url_nl = models.CharField(max_length=255, verbose_name="URL")
gijs's avatar
gijs committed
343
  url_en = models.CharField(max_length=255, verbose_name="URL (English)")
gijs's avatar
gijs committed
344
345
346
347
348
349
350
  item_order = models.PositiveIntegerField(default=0, editable=False, db_index=True)

  def __getattr__ (self, attrname):
    if attrname in ['label', 'url']:
      return getattr(self, attrname + '_' + get_language())
    
    return super().__getattr__(attrname)