def admin_featured(request, enc): area = int(request.GET.get('area', 0)) if request.method == 'POST': form = request.POST['form'] if form == 'create_new': fgid = request.POST['id'] rank = float(request.POST['rank']) area = int(request.POST['area']) try: fg = models.Flowgram.objects.get(id=fgid) except models.Flowgram.DoesNotExist: models.add_bad_message(request, "Invalid Flowgram ID %s" % fgid) else: models.Featured.objects.create(area=area, flowgram=fg, rank=rank) elif form == 'modify': to_delete = [] for (key, value) in request.POST.items(): if key.find('__') == -1: continue (feat_id, attr) = key.split('__') feat = models.Featured.objects.get(id=feat_id) if attr == 'id': fgid = value if not fgid: to_delete.append(feat) continue try: fg = models.Flowgram.objects.get(id=fgid) except models.Flowgram.DoesNotExist: models.add_bad_message(request, "Invalid Flowgram ID %s" % fgid) break feat.flowgram = fg feat.save() elif attr == 'pri': feat.rank = float(value) feat.save() else: raise Http500 for feat in to_delete: feat.delete() else: raise Http500 featured = models.Featured.objects.filter(area=area) return helpers.req_render_to_response( request, "admin/admin_featured.html", { 'featured':featured, 'area': area, 'areas': models.FEATURED_AREAS, })
def admin_polls(request, enc, current_poll_id): polls = models.FlowgramPoll.objects.filter().order_by('-date_created') if request.method == 'GET': if not current_poll_id == '': current_poll = models.FlowgramPoll.objects.get(id=current_poll_id) (fg1, fg2, fg3) = models.Flowgram.objects.filter(id__in=[current_poll.candidate_id_1, current_poll.candidate_id_2, current_poll.candidate_id_3]) return helpers.req_render_to_response( request, 'admin/admin_polls.html', { 'current_poll': current_poll, 'current_poll_id': current_poll_id, 'fg1': fg1, 'fg2': fg2, 'fg3': fg3, }) else: return helpers.req_render_to_response(request, 'admin/admin_polls.html', {'polls': polls}) elif request.method == 'POST': form = request.POST['form'] if form == 'create_new': poll_short_name = request.POST['poll_short_name'] poll_title = request.POST['poll_title'] candidate_id_1 = request.POST['candidate_id_1'] candidate_id_2 = request.POST['candidate_id_2'] candidate_id_3 = request.POST['candidate_id_3'] had_exception = False try: fg = models.Flowgram.objects.get(id=candidate_id_1) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message( request, "Candidate ID 1 is invalid. Please try again, cheeto-breath.") try: fg = models.Flowgram.objects.get(id=candidate_id_2) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message( request, "Candidate ID 2 is invalid. Please try again, cheeto-breath.") try: fg = models.Flowgram.objects.get(id=candidate_id_3) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message( request, "Candidate ID 3 is invalid. Please try again, cheeto-breath.") if had_exception: return helpers.req_render_to_response( request, 'admin/admin_polls.html', { 'polls': polls, 'poll_short_name': poll_short_name, 'poll_title': poll_title, 'candidate_id_1': candidate_id_1, 'candidate_id_2': candidate_id_2, 'candidate_id_3': candidate_id_3, }) poll_active = request.POST['poll_active_hidden'].lower() == 'true' models.FlowgramPoll.objects.create(poll_short_name=poll_short_name, poll_title=poll_title, candidate_id_1=candidate_id_1, candidate_id_2=candidate_id_2, candidate_id_3=candidate_id_3, poll_active=poll_active) helpers.add_good_message(request, "You created a new poll called %s." % poll_title) return helpers.req_render_to_response(request, 'admin/admin_polls.html', {'polls': polls}) elif form == 'modify': current_poll = models.FlowgramPoll.objects.get(id=request.POST['poll_id']) current_poll.poll_short_name = request.POST['poll_short_name'] current_poll.poll_title = request.POST['poll_title'] current_poll.candidate_id_1 = request.POST['candidate_id_1'] current_poll.candidate_id_2 = request.POST['candidate_id_2'] current_poll.candidate_id_3 = request.POST['candidate_id_3'] current_poll_id = request.POST['current_poll_id'] had_exception = False try: fg = models.Flowgram.objects.get(id=current_poll.candidate_id_1) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message(request, "Candidate ID 1 is invalid.") try: fg = models.Flowgram.objects.get(id=current_poll.candidate_id_2) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message(request, "Candidate ID 2 is invalid.") try: fg = models.Flowgram.objects.get(id=current_poll.candidate_id_3) except models.Flowgram.DoesNotExist: had_exception = True models.add_bad_message(request, "Candidate ID 3 is invalid.") if had_exception: return HttpResponseRedirect('/adminpolls/%s' % current_poll_id) current_poll.poll_active = request.POST['poll_active_hidden'].lower() == 'true' current_poll.save() helpers.add_good_message(request, "You modified the poll called %s." % current_poll.poll_title) return helpers.req_render_to_response(request, 'admin/admin_polls.html', {'polls': polls})