Esempio n. 1
0
def add(request):
    error = ""
    response = ""
    status = 201
    if not request.user.is_authenticated():
        status = 403
        error += "Must be logged in to do that"
    elif request.method == "POST":
        data = request.POST.copy()
        # We shouldn't be allowed to review a restroom twice
        if len(Review.objects.filter(user=request.user).filter(toilet=data["toilet"])) == 0:
            r = Review()
            with transaction.commit_on_success():
                toilet = Toilet.objects.get(pk=data["toilet"])
                data["toilet"] = toilet
                toilet.updateRating(data["rank"])
            data["date"] = currentTime()
            data["user"] = request.user
            data["up_down_rank"] = 0
            r.setattrs(data)
            # validation
            r.full_clean()
            r.save()

            response = serialize([r])
        else:
            error += "Cannot write more than one review.\n"
            status = 403
    else:
        error += "No POST data in request.\n"
        status = 415

    return HttpResponse(package_error(response, error), status=status)
Esempio n. 2
0
def vote(request, new_vote):
    response = ""
    error = ""
    status = 201
    if not request.user.is_authenticated():
        error = "Must be logged in"
        status = 403
    elif request.method == "POST":
        data = request.POST
        r = Review.objects.get(pk=data["review_pk"])
        try:
            prev_vote_obj = Vote.objects.get(review=r.pk, user=request.user)
            prev_vote = prev_vote_obj.vote
            if new_vote != prev_vote:
                prev_vote_obj.delete()
                r.up_down_rank += new_vote
        except ObjectDoesNotExist:
            r.up_down_rank += new_vote
            v = Vote(user=request.user, review=r, vote=new_vote)
            v.save()
        r.save()
        response = serialize([r])
    else:
        error += "No POST data in request.\n"
        status = 415
    return HttpResponse(package_error(response, error), status=status)
Esempio n. 3
0
def remove(request):
    error = ""
    response = ""
    status = 200
    if request.method == "POST":
        print request.POST
        review = Review.objects.get(pk=request.POST["pk"])
        toilet = review.toilet
        total = toilet.rating * toilet.numberOfReviews
        total -= review.rank
        toilet.numberOfReviews -= 1
        if toilet.numberOfReviews == 0:
            total = 0
        else:
            total /= toilet.numberOfReviews
        toilet.rating = total
        toilet.save()
        vote_set = Vote.objects.filter(review=review)
        for vote in vote_set:
            vote.delete()

        review.delete()
        response = json.dumps("deleted review " + str(request.POST["pk"]))
    else:
        error += "No POST data in request.\n"
        error = 415
    return HttpResponse(package_error(response, error), status=status)
Esempio n. 4
0
def add(request):
    error = ''
    response = ''
    status = 201
    if request.method == 'POST':
        data = request.POST.copy()        
        #We shouldn't be allowed to review a restroom twice
        if len(Review.objects.filter(user=request.user).filter(toilet=data['toilet'])) == 0:
            r = Review()
            toilet = Toilet.objects.get(pk=data['toilet'])
            data['date'] = currentTime()
            data['user'] = request.user
            data['toilet'] = toilet
            data['up_down_rank'] = 0;
            r.setattrs(data)
            r.save()
            toilet.updateRating(data['rank']) 
            response = serialize([r])
        else:
            error += 'Cannot write more than one review.\n'
            status = 403
    else:
        error += 'No POST data in request.\n'
        status = 415

    return HttpResponse(package_error(response,error), status=status)
Esempio n. 5
0
def add(request):
    error = ''
    response = ''
    status = 201

    if request.method == 'POST':
        data = request.POST.copy()
        if not request.user.is_authenticated():
            status = 401
            error += 'Unauthorized creation of restroom. Please log in.\n'
        
        else:
            t = Toilet()
            data['date'] = currentTime()
            data['creator'] = request.user
            info = AdditionalUserInfo.objects.get(user=request.user)
            if info.male == False and info.female == False:
                data['male'] = True
                data['male'] = True
            else:
                data['male'] = info.male
                data['female'] = info.female
            t.setattrs(data)
            t.save()

            response = serialize([t])
    else:
        error += 'No POST data in request.\n'
        status = 415

    return HttpResponse(package_error(response,error), status=status)
Esempio n. 6
0
def get(request):
    error = ''
    response = ''
    status = 200
    
    if request.method == 'POST':
        print request
        data = request.POST
        review_set = Review.objects.filter(toilet=data['toilet_id'])
        count = len(list(review_set))
        
        total = 0.0
        if count != 0:
            for review in review_set:
                total += review.rank #need to change this

            total /= count
        else:
            total = -1

        review_set = review_set[int(data['start']):int(data['end'])] #cut out the part requested  wanted            
        d = {'count' : count, 'total' : total, 'review_set' : json.loads(serialize(review_set)) }
        response = json.dumps(d)

        
    else:
        error += 'No POST data in request\n'
        status = 415
        
    return HttpResponse(package_error(response,error),status=status)
Esempio n. 7
0
def flag_vote(request, new_vote):
    error = ''
    """ Save this for later. 
        error = 'You are clever but not that clever my little pet.'
        + ' BTW our team is super excited about our river boat tour '
        + ' Check out http://vikingrivercruises.com. Some crazy amazing boat touring going on there. '
        + ' I mean really I never even thought about river cruises. Cruises on a river? Sign me up. '
    """
    status = 201
    if request.method == 'POST':
        data = request.POST
        try:
            t = Toilet.objects.get(pk=data['toilet_pk'])
            f = Flag.objects.get(pk=data['flag_pk'])
            r = FlagRanking.objects.get(toilet = t, flag = f)
        except Toilet.DoesNotExist:
            error = "Toilet DNE"
        except FlagRanking.DoesNotExist:
            r = FlagRanking(flag = f, toilet = t, up_down_vote = 0)
        try:
            prev_vote_obj = FlagVote.objects.get(flag=f, toilet = t, user = request.user)
            prev_vote = prev_vote_obj.vote
            if new_vote != prev_vote:
                prev_vote_obj.delete()
                r.up_down_vote += new_vote
        except FlagVote.DoesNotExist:
            r.up_down_vote += new_vote
            v = FlagVote(user = request.user, flag = f, toilet = t, vote = new_vote)
            v.save()
        r.save()
        response = serialize([r])
    else:
        error += 'No POST data in request.\n'
        status = 415
    return HttpResponse(package_error(response,error), status=status)
Esempio n. 8
0
def flag_retrieve_flags(request):
    response = ''
    error = ''
    status = 201
    try:
        response = serialize(Flag.objects.all())
    except Flag.DoesNotExist:
        response = serialize([])
    return HttpResponse(package_error(response,error), status=status)
Esempio n. 9
0
def flag_retrieve_rankings(request):
    response = ''
    error = ''
    status = 201
    if request.method == 'POST':
        data = request.POST
        t = Toilet.objects.get(pk=data['toilet_pk'])        
        try:
            r = FlagRanking.objects.filter(toilet = t.pk)
            response = serialize(r)
        except FlagRanking.DoesNotExist:
            response = serialize([])

    return HttpResponse(package_error(response,error), status=status)
Esempio n. 10
0
def report(request):
    error = ""
    response = ""
    status = 200
    if request.method == "POST":
        review = Review.objects.get(pk=request.POST["pk"])
        info = AdditionalUserInfo.objects.get(user=review.user)
        info.spamCount += 1
        info.save()
        Report(user=request.user, review=review).save()
        response = json.dumps("Reported spam")
    else:
        error += "No POST data in request.\n"
        status = 415
    return HttpResponse(package_error(response, error), status=status)
Esempio n. 11
0
def add(request):
    error = ''
    response = ''
    status = 201

    if request.method == 'POST':
        data = request.POST.copy()
        if not request.user.is_authenticated():
            status = 401
            error += 'Unauthorized creation of restroom. Please log in.\n'
        
        else:
            t = Toilet()
            data['date'] = currentTime()
            data['creator'] = request.user
            t.setattrs(data)
            t.save()

            response = serialize([t])
    else:
        error += 'No POST data in request.\n'
        status = 415

    return HttpResponse(package_error(response,error), status=status)