Created
February 14, 2013 19:54
-
-
Save particledecay/94f7f273808a538a2341 to your computer and use it in GitHub Desktop.
[Django] Simple task class and functions for adding/removing three types of tasks (tasks are for notifying the administrator that there is something to be done, and should be tied to an event and optionally an event registration object).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Task(models.Model): | |
| REGISTRATION = 0 | |
| ATTENDANCE = 1 | |
| CLE = 2 | |
| TASK_TYPES = ( | |
| (REGISTRATION, 'Registration Request'), | |
| (ATTENDANCE, 'Attendance Request'), | |
| (CLE, 'CLE Request'), | |
| ) | |
| task_type = models.PositiveSmallIntegerField(max_length=1, choices=TASK_TYPES, default=0) | |
| event = models.ForeignKey(Event) | |
| registration = models.ForeignKey(EventRegistration, blank=True, null=True) | |
| def __unicode__(self): | |
| return "{0} - {1}".format(self.event.title, self.TASK_TYPES[self.task_type][1]) | |
| # For registering actions that should trigger a task for the Firm Admin | |
| def add_task(task_type, event, registration): | |
| try: | |
| Event.objects.get(pk=event.pk) | |
| except Event.DoesNotExist: | |
| return False | |
| if task_type == Task.REGISTRATION or task_type == Task.ATTENDANCE or task_type == Task.CLE: | |
| try: | |
| Task.objects.get(Q(task_type=task_type) & Q(registration=registration) & Q(event=event)) | |
| except Task.DoesNotExist: | |
| Task(task_type=task_type, event=event, registration=registration).save() | |
| return True | |
| else: | |
| return False | |
| # For clearing out tasks that have been completed | |
| def remove_task(task_type, event, registration): | |
| try: | |
| Event.objects.get(pk=event.pk) | |
| except Event.DoesNotExist: | |
| return False | |
| if task_type == Task.REGISTRATION or task_type == Task.ATTENDANCE or task_type == Task.CLE: | |
| try: | |
| task = Task.objects.get(Q(task_type=task_type) & Q(registration=registration) & Q(event=event)) | |
| task.delete() | |
| return True | |
| except Task.DoesNotExist: | |
| return False | |
| else: | |
| return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment