コード例 #1
0
    def dispatch(self, request, *args, **kwargs):

        if not request.method == 'POST':
            return CORSHttpResponse(status=404)

        if 'username' in request.POST:
            self.username = request.POST['username']
        else:
            return CORSHttpResponse(status=400)

        self.tags = request.POST.getlist('tag')

        try:
            user = OurUser.objects.get(username=self.username)
        except ObjectDoesNotExist:
            return CORSHttpResponse(status=403)

        tag_objs = []
        for tag in self.tags:
            try:
                tag_objs.append(
                    Tag.objects.get(name=slugify(tag, separator="_")))
            except ObjectDoesNotExist:
                return CORSHttpResponse(status=404)

        for tag in tag_objs:
            user.subscriptions.add(tag)

        user.save()

        return CORSHttpResponse(status=200)
コード例 #2
0
    def dispatch(self, request, *args, **kwargs):

        if not request.method == 'GET':
            return CORSHttpResponse(status=403)

        events = self.handle_params(request.GET)

        attending = []
        not_attending = []

        if 'username' in request.GET:
            try:
                user = OurUser.objects.get(username=request.GET['username'])
                for e in events:
                    if user in e.attendees.all():
                        attending.append(e)
                    else:
                        not_attending.append(e)
            except ObjectDoesNotExist:
                return CORSHttpResponse(status=404)
        else:
            not_attending = events

        if events is None:
            return CORSHttpResponse(status=400)
        
        serialized_attending = serializers.serialize("json", attending)
        serialized_not_attending = serializers.serialize("json", not_attending)
        json = JsonResponse({'attending': serialized_attending, 'not_attending': serialized_not_attending}, safe=False)
        return CORSHttpResponse(status=200, content=json, content_type="application/json")
コード例 #3
0
    def dispatch(self, request, *args, **kwargs):

        if not request.method == 'POST':
            return CORSHttpResponse(status=403)

        self.name = request.POST['name']
        self.admins = request.POST.getlist('admins')
        self.tagname = slugify(self.name, separator="_")
        tag, create = Tag.objects.get_or_create(name=self.tagname,
                                                is_org_tag=True)
        admin_objects = []
        try:
            for admin_name in self.admins:
                admin, created = OurUser.objects.get_or_create(
                    username=admin_name)
                admin_objects.append(admin)
        except ObjectDoesNotExist:
            # One or more of the Users given does not exist, HTTP 400 - bad request
            return CORSHttpResponse(status=400)

        org, created = Organization.objects.get_or_create(name=self.name,
                                                          org_tag=tag)
        if not created:
            # Organization with name already exists, HTTP 409 - conflict
            return CORSHttpResponse(status=409)

        org.admins = admin_objects
        org.save()

        return CORSHttpResponse(status=200)
コード例 #4
0
    def dispatch(self, request, *args, **kwargs):

        if not request.method == 'GET':
            return CORSHttpResponse(status=403)

        response = None

        if 'id' in request.GET:
            response = Tag.objects.filter(
                pk__in=request.GET.getlist('id')).distinct()
        else:
            response = Tag.objects.all()

        if 'username' in request.GET:
            try:
                subscribed = [
                    s.pk for s in OurUser.objects.get(
                        username=request.GET['username']).subscriptions.all()
                ]
            except ObjectDoesNotExist:
                CORSHttpResponse(status=400)
            response = response.filter(~Q(pk__in=subscribed))

        if response is None:
            return CORSHttpResponse(status=400)

        serialized_response = serializers.serialize('json', response)
        return CORSHttpResponse(status=200,
                                content=serialized_response,
                                content_type="application/json")
コード例 #5
0
    def dispatch(self, request, *args, **kwargs):

        if not request.method == 'POST':
            return CORSHttpResponse(status=403)

        if 'username' in request.POST:
            username = request.POST['username']
            user_obj, created = OurUser.objects.get_or_create(
                username=username)

            json_response = JsonResponse({"new": created})
            return CORSHttpResponse(status=200,
                                    content=json_response.content,
                                    content_type="application/json")
コード例 #6
0
    def dispatch(self, request, *args, **kwargs):
        if not request.method == 'POST':
            return CORSHttpResponse(status=403)
    
        if 'username' in request.POST.keys() and 'event' in request.POST.keys():
            try:
                user = OurUser.objects.get(username=request.POST['username'])
                event = Event.objects.get(pk=request.POST['event'])
                event.attendees.remove(user)
                event.save()
            except ObjectDoesNotExist:
                return CORSHttpResponse(status=404)
        else:
            return CORSHttpResponse(status=400)

        return CORSHttpResponse(status=200)
コード例 #7
0
    def dispatch(self, request, *args, **kwargs):

        if not request.method == 'GET':
            return CORSHttpResponse(status=403)

        if 'username' in request.GET.keys():
            try:
                user = OurUser.objects.get(username=request.GET['username'])
            except ObjectDoesNotExist:
                return CORSHttpResponse(status=404)
        else:
            return CORSHttpResponse(status=400)
        events = Event.objects.filter(attendees__id__exact=user.pk,
                                      end_datetime__gt=datetime.now())
        serialized_response = serializers.serialize("json", events)

        return CORSHttpResponse(status=200,
                                content=serialized_response,
                                content_type="application/json")
コード例 #8
0
    def dispatch(self, request, *args, **kwargs):

        if not request.method == 'GET':
            return CORSHttpResponse(status=403)

        if 'username' in request.GET.keys():
            self.username = request.GET['username']
        else:
            return CORSHttpResponse(status=400)

        try:
            user = OurUser.objects.get(username=self.username)
        except ObjectDoesNotExist:
            return CORSHttpResponse(status=404)

        subs = [tag.name for tag in user.subscriptions.all()]
        serialized_response = JsonResponse({"tags": subs}, safe=False)
        return CORSHttpResponse(status=200,
                                content=serialized_response,
                                content_type="application/json")
コード例 #9
0
    def dispatch(self, request, *args, **kwargs):
        if not request.method == 'GET':
            return CORSHttpResponse(status=403)
    
        if 'username' in request.GET.keys():
            try:
                user = OurUser.objects.get(username=request.GET['username'])
            except ObjectDoesNotExist:
                return CORSHttpResponse(status=404)
        else:
            return CORSHttpResponse(status=400)
        
        tags = [tag.name for tag in user.subscriptions.all()]
        events = []

        for e in Event.objects.all():
            for tag in e.tags.all():
                if tag.name in tags:
                   events.append(e)
                   break
        
        serialized_event = serializers.serialize("json", events)
        return CORSHttpResponse(status=200, content=serialized_event, content_type="application/json")
コード例 #10
0
    def dispatch(self, request, *args, **kwargs):

        if not request.method == 'GET':
            return CORSHttpResponse(status=403)

        if 'id' in request.GET:
            try:
                org = Organization.objects.filter(pk=request.GET['id'])
            except ObjectDoesNotExist:
                return CORSHttpResponse(status=400)
            serialized_response = serializers.serialize("json", org)
            return CORSHttpResponse(status=200,
                                    content=serialized_response,
                                    content_type="application/json")

        elif 'name' in request.GET:
            try:
                org = Organization.objects.filter(name=request.GET['name'])
            except ObjectDoesNotExist:
                return CORSHttpResponse(status=400)
            serialized_response = serializers.serialize("json", org)
            return CORSHttpResponse(status=200,
                                    content=serialized_response,
                                    content_type="application/json")

        elif 'admin' in request.GET:
            try:
                orgs = set()
                for org in Organization.objects.all():
                    for admin in org.admins.all():
                        if request.GET['admin'] == admin.username:
                            orgs.add(org)
                orgs_as_list = list(orgs)
            except ObjectDoesNotExist:
                return CORSHttpResponse(status=400)
            serialized_response = serializers.serialize("json", orgs_as_list)
            return CORSHttpResponse(status=200,
                                    content=serialized_response,
                                    content_type="application/json")

        else:
            orgs = Organization.objects.all()
            serialized_response = serializers.serialize("json", orgs)
            return CORSHttpResponse(status=200,
                                    content=serialized_response,
                                    content_type="application/json")
コード例 #11
0
    def dispatch(self, request, *args, **kwargs):

        if not request.method == 'POST':
            return CORSHttpResponse(status=403)

        self.creator = request.POST['creator']
        self.name = request.POST['name']
        self.start_date = request.POST['start_date']
        self.start_time = request.POST['start_time']
        self.end_date = request.POST['end_date']
        self.end_time = request.POST['end_time']
        self.location = request.POST['location']
        self.organization = request.POST['host']
        self.description = request.POST['description']
        self.tags = request.POST.getlist('tag')

        try:
            org = Organization.objects.get(name=self.organization)
        except ObjectDoesNotExist:
            return CORSHttpResponse(status=400)

        access_granted = False
        for admin in org.admins.all():
            if self.creator == admin.username:
                access_granted = True

        if not access_granted:
            return CORSHttpResponse(status=400)

        try:
            org = Organization.objects.get(name=self.organization)
            org_tag = org.org_tag
        except ObjectDoesNotExist:
            return CORSHttpResponse(status=500)

        try:
            start_date_obj = datetime.strptime(self.start_date, '%Y-%m-%d')
            start_time_obj = datetime.strptime(self.start_time, '%H:%M')
            end_date_obj = datetime.strptime(self.end_date, '%Y-%m-%d')
            end_time_obj = datetime.strptime(self.end_time, '%H:%M')
        except ValueError:
            return CORSHttpResponse(status=400)


        tags_list = [Tag.objects.get_or_create(name=slugify(tag, separator="_"))[0] for tag in self.tags]
        tags_list.append(org_tag)

        hosts_list = [org]

        start_datetime_obj = datetime.combine(start_date_obj.date(), start_time_obj.time())
        end_datetime_obj = datetime.combine(end_date_obj.date(), end_time_obj.time())

        if end_datetime_obj < start_datetime_obj:
            return CORSHttpResponse(status=400)

        event = Event(name=self.name, start_datetime=start_datetime_obj, end_datetime=end_datetime_obj,
                      description=self.description, location=self.location)
        event.save()
        event.tags = tags_list
        event.hosts = hosts_list
        event.attendees = list(org.admins.all())
        event.save()
        return CORSHttpResponse(status=200)
コード例 #12
0
    def dispatch(self, request, *args, **kwargs):

        if not request.method == 'POST':
            return CORSHttpResponse(status=403)

        return CORSHttpResponse(status=501)