class Proposal(UserModel): order = models.IntegerField(default=0) title = models.CharField(max_length=256,null=True,blank=True) meeting_minutes = models.ForeignKey(MeetingMinutes) original = MarkDownField() ammended = MarkDownField(null=True,blank=True) __unicode__ = lambda self: "Proposal #%s: %s"%(self.order,self.title or "(UNNAMED)") final_text = property(lambda self: self.ammended or self.original) class Meta: ordering = ('order',)
class UserMembership(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL) voting_rights = models.BooleanField(default=False) suspended = models.BooleanField(default=False) photo = models.ForeignKey(Photo,null=True,blank=True) bio = MarkDownField(null=True,blank=True) api_key = models.CharField(max_length=32,default=rand32) _h = "A short description of what you do for the lab." by_line = models.CharField(max_length=50,null=True,blank=True,help_text=_h) name = lambda self: "%s %s"%(self.user.first_name,self.user.last_name) _h = "Uncheck this to stop all email correspondance from this website " _h += "(same as unchecking all the below items and any future notifications we add)." notify_global = models.BooleanField("Global Email Preference",default=True,help_text=_h) _h = "If checked, you will be emailed whenever someone replies to a comment you make on this site." notify_comments = models.BooleanField("Comment Response Email",default=True,help_text=_h) _h = "If checked, you will be emailed a reminder 24 hours before a class (that you've signed up for)." notify_classes = models.BooleanField("Class Reminder Email",default=True,help_text=_h) _h = "If checked, you will be emailed new class offerings (twice a month)." notify_sessions = models.BooleanField("New Course Email",default=True,help_text=_h) __unicode__ = lambda self: "%s's Membership"%self.user objects = UserMembershipManager() @property def all_subscriptions(self): return Subscription.objects.filter(user=self.user).order_by("created") @cached_method def get_photo(self): return self.photo or Photo.objects.get(pk=144) @cached_method def get_term_sessions(self): #! Needs to be separated by something now that term is depracated! term = Term.objects.all()[0] return [(term,Session.objects.filter(user=self.user))]
class Lab(PhotosMixin,OrderedModel): name = models.CharField(max_length=128) __unicode__ = lambda self: self.name get_admin_url = lambda self: "/admin/tool/lab/%s/"%self.id slug = property(lambda self: slugify(self.name)) photo = models.ForeignKey(Photo,null=True,blank=True) description = MarkDownField(null=True,blank=True) url = lambda self: reverse("lab_detail",args=[self.slug,self.id]) class Meta: ordering = ("order",)
class Question(models.Model): topic = models.ForeignKey(Topic) question = models.CharField(max_length=256) answer = MarkDownField(blank=True, null=True) order = models.IntegerField(default=9999999) created = models.DateField(auto_now_add=True) modified = models.DateField(auto_now=True) __unicode__ = lambda self: self.question class Meta: ordering = ('order', )
class MeetingMinutes(models.Model): date = models.DateField(default=datetime.date.today,unique=True) voters_present = models.ManyToManyField(settings.AUTH_USER_MODEL,blank=True) inactive_present = models.ManyToManyField(settings.AUTH_USER_MODEL,blank=True,related_name="meetings_inactive") nonvoters_present = models.ManyToManyField(settings.AUTH_USER_MODEL,blank=True,related_name="+") content = MarkDownField() _ht = "Used only when an exact list of members is unavailable (eg legacy minutes)" member_count = models.IntegerField(default=0,help_text=_ht) __unicode__ = lambda self: "Minutes: %s"%self.date get_absolute_url = lambda self: reverse('meeting_minutes',args=[str(self.date)]) def get_member_count(self): attrs = ['voters_present','inactive_present'] return sum([getattr(self,a).count() for a in attrs]) or self.member_count or "unknown" class Meta: ordering = ('-date',)
class Tool(ConsumablesMixin, PhotosMixin, OrderedModel): name = models.CharField(max_length=128) __unicode__ = lambda self: self.name get_admin_url = lambda self: "/admin/tool/tool/%s/" % self.id value = property(lambda self: self.pk) slug = property(lambda self: slugify(self.name)) lab = models.ForeignKey(Lab) make = models.CharField(max_length=64, null=True, blank=True) model = models.CharField(max_length=32, null=True, blank=True) description = MarkDownField(blank=True, null=True) est_price = models.FloatField(null=True, blank=True) links = lambda self: self.toollink_set.all() materials = models.ManyToManyField("thing.Material", blank=True) room = models.ForeignKey(Room, null=True, blank=True) get_absolute_url = lambda self: reverse("tool_detail", args=[self.slug, self.id]) functional = models.BooleanField(default=True) repair_date = models.DateField(null=True, blank=True) permission = models.ForeignKey("Permission", null=True, blank=True) get_status = lambda self: "Functional" if self.functional else "Non-functional" @property def choice_name(self): if self.room: return "(%s) %s" % (self.room.name, self.name) return self.name class Meta: ordering = ("lab", "order") # Abstract the next two! @cached_property def courses(self): ct_id = ContentType.objects.get(model="course").id tagged = list( TaggedTool.objects.filter(content_type__id=ct_id, tool=self)) return [t.content_object for t in tagged] @cached_property def ordered_courses(self): singles = [] groups = [] for criterion in self.permission.criteria.all(): if criterion.courses.count() == 1: singles.append(criterion.courses.all()[0]) elif criterion.courses.all(): groups.append(criterion.courses.all()) return singles, groups single_courses = property(lambda self: self.ordered_courses[0]) group_courses = property(lambda self: self.ordered_courses[1]) number_required_courses = property( lambda self: len(self.single_courses) + len(self.group_courses)) @cached_property def things(self): ct_id = ContentType.objects.get(model="thing").id tagged = list( TaggedTool.objects.filter(content_type__id=ct_id, tool=self)) return [t.content_object for t in tagged] @property def as_json(self): fields = ['id', 'name'] return {field: getattr(self, field) for field in fields}