Example #1
0
def create_event(request):
    event_data = json.loads(request.POST.get('event'))
    user_profile_data = json.loads(request.POST.get('user_profile'))

    user_profile = None
    if USER_TOKEN in request.COOKIES:
        try:
            user_profile = UserProfile.objects.get(
                token=request.COOKIES[USER_TOKEN])
        except UserProfile.DoesNotExist:
            pass
    if not user_profile:
        user_profile = UserProfileForm(user_profile_data, request.FILES).save()

    # Create event
    event = process_event_data(event_data)
    if not event:
        return HttpResponseBadRequest()

    data = {
        'event': event.as_dict(host='http://' + request.get_host()),
        'user_profile': user_profile.as_dict(),
    }
    cookie_dict = {
        USER_TOKEN: user_profile.token
    }
    return render_as_json(data, cookie_dict)
Example #2
0
def update_event(request, event_id):
    try:
        event_data = json.loads(request.POST.get('event'))
    except ValueError:
        return HttpResponseBadRequest()
    try:
        event = Event.objects.get(id=event_id)
    except Event.DoesNotExist:
        return HttpResponseNotFound()
    creator = event.creator
    if not USER_TOKEN in request.COOKIES:
        return HttpResponseBadRequest()
    if not request.COOKIES[USER_TOKEN] == event.creator.token:
        return HttpResponseForbidden()
    try:
        process_event_data(event_data)
    except:
        return HttpResponseBadRequest()
    data = {
        'event': event.as_dict(host='http://' + request.get_host()),
    }
    return render_as_json(data)