def _submit_vote(request,hunt): url = request.POST.get("url") if not url: return HttpResponseBadRequest() # resolve the URL for the slug and object_id try: view, args, kwargs = resolve(urlparse(url)[2]) except: return api_utils.api_error(request, "Invalid photo URL: "+str(url)) slug = kwargs['slug'] object_id = kwargs['object_id'] if hunt.slug != slug: return api_utils.api_error(request, "Photo isn't a part of this hunt") submission = get_object_or_404(Submission,pk=object_id) vote = Vote(hunt=hunt, submission=submission, ip_address=request.META.get('REMOTE_ADDR')) # TODO: Some users may have a vote of more value. vote.value = 1 if request.user.is_authenticated(): vote.user = request.user else: vote.anon_source = get_anon_source(request) vote.save() return _get_ballot(request,hunt)
def _submit_photo(request,hunt): f = SubmissionForm(request.POST, request.FILES) if not f.is_valid(): return api_utils.api_error(request,str(f.errors)) # Ensure the time is within the hunt response = _ensure_current(request, hunt) if response: return response photo = f.save(commit=False) if request.user.is_authenticated(): photo.user = request.user else: photo.anon_source = get_anon_source(request) photo.ip_address = request.META.get('REMOTE_ADDR') photo.hunt = hunt photo.submit() # response_content_type is an idiotic hack to work around some # weird interaction between JSONView and ajaxSubmit(). response = HttpResponse(api_utils.to_json(request,photo), status=201, content_type=request.POST.get('response_content_type', api_utils.JSON_TYPE)) response['Content-Location'] = request.build_absolute_uri(photo.get_api_url()) return response;
def user_activity_view(request,slug): user = get_object_or_404(User, username=slug) profile,created = Profile.objects.get_or_create(user=user) if not profile.public_activity: if request.user != user: return HttpResponseForbidden() since = request.REQUEST.get("since") if since: # better be parsable as ISO try: since = iso8601.parse_date(since) if since.utcoffset(): return utils.api_error(request, "We currently don't support non-UTC times") since = since.replace(tzinfo=None) except ValueError: return utils.api_error(request, "Unable to parse: " + since) url = request.build_absolute_uri(reverse('profile', kwargs={ 'slug': slug })) activity = map(_convert_to_activity(request), user_activity(user, since)) return utils.to_json(request, { 'user': { 'name': slug, 'url': url }, 'activity': activity })
def _submit_comment(request, hunt, submission): f = CommentForm(request.POST) if not f.is_valid(): return api_utils.api_error(request,str(f.errors)) # You can leave a comment at any time comment = f.save(commit=False) if request.user.is_authenticated(): comment.user = request.user else: comment.anon_source = get_anon_source(request) comment.ip_address = request.META.get('REMOTE_ADDR') comment.hunt = hunt comment.submission = submission comment.save() response = HttpResponse(api_utils.to_json(request,comment), status=201, content_type=api_utils.JSON_TYPE) response['Content-Location'] = request.build_absolute_uri(comment.get_api_url()) return response
def _get_ballot(request,hunt): try: return _get_photos(request, random.sample(hunt.submission_set.filter(is_removed=False), 2)) except ValueError: return api_utils.api_error(request, "We're still waiting on photos. Check back later, or add your own!")
def _ensure_vote_current(request,hunt): now = datetime.utcnow() if now < hunt.start_time: return api_utils.api_error(request, "Hunt hasn't started yet") if now >= hunt.vote_end_time: return api_utils.api_error(request, "Hunt has ended")