[docs]defthis_year(self):"""Get enrichment activities created during this school year. Returns: Enrichment activities created during this school year. """start_date,end_date=get_date_range_this_year()returnself.filter(added__gte=start_date,added__lte=end_date)
[docs]defvisible_to_user(self,*args):# , user):"""Get a list of visible enrichment activities for a given user (usually request.user). TODO: implement this method. Add group restrictions. """returnEnrichmentActivity.objects.all().this_year()
[docs]classEnrichmentActivity(models.Model):"""An enrichment activity available to the TJ community."""objects=EnrichmentActivityManager()title=models.CharField(max_length=100)description=models.TextField(max_length=10000)added=models.DateTimeField(auto_now=True)time=models.DateTimeField(blank=True,null=True)location=models.CharField(max_length=300)user=models.ForeignKey(settings.AUTH_USER_MODEL,null=True,on_delete=set_historical_user)capacity=models.SmallIntegerField(default=28)attending=models.ManyToManyField(settings.AUTH_USER_MODEL,blank=True,related_name="enrichments_attending")attended=models.ManyToManyField(settings.AUTH_USER_MODEL,blank=True,related_name="enrichments_attended")attendance_taken=models.BooleanField(default=False)presign=models.BooleanField(default=False)groups_allowed=models.ManyToManyField(DjangoGroup,related_name="allowed_enrichments_set",blank=True)groups_blacklisted=models.ManyToManyField(DjangoGroup,related_name="blacklisted_enrichments_set",blank=True)
[docs]defuser_can_signup(self,user):"""Return whether a user can sign up for an enrichment activity. Args: user: The user to check. Returns: Whether the user can sign up for the enrichment activity. """return(notself.groups_allowed.exists()orany(groupinuser.groups.all()forgroupinself.groups_allowed.all()))andnotany(groupinuser.groups.all()forgroupinself.groups_blacklisted.all())
defuser_is_blacklisted(self,user):returnany(groupinuser.groups.all()forgroupinself.groups_blacklisted.all())@propertydefis_this_year(self):"""Return whether the enrichment activity was created after the start of the school year."""returnis_current_year(self.added)@propertydefhappened(self):"""Return whether an enrichment activity has happened."""returnself.time<timezone.now()@propertydefrestricted(self):returnself.groups_allowed.exists()@propertydefis_too_early_to_signup(self):"""Returns whether it is too early to sign up for the activity if it is a presign. This contains the 2 day presign logic. Returns: Whether it is too early to sign up for this scheduled activity and when the activity opens for signups. """now=timezone.localtime()# Midnight of the day of the activityactivity_date=self.time.astimezone(timezone.get_default_timezone())activity_date=datetime.datetime.combine(activity_date.date(),datetime.time(0,0,0),tzinfo=activity_date.tzinfo)presign_period=datetime.timedelta(minutes=settings.EIGHTH_PRESIGNUP_HOURS*60+settings.EIGHTH_PRESIGNUP_MINUTES)return(now<(activity_date-presign_period),activity_date-presign_period)def__str__(self):returnf"{self.title} - {self.time}"classMeta:ordering=["time"]verbose_name_plural="enrichment activities"