示例#1
0
    def get(self, request):
        site_url = get_site_url_from_request(request)
        grouped_subjects = Subjects.group_for_part_drill_down(
            Subjects.all_subjects())
        # Get the cambridge year data for the acadamic year set in the settings.
        acadamic_year = AcademicYear.for_year(settings.DEFAULT_ACADEMIC_YEAR)
        context = {
            "subjects": Subjects.to_json(grouped_subjects),
            "site_url": site_url,
            "terms": acadamic_year.get_terms_json(),
            "calendar_start": acadamic_year.start_boundary.isoformat(),
            "calendar_end": acadamic_year.end_boundary.isoformat()
        }
        if request.user.is_authenticated():
            # create a url with a hmac in it if the thing is a user. If not just a simple url will do.
            thing = Thing.get_or_create_user_thing(request.user)
            context["user_thing"] = thing

            thingsubject = ThingSubject(thing=thing)
            hmac = thingsubject.create_hmac()
            context["ics_feed_url"] = reverse("export ics hmac",
                                              kwargs={
                                                  "thing": thing.fullpath,
                                                  "hmac": hmac
                                              })
        else:
            context["ics_feed_url"] = None
        try:
            context['raven_url'] = settings.RAVEN_URL
        except:
            pass

        return render(request, "student/base.html", context)
 def set_metadata(self, crsid, metadata):
     fake_user = FakeUser(username=crsid)
     user_thing = Thing.get_or_create_user_thing(fake_user)
     user_thing.metadata["studentinfo"] = metadata
     user_thing.save()
    def post(self, request, thing):
        # Check if the user is logged in
        if request.user.is_anonymous():
            return HttpResponseForbidden("Not logged in")
        elif not request.user.has_perm(Thing.PERM_LINK,
                                       ThingSubject(fullpath=thing)):
            return HttpResponseForbidden("Not your calendar")

        hashid = Thing.hash(thing)
        try:
            try:
                thing = Thing.objects.get(pathid=hashid)
            except Thing.DoesNotExist:
                path = "user/%s" % request.user.username
                if thing == path:
                    thing = Thing.get_or_create_user_thing(request.user)

            # Delete associations first
            elist = self._expand(request.POST.getlist("esd"))
            if len(elist) > 0:
                EventSourceTag.objects.filter(
                    thing=thing,
                    eventsource__in=EventSource.objects.filter(
                        id__in=elist)).delete()
            elist = self._expand(request.POST.getlist("ed"))
            if len(elist) > 0:
                EventTag.objects.filter(
                    thing=thing,
                    event__in=Event.objects.filter(id__in=elist)).delete()

            # If there is a list of things to delete, this is a little more complicated.
            tlist = self._expand(request.POST.getlist("td"))
            if len(tlist) > 0:
                # remove all EventTags and EventSourceTags that link this thing to Events or EventSource linked to by any child
                # The following query gets the decendents of all the things listed
                decendents = Thing.objects.filter(Thing.treequery(tlist))
                # Then get the Events associated with all the decendents of all the things
                decendent_events = Event.objects.filter(
                    eventtag__thing__in=decendents)
                # And delete all events tags on this thing, that match those events.
                EventTag.objects.filter(thing=thing,
                                        event__in=decendent_events).delete()
                # get all eventsources that are associated with the list of decendent things
                decendent_eventsource = EventSource.objects.filter(
                    eventsourcetag__thing__in=decendents)
                EventSourceTag.objects.filter(
                    thing=thing,
                    eventsource__in=decendent_eventsource).delete()

            # Add associations
            elist = self._expand(request.POST.getlist("es"))
            if len(elist) > 0:
                # Delete then bulk add, note that no hooks on bulk add
                EventSourceTag.objects.filter(
                    thing=thing,
                    eventsource__in=EventSource.objects.filter(
                        id__in=elist)).delete()
                items = []
                for es in EventSource.objects.filter(id__in=elist,
                                                     current=True):
                    eventsourcetag = EventSourceTag(thing=thing,
                                                    eventsource=es)
                    eventsourcetag.on_pre_save()
                    items.append(eventsourcetag)
                EventSourceTag.objects.bulk_create(items)

            elist = self._expand(request.POST.getlist("e"))
            if len(elist) > 0:
                # Delete then bulk add, note that no hooks on bulk add
                EventTag.objects.filter(
                    thing=thing,
                    event__in=Event.objects.filter(id__in=elist)).delete()
                items = []
                for e in Event.objects.filter(id__id=elist, current=True):
                    eventtag = EventTag(thing=thing, event=e)
                    eventtag.on_pre_save()
                    items.append(eventtag)
                EventTag.objects.bulk_create(items)

            tlist = self._expand(request.POST.getlist("t"))
            if len(tlist) > 0:
                # remove all EventTags and EventSourceTags that link this thing to Events or EventSource linked to by any child
                # The following query gets the decendents of all the things listed
                decendents = Thing.objects.filter(Thing.treequery(tlist))
                # Then get the Events associated with all the decendents of all the things
                decendent_events = Event.objects.filter(
                    eventtag__thing__in=decendents)
                # And delete all events tags on this thing, that match those events.
                EventTag.objects.filter(thing=thing,
                                        event__in=decendent_events).delete()
                # get all eventsources that are associated with the list of decendent things
                decendent_eventsource = EventSource.objects.filter(
                    eventsourcetag__thing__in=decendents)
                EventSourceTag.objects.filter(
                    thing=thing,
                    eventsource__in=decendent_eventsource).delete()

                # Having deleted, we need to add, first the events bulk creating EventTags
                items = []
                for e in decendent_events.filter(current=True):
                    eventtag = EventTag(thing=thing, event=e)
                    eventtag.on_pre_save()
                    items.append(eventtag)
                EventTag.objects.bulk_create(items)

                # Next the Event Sources bulk creating EventSourceTags
                items = []
                for es in decendent_eventsource.filter(current=True):
                    eventtag = EventSourceTag(thing=thing, eventsource=es)
                    eventtag.on_pre_save()
                    items.append(eventtag)
                EventSourceTag.objects.bulk_create(items)

            return HttpResponse("ok")

        except Thing.DoesNotExist:
            return HttpResponseNotFound()