Source

fossconf / web / models.py

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
from django.db import models
from django.contrib.auth.models import User, Group
from django.utils.translation import ugettext_lazy as _
from datetime import datetime
from tagging.models import Tag
from tagging.fields import TagField
from django.conf import settings
from django.db.models.signals import post_save
from django.core.mail import send_mail,EmailMessage
from django.contrib.sites.models import Site
import twitter
# Create your models here.

__all__ = ['Volunteertask', 'Sponsortype', 'Category', 'Occupation',
           'Country', 'Reporter', 'Menu', 'Staticpage', 'State',
           'City', 'Article', 'Sponsor', 'Link', 'Delegate', 'Talk',
           'Tempreg', 'Hall', 'Volunteer', 'Requesttalk', 'Talkrating',
           'Day', 'Slot', 'Tag_Colour', 'levels', 'Defaultimage','Menuhead',
           'Menuitem']

levels=(
    ('B', _("Beginner")),
    ('M', _("Moderate")),
    ('A', _("Advanced")),
    )

TEE_SIZES=(
    ('SM', _("Small")),
    ('ME', _("Medium")),
    ('LA', _("Large")),
    ('X1', _("XL")),
    ('X2', _("XXL")),
    ('X3', _("XXXL")),
    )

rating_list=(
    ('+1', _("Accept")),
    ('0', _("No Comment")),
    ('-1', _("Reject")),
    )




# foreign keys 
class Volunteertask(models.Model):
    """ To store the volunteertasks"""
    name = models.CharField(_("Task"),max_length=100,unique=True)

    class Meta:
        verbose_name_plural = "Volunteer Tasks"

    def __unicode__(self):
        return self.name

        
class Sponsortype(models.Model):
    """ To store the sponsortypes"""
    name = models.CharField(_("Sponsor type"),max_length=100,unique=True)
    priority = models.IntegerField(_("Priority"),default=1)
    class Meta:
        verbose_name_plural = "Sponsor Types"

    def __unicode__(self):
        return self.name


class Category(models.Model): 
    """talk categories"""
    name = models.CharField(_("Talk Category"),max_length=100,unique=True)

    class Meta:
        verbose_name_plural = 'Talk Categories'

    def __unicode__(self):
        return self.name

        
class Occupation(models.Model):   
    name = models.CharField(_("Occupation"),max_length=100,unique=True)

    def __unicode__(self):
        return self.name

        
class Country(models.Model):   
    name = models.CharField(_("Country"),max_length=100,unique=True)

    class Meta:
        verbose_name_plural = 'Countries'

    def __unicode__(self):
        return self.name

        
class Reporter(models.Model):   
    name = models.CharField(_("Name"),max_length=100,unique=True)

    class Meta:
        verbose_name_plural = 'Reporters'

    def __unicode__(self):
        return self.name

class Menuitem(models.Model):
    """this is to control order and off/on for other menu items
        """
    name = models.CharField(_("Menu name"),max_length=100,unique=True,editable=False)
    priority = models.IntegerField(_("Priority"),default=1)
    active = models.BooleanField(_("Active?"),default=False)
    class Meta:
        verbose_name_plural = 'Menuitems'

    def __unicode__(self):
        return self.name.capitalize()
        
class Menuhead(models.Model): 
    """ this is the head for static page menus"""
    name = models.CharField(_("Head name"),max_length=100,unique=True)
    priority = models.IntegerField(_("Priority"),default=1)
    active = models.BooleanField(_("Active?"),default=False)
    class Meta:
        verbose_name_plural = 'Menuheads'

    def __unicode__(self):
        return self.name.capitalize()

        
class Menu(models.Model):
    """Menu for static page menus"""
    menuhead = models.ForeignKey(Menuhead,verbose_name=_("Menu head"))   
    name = models.CharField(_("Menu item"),max_length=100,unique=True)
    priority = models.IntegerField(_("Priority"),default=1)
    active = models.BooleanField(_("Active?"),default=False)
    class Meta:
        verbose_name_plural = 'Menus'

    def __unicode__(self):
        return self.name.capitalize()

        
class Staticpage(models.Model):
    """static pages - one per menu"""
    menu = models.ForeignKey(Menu,unique = True,verbose_name=_("Menu"))
    matter    = models.TextField(_("Matter"))

    class Meta:
        verbose_name_plural = "Static Pages"

    def __unicode__(self):
        return self.menu.name


class State(models.Model):
    """Names of states to be used"""
    name    = models.CharField(_("State"),max_length=50,unique=True)
    country = models.ForeignKey(Country,blank=True,null=True,related_name="country",
                                default=_("India"),verbose_name=_("Country"))

    def __unicode__(self):
        return self.name


class City(models.Model):
    """Names of cities to be used"""
    name  = models.CharField(_("City"),max_length=50,unique=True)
    state = models.ForeignKey(State,blank=True,null=True,
                              related_name="state",verbose_name=_("State"))
    class Meta:
        verbose_name_plural = 'Cities'

    def __unicode__(self):
        return "%s : %s" %(self.name,self.state)
        
class Article(models.Model):
    title = models.CharField(_("Title"),max_length=250)
    pubdate = models.DateTimeField(_("Date"),default=datetime.now)
    expires = models.DateField(_("Expiry Date"),blank=True,null=True)
    author = models.ForeignKey(Reporter)
    lead = models.CharField(_("Lead"),max_length=250)
    photo = models.ImageField(_("Photo"),upload_to="images/misc/",
                              blank=True,null=True)
    matter = models.TextField(_("Matter"),blank=True,null=True)
    
    class Meta:
        unique_together = (("title","pubdate"),)
        
    def get_absolute_url(self):
        return u"/postsfull/%d/" %(self.id)

    def __unicode__(self):
        return _(u"%s written by Author: %s on Date: %s") %(self.title.capitalize(),
                                                            self.author,self.pubdate)


class Sponsor(models.Model):
    """Names of sponsors"""
    name  = models.CharField(_("Sponsor name"),max_length=50,unique=True)
    logo  = models.ImageField(_("Logo"),upload_to='images/pics')
    address = models.URLField(_("Url"))
    kind = models.ForeignKey(Sponsortype,verbose_name=_("Sponsor type"))
    priority = models.IntegerField(_("Priority"),default=1)

    class Meta:
        verbose_name_plural = 'Sponsors'

    def __unicode__(self):
        return "%s : %s" %(self.name,self.priority)
        
class Link(models.Model):
    """links - name and url"""
    name  = models.CharField(_("link name"),max_length=50,unique=True)
    address = models.URLField(_("Url"))
    priority = models.IntegerField(_("Priority"))

    class Meta:
        verbose_name_plural = 'Links'
        ordering = ['priority']

    def __unicode__(self):
        return "%s : %s" %(self.name,self.priority)


class Delegate(models.Model):
    username = models.ForeignKey(User, related_name = "delegate_user_name",
                                   unique = True,verbose_name = _("User Name"))
    bio = models.TextField(_("Bio"),blank = True,null = True)
    organisation = models.CharField(_("Organisation"),max_length=200,blank = True,null = True)
    city = models.CharField(_("Town or city"),max_length=100,blank = True,null = True)
    occupation = models.ForeignKey(Occupation,blank = True,null = True,
                                   related_name = "occupation", verbose_name = _("Occupation"))
    photo = models.ImageField(_("Photo"), upload_to="delphotos/", blank=True, null=True)
    joindate = models.DateTimeField(_("Date of registration"),default=datetime.now,
                                    editable=False)
    private = models.NullBooleanField(_("Keep my email private"),blank=True)
    teesize = models.CharField(_("Tee shirt size"),choices = TEE_SIZES,
                        max_length=2,blank=True,null=True)

    def __unicode__(self):
        return User.objects.get(username=self.username.username).get_full_name()

    def get_absolute_url(self):
        return u"/delegatefull/%d/" %(self.id)


class Defaultimage(models.Model):
    name = models.CharField(_("Name"),max_length=100)
    photo   = models.ImageField(_("Photo"), upload_to="delphotos/")

    class Meta:
        verbose_name_plural = "Default delegate photos"

    def __unicode__(self):
        return self.name


class Talk(models.Model):
    title     = models.CharField(_("Title"),max_length=250,unique=True)
    speaker   = models.ManyToManyField(Delegate, related_name="speakers", verbose_name=_("Speakers"))
    abstract  = models.TextField(_("Abstract"))
    slides    = models.FileField(_("Slides"),upload_to="slides/",
                                 blank=True,null=True)
    category  = models.ForeignKey(Category,related_name="category", verbose_name=_("Category"))
    talk_tags = TagField(max_length=255)
    level     = models.CharField(_("Level"),max_length=1,choices=levels)
    accepted  = models.BooleanField(_("Accepted"),default=False)
    scheduled = models.BooleanField(_("Scheduled"),default=False)

    def __unicode__(self):
            return "%s : %s" %(self.title, self.category)

    def save(self, force_insert=False, force_update=False):
        self.talk_tags = ' '.join([tag.capitalize() for tag in self.talk_tags.split(' ')])
        super(Talk, self).save()
        self.tags = self.talk_tags

    def display_tags(self):
        lst = self.talk_tags.split()
        return ' '.join([x.capitalize() for x in lst])

    def get_colour(self):
        """if colour given for first tag, return it else return default grey"""
        lst = self.talk_tags.split()
        try:
            colour = Tag_Colour.objects.get(tag=lst[0].strip(',')).colour
            return colour
        except:
            colour = '#CCCCCC'
            return colour
            


    def _get_tags(self):
        return Tag.objects.get_for_object(self)
    
    def _set_tags(self, talk_tags):
        Tag.objects.update_tags(self, talk_tags)

    tags = property(_get_tags, _set_tags)

    def get_absolute_url(self):
        return "/talkfull/%d/" %(self.id)

    def getrating(self):
        """gets talk ratings for a given talk"""
        lst = Talkrating.objects.filter(talk=self)
        tot = len(lst)
        score = 0
        for rate in lst:
            score += int(rate.rating)
        if tot:
            return _("Score: %d from %d ratings") % (score,tot)
        else:
            return _("Not rated")

    def getschedule(self):
       try:
           slt = Slot.objects.get(talk = self)
           dy = slt.day.confdate
           tme = slt.talk_start_time
           sched = datetime(dy.year,dy.month,dy.day,tme.hour,tme.minute)
           return sched
       except:
           return datetime(2000,1,1)


class Tempreg(models.Model):
    username = models.CharField(_("User Name"),max_length=30,unique=True)
    email    = models.EmailField(_("Email Address"),unique=True)
    date     = models.DateField(_("Date"),default=datetime.now,editable=False)
    code     = models.CharField(_("Code"),max_length=100,blank=True,null=True,
                                editable=False,default='123')

    class Meta:
        verbose_name_plural = "Temporary Registrations"

    def __unicode__(self):
           return _("%s %s") %(self.username, self.email)
               

class Volunteer(models.Model):
    """To store the information of the volunteer and their tasks,availability"""
    name         = models.ForeignKey(Delegate,related_name="volunteer_name", 
                                     verbose_name = _("Volunteer Name"))
    skills       = models.TextField(_("Skills"))
    task         = models.ManyToManyField(Volunteertask,related_name="tasks",
                                          verbose_name = _("Volunteer Tasks"))
    availability = models.TextField(_("Availability"))

    def __unicode__(self):
        return "%s : %s" %(self.name,self.skills)

    def get_absolute_url(self):
        return u"/volunteers/"

        
class Hall(models.Model):
    name     = models.CharField(_("Hall Name"),max_length=100,unique=True)
    location = models.CharField(_("Location"),max_length=100,unique=True)
    capacity = models.IntegerField(_("Capacity"))

    def __unicode__(self):
        return "%s : %s" %(self.name,self.capacity)

        
class Requesttalk(models.Model):
    topic = models.TextField(_("Topic"))

    def __unicode__(self):
        return self.topic

    class Meta:
        verbose_name_plural = "Request Talks"

        
class Talkrating(models.Model):
    """Talk Ratings Module"""
    username =  models.ForeignKey(User, related_name="talk_rating_user_name", verbose_name=_("User Name"))
    talk     =  models.ForeignKey(Talk, related_name="rating_talk", verbose_name=_("Talk"))
    rating   =  models.CharField(_("Ratings"),max_length=3,choices=rating_list)

    class Meta:
        # a user can only rate one talk once
        unique_together = (("username","talk"),)
        verbose_name_plural = "Talk Ratings"

    def __unicode__(self):
        return "%s : %s" %(self.talk, self.rating)
    
        
class Day(models.Model):
    """Date of the conference"""
    confdate        = models.DateField(_("Date of the Conference"))
    conf_start_time = models.TimeField(_("Conference Start Time"))
    conf_end_time   = models.TimeField(_("Conference End Time"))

    def __unicode__(self):
        return self.confdate.strftime("%d-%B-%Y") 

    class Meta:
       ordering = ['confdate',]


class Slot(models.Model):
    """Slot inculdes the time of the talk and the hall to the particular date"""
    day             = models.ForeignKey(Day, related_name="conf_date", verbose_name=_("Conference Date"))
    talk_start_time = models.TimeField(_('Talk Start Time'))
    talk_end_time   = models.TimeField(_('Talk End Time'))
    hall            = models.ForeignKey(Hall, related_name="hall", verbose_name=_("Hall"))
    talk            = models.ForeignKey(Talk, related_name= "talk", verbose_name=_("Talk"),
                                        limit_choices_to = {'accepted': True, 'scheduled': False})

    def save(self, force_insert=False, force_update=False):
        self.talk.scheduled = True
        self.talk.save()
        super(Slot, self).save(force_insert, force_update)
    
    def __unicode__(self):
        return "%s on %s starting at %s at %s" %(self.talk.title,
                                                 self.day.confdate.strftime("%d %B,%Y"),
                                                 self.talk_start_time.strftime("%H:%M %p"),
                                                 self.hall.name)


class Tag_Colour(models.Model):
    tag    = TagField()
    colour = models.CharField(max_length=7, unique=True)

    class Meta:
        verbose_name_plural = "Tag Colours"

    def __unicode__(self):
        return "%s rendered in %s" %(self.tag, self.colour)
        
#signals

def tweetit(sender,**kwargs):
    """
        extract relevant info and tweet it
        """
    if kwargs['created']:
        t = twitter.Api(settings.TWITTER_USER, settings.TWITTER_PASSWORD)
        t.PostUpdate("New Talk: %s http://%s%s" %(kwargs['instance'],
                        Site.objects.get_current().domain,
                        kwargs['instance'].get_absolute_url()))
   

post_save.connect(tweetit, sender=Talk)