def test_by_date(self): # create published calendar published_cal = Calendar(title='title', state='published') published_cal.save() published_cal.sites.add(self.web_site) published_cal.save() # create published content content = ModelBase(title='title', state='published') content.save() content.sites.add(self.web_site) content.save() # create entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=content) entry_obj.save() entry_obj.calendars.add(published_cal) entry_obj.save() now = datetime.now() date = now.date() # result should only contain the entry for the date result = EntryItem.permitted.by_date(date) self.failUnlessEqual(result.count(), 1)
def test_now(self): # create published calendar published_cal = Calendar(title='title', state='published') published_cal.save() published_cal.sites.add(self.web_site) published_cal.save() # create published content content = ModelBase(title='title', state='published') content.save() content.sites.add(self.web_site) content.save() # create entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=content) entry_obj.save() entry_obj.calendars.add(published_cal) entry_obj.save() # should return currently active entry items, ordered by start queryset = EntryItem.permitted.now() self.failUnless(queryset.count()) for entry_item in queryset: self.failUnless(entry_item.start < datetime.now()) self.failUnless(entry_item.end > datetime.now())
def test_by_model(self): # create published calendar published_cal = Calendar(title='title', state='published') published_cal.save() published_cal.sites.add(self.web_site) published_cal.save() # create published wanted content wanted_content = WantedContent(title='title', state='published') wanted_content.save() wanted_content.sites.add(self.web_site) wanted_content.save() # create published unwanted content unwanted_content = UnwantedContent(title='title', state='published') unwanted_content.save() unwanted_content.sites.add(self.web_site) unwanted_content.save() # create entries for wanted entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=wanted_content) entry_obj.save() entry_obj.calendars.add(published_cal) entry_obj.save() # create entries for unwanted entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=unwanted_content) entry_obj.save() entry_obj.calendars.add(published_cal) entry_obj.save() # should only return entry items for content of the provided model. queryset = EntryItem.permitted.by_model(WantedContent) for obj in queryset: self.failUnlessEqual(obj.content.class_name, 'WantedContent')
def calendarCreate(request, pk=None): instance = Calendar() instance = Calendar(group_id=pk) form = CalendarForm(request.POST or None, instance=instance) if request.POST and form.is_valid(): form.save() return HttpResponseRedirect(reverse('cal:list_calendar', args=[pk])) return render(request, 'cal/form.html', {'form': form, 'name': "stanza"})
def form_valid(self, form): form.instance.created_by = self.request.user #sets the created_by user to the current one network = form.save(commit=False) network.save() #saves the object, sets the id self.object = network network_cal = Calendar(network=self.object) network_cal.save() messages.success(self.request, self.get_success_message()) return HttpResponseRedirect(self.get_success_url())
def form_valid(self, form): organization = form.save(commit=False) organization.save() #saves the object, sets the id organization.leader.add( self.request.user) #sets the leader user to the current one self.object = organization organization_cal = Calendar( organization=self.object ) #create a new calendar for the organization organization_cal.save() return HttpResponseRedirect(self.get_success_url())
def index(request): t=loader.get_template('cal/index.html') cal = Calendar() if request.method == 'GET' and 'month' in request.GET and 'year' in request.GET: month = cal.get_month(int(request.GET['month']),int(request.GET['year'])) else: month = cal.get_month(0,0) events = cal.get_events_by_month(month[0],month[1]) #print events days = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'] fields = cal.prepare_fields(month,days,events); print fields c= Context({ 'name' : month[0].strftime('%B %Y'), 'month': month, 'days' : days, 'fields' : fields }) return HttpResponse(t.render(c))
def activate(request, uidb64, token): user = None try: uid = force_text(urlsafe_base64_decode(uidb64)) user = User.objects.get(pk=uid) except (TypeError, ValueError, OverflowError, User.DoesNotExist): messages.error(request, "That activation link did not work") return HttpResponseRedirect(reverse_lazy('login')) if user is not None and account_activation_token.check_token(user, token): user.is_active = True user.save() login(request, user) try: user_cal = Calendar.objects.get(user=user) except Calendar.DoesNotExist: user_cal = Calendar(user=user) user_cal.save() #all of this down to the return statement creates a yearly repeating account anniversary event today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) pattern = rrule(freq=YEARLY, dtstart=today) description = "On this day in " + str( today.year) + ", your account was created!" aa_event = Event(rrule=pattern, all_day=True, start_time=today, end_time=today + timedelta(days=1), event_type='AA', title="Account Anniversary", description=description) aa_event.calendar = user_cal aa_event.save() messages.success(request, "Successfully verified your account!") return HttpResponseRedirect(reverse_lazy('home:main'))
def form_valid(self, form): form.instance.created_by = self.request.user nonprofit = form.save(commit=False) if self.kwargs.get('network'): nonprofit.network = get_object_or_404(Network, slug=self.kwargs['network']) tag_temp_var = form.cleaned_data.get('tags') #this gets the tag data nonprofit.save() #saves the object, sets the id nonprofit.tags.set( tag_temp_var) #saves the previous tag data after the id is created nonprofit.save() self.object = nonprofit network_calendar = Calendar.objects.get(network=self.object.network.id) nonprofit_cal = Calendar(nonprofit=self.object, network_calendar=network_calendar) nonprofit_cal.save( ) #these last three lines create a calendar for this nonprofit messages.success(self.request, self.get_success_message()) return HttpResponseRedirect(self.get_success_url())
def test_by_range(self): # create published calendar published_cal = Calendar(title='title', state='published') published_cal.save() published_cal.sites.add(self.web_site) published_cal.save() # create published content content = ModelBase(title='title', state='published') content.save() content.sites.add(self.web_site) content.save() start = datetime.now() end = start + timedelta(days=2) # create entryitem that spans the range spanning_entryitem = EntryItem(entry_id=1, start=start - timedelta(days=1), end=end + timedelta(days=1), content=content) spanning_entryitem.save() spanning_entryitem.calendars.add(published_cal) # create entryitem that precedes the range preceding_entryitem = EntryItem(entry_id=1, start=start - timedelta(days=1), end=start, content=content) preceding_entryitem.save() preceding_entryitem.calendars.add(published_cal) # create entryitem that procedes the range proceding_entryitem = EntryItem(entry_id=1, start=end, end=end + timedelta(days=1), content=content) proceding_entryitem.save() proceding_entryitem.calendars.add(published_cal) # create entryitem that is contained in the range contained_entryitem = EntryItem(entry_id=1, start=start, end=end, content=content) contained_entryitem.save() contained_entryitem.calendars.add(published_cal) # create entryitem that starts before range but ends within range end_contained_entryitem = EntryItem(entry_id=1, start=start-timedelta(days=1), end=end, content=content) end_contained_entryitem.save() end_contained_entryitem.calendars.add(published_cal) # create entryitem that starts in range but ends after range start_contained_entryitem = EntryItem(entry_id=1, start=start, end=end+timedelta(days=1), content=content) start_contained_entryitem.save() start_contained_entryitem.calendars.add(published_cal) result = EntryItem.permitted.by_range(start, end) # spanning entry should be in result self.failUnless(spanning_entryitem in result) # preceding entry should not be in result self.failIf(preceding_entryitem in result) # proceding entry should not be in result self.failIf(proceding_entryitem in result) # contained entryitem should be in result self.failUnless(contained_entryitem in result) # entry starting before range but ending withing range should be in result self.failUnless(end_contained_entryitem in result) # entry starting in range but ending after range should be in result self.failUnless(start_contained_entryitem in result)
def test_get_query_set(self): # create unpublished calendar unpublished_cal = Calendar(title='title', state='unpublished') unpublished_cal.save() unpublished_cal.sites.add(self.web_site) unpublished_cal.save() # create staging calendar staging_cal = Calendar(title='title', state='staging') staging_cal.save() staging_cal.sites.add(self.web_site) staging_cal.save() # create published calendar published_cal = Calendar(title='title', state='published') published_cal.save() published_cal.sites.add(self.web_site) published_cal.save() # create unpublished content unpublished_content = ModelBase(title='title', state='unpublished') unpublished_content.save() unpublished_content.sites.add(self.web_site) unpublished_content.save() # create staging content staging_content = ModelBase(title='title', state='staging') staging_content.save() staging_content.sites.add(self.web_site) staging_content.save() # create published content published_content = ModelBase(title='title', state='published') published_content.save() published_content.sites.add(self.web_site) published_content.save() # entries with unpublished calendars and content should not be available in queryset # create unpublished cal and content entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=unpublished_content) entry_obj.save() entry_obj.calendars.add(unpublished_cal) entry_obj.save() queryset = EntryItem.permitted.all() self.failIf(queryset.count()) Entry.objects.all().delete() # entries with unpublished calendars should not be available in queryset, regardless of content state # create unpublished cal, published content entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=published_content) entry_obj.save() entry_obj.calendars.add(unpublished_cal) entry_obj.save() queryset = EntryItem.permitted.all() self.failIf(queryset.count()) Entry.objects.all().delete() # entries with unpublished content should not be available in queryset, regardless of cal state # create unpublished content, published cal entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=unpublished_content) entry_obj.save() entry_obj.calendars.add(published_cal) entry_obj.save() queryset = EntryItem.permitted.all() self.failIf(queryset.count()) Entry.objects.all().delete() # entries with staging calendars and content should be available in queryset but only if settings.STAGING = True # create staging cal and content entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=staging_content) entry_obj.save() entry_obj.calendars.add(staging_cal) entry_obj.save() settings.STAGING = False queryset = EntryItem.permitted.all() self.failIf(queryset.count()) settings.STAGING = True queryset = EntryItem.permitted.all() self.failUnless(queryset.count()) Entry.objects.all().delete() # entries with published cal and content should be available in queryset # create published cal and content entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=published_content) entry_obj.save() entry_obj.calendars.add(published_cal) entry_obj.save() queryset = EntryItem.permitted.all() self.failUnless(queryset.count()) Entry.objects.all().delete() # queryset should not contain items for other sites mobile_site = Site(domain="mobi.address.com") mobile_site.save() # create published calendar for mobile site published_cal_mobile = Calendar(title='title', state='published') published_cal_mobile.save() published_cal_mobile.sites.add(mobile_site) published_cal_mobile.save() # create published calendar for mobile site published_content_mobile = ModelBase(title='title', state='published') published_content_mobile.save() published_content_mobile.sites.add(mobile_site) published_content_mobile.save() entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=published_content_mobile) entry_obj.save() entry_obj.calendars.add(published_cal_mobile) entry_obj.save() queryset = EntryItem.permitted.all() self.failIf(queryset.count())