def dashboard(request, comp_url=None): try: request.user.get_profile() except: #catch old users and give them a profile profile = UserProfile(user=request.user) profile.save() competition = get_object_or_404(Competition, hosted_url=comp_url) phase = competition.current_phase max_score = phase.max_score() score_groups = chart_util.score_distribution(phase.all_judgements(), max(phase.max_score() / 20, 1)) if competition.owner != request.user: return HttpResponseRedirect('/no_permissions/') return render_to_response("dashboard/dashboard.html", locals())
def create_new_comp_for_user(user): key = None while True: #find a unique url for the competition key = rand_key(6) try: Competition.objects.get(hosted_url=key) except: break competition = Competition(owner=user, hosted_url=key) competition.save() current_phase = Phase(competition=competition) current_phase.save() competition.current_phase = current_phase competition.save() if not user.get_profile(): #ensure old accounts have a profile profile = UserProfile(user=request.user) profile.selected_competition = competition profile.save() return competition
def judging(request, comp_url=None, judgedpitch_id=None, unjudged_pitch_id=None): competition = None if comp_url: competition = get_object_or_404(Competition, hosted_url=comp_url) elif judgedpitch_id: judgedpitch = get_object_or_404(JudgedPitch, id=judgedpitch_id) competition = judgedpitch.pitch.phase.competition elif unjudged_pitch_id is not None: pitch = get_object_or_404(Pitch, id=unjudged_pitch_id) competition = pitch.phase.competition else: competition = get_competition_for_user(request.user) try: request.user.get_profile() except: #ensure old users have a profile profile = UserProfile(user=request.user) profile.save() judged_pitch = None pitch = None #if we're requesting a specific pitch, only allow it for #either the organizer or the person who did the judging if judgedpitch_id is not None: judged_pitch = get_object_or_404(JudgedPitch, id=judgedpitch_id) if judged_pitch.judge.user != request.user and request.user != judged_pitch.pitch.phase.competition.owner: return HttpResponseRedirect('/no_permissions/') elif unjudged_pitch_id is not None: pitch = get_object_or_404(Pitch, id=unjudged_pitch_id) competition = pitch.phase.competition #they're allowed to judge this particular application if they're either a judge #or the owner of this competition if request.user != competition.owner and JudgeInvitation.objects.filter(user=request.user).filter(competition=competition): #if they've already judged this application, we want to redirect them to #the edit page (which ends up in the same place for now, but it may not always) earlier_pitches = JudgedPitch.objects.filter(pitch=pitch, judge__user=request.user) if earlier_pitches.count() > 0: return HttpResponseRedirect('/judge/review/%s/' % earlier_pitches[0].id) is_organizer = competition.owner == request.user redirect = get_permissions_redirect(request, competition) if redirect: return HttpResponseRedirect(redirect) try: #if a judge thing exists for this user, grab it judge = JudgeInvitation.objects.filter(user=request.user)[0] except: fail = None fail.no_judge_invite() #judging open in current phase? if competition and competition.is_judging_open(): if request.method == "POST": pitch_id = request.POST["pitch"] pitch = Pitch.objects.get(id=pitch_id) try: #look for existing judgement judgement = JudgedPitch.objects.filter(pitch=pitch).get(judge=judge) except: #create new judgement judgement = JudgedPitch(judge=judge, pitch=pitch) judgement.save() if "overall_score" in request.POST: judgement.overall_score = request.POST["overall_score"] judgement.save() for key in request.POST: if key.startswith("answer_") or key.startswith("feedback_"): toks = key.split('_') try: answer_id = int(toks[len(toks) - 1]) #last token is ID except: answer_id = None if key.startswith("answer_q_") or key.startswith("feedback_q_"): question_id = int(toks[len(toks) - 1]) #last token is ID question = PitchQuestion.objects.get(id=question_id) answer = PitchAnswer(question=question, pitch=pitch) answer.save() else: answer = PitchAnswer.objects.get(id=answer_id) try: judged_answer = JudgedAnswer.objects.filter(judged_pitch=judgement).get(answer=answer) except: judged_answer = JudgedAnswer(judged_pitch=judgement, answer=answer) if key.startswith("answer_"): try: judged_answer.score = int(request.POST[key]) except: #this happens when there was no submitted answer, so #they loooose. judged_answer.score = 0 elif key.startswith("feedback_"): judged_answer.feedback = request.POST[key] judged_answer.save() if pitch.phase.pitch_type == "live pitch": #judging live we always go back to the list so they can be sure #to pick the correct next pitch return HttpResponseRedirect('/judge/%s/list/' % competition.hosted_url) elif judgedpitch_id is not None or unjudged_pitch_id is not None: #judging online phases, we go back to the list if they have #already started choosing specific pitches return HttpResponseRedirect('/judge/%s/list/' % competition.hosted_url) else: #if they're judging online ptiches and going via the "go" #option, we just keep feeding them more relevant pitches return HttpResponseRedirect('/judge/%s/go/' % competition.hosted_url) #get a pitch to judge if judged_pitch is not None: pitch = judged_pitch.pitch elif not pitch: pitch = get_next_pitch_to_judge(competition, judge) if pitch: #populate forms questions = pitch.phase.questions() uploads = pitch.phase.uploads() for question in questions: try: question.answer = PitchAnswer.objects.filter(pitch=pitch).get(question=question) except: question.answer = None try: if judged_pitch is not None: try: question.score = JudgedAnswer.objects.filter(judged_pitch=judged_pitch).get(answer=question.answer).score except: question.score = 0 try: question.feedback = JudgedAnswer.objects.filter(judged_pitch=judged_pitch).get(answer=question.answer).feedback except: question.feedback = "" else: question.score = 0 question.feedback = "" except: pass for upload in uploads: try: upload.file = PitchFile.objects.filter(pitch=pitch).get(upload=upload) except: upload.file = None scores = range(1,100) max_score = competition.current_phase.max_score() print 'maxscore: %s' % max_score num_judged = len(competition.current_phase.judgements(judge)) num_to_judge = len(competition.current_phase.pitches_to_judge(judge)) judge_rank = competition.current_phase.judge_rank(judge) #yeah! start showing shit! return render_to_response('judge/judging.html', locals()) else: message = """Judging is complete and all applications have already been assessed. Thanks for your help!""" return render_to_response('util/message.html', locals()) else: #no? tell them when judging opens message = """Hello, Judging for this phase isn't open yet. You'll receive an email as soon as the applications are ready for review. Thanks!""" return render_to_response('util/message.html', locals()) #should have already returned pass