예제 #1
0
def wappuheila(request, wph_id=None):
    if wph_id is not None:
        wappuheila = get_object_or_404(Wappuheila, id=wph_id)
        return render_to_request_context_response(request, "wappuheila_details.html", {"wappuheila" : wappuheila})
    
    wappuheilas = Wappuheila.objects.all()
    return render_to_request_context_response(request, "wappuheilas.html", {"wappuheilas" : wappuheilas})
예제 #2
0
def wappuheila_self_admin(request):
    if request.method == 'POST' and request.POST.get('common_pw') == WPH_REGISTER_PASSWORD:
        wappuheila, created = Wappuheila.objects.get_or_create(user=request.user)
        wappuheila_form = WappuheilaForm(request.POST, instance=wappuheila)
        if wappuheila_form.is_valid():
            wappuheila_form.save()
            return render_to_request_context_response(request, 'wappuheila_changed_or_created.html', {'new_wappuheila' : created})
    else:
        try:
            wappuheila = Wappuheila.objects.get(user=request.user)
            wappuheila_form = WappuheilaForm(instance=wappuheila)
        except Wappuheila.DoesNotExist:
            wappuheila_form = WappuheilaForm()
    return render_to_request_context_response(request, 'register_as_wappuheila.html', {'wappuheila_form':wappuheila_form})
예제 #3
0
def results(request, answer=None):
    if answer is None:
            answer = get_object_or_404(Answer, user=request.user)
    
    #calculate the percentage of same answers compared to each wappuheilas answers
    results = []
    for wappuheila in Wappuheila.objects.all():
        wph_answer = Answer.objects.get(user=wappuheila.user)
        same_answers = QuestionOption.objects.filter(id__in=wph_answer.choices.all()).filter(id__in=answer.choices.all()).distinct().count()
        percentage = float(same_answers) / wph_answer.choices.all().count() * 100
        results.append((percentage, wappuheila))
        
    #sort from largest to smallest percentage
    results.sort(reverse=True)
    rcontext = {'results':results}
    
    #if the user is anonymous
    if request.user.is_anonymous():
        answer.choices.clear()
        answer.delete()
    else:
        try:
            rcontext['sent_message'] = Message.objects.get(sender=request.user)
        except Message.DoesNotExist:
            pass;
        
    return render_to_request_context_response(request, 'questions_results.html', rcontext)
예제 #4
0
def questions(request):
    def add_choices_to_answer(formset, answer):
        """
        An utility function to add choices to a persisted Answer-object
        """
        for form in formset:
            tmp_q = form.save(commit=False)
            if tmp_q.is_multiple_choice():
                for choice in form.cleaned_data['choices']:
                    answer.choices.add(choice)
            else:
                choice = form.cleaned_data['choices']
                answer.choices.add(choice)
        answer.save()
    if not request.user.is_anonymous():
        try:
            Answer.objects.get(user=request.user)
            #render without the questions
            return render_to_request_context_response(request, "questions.html")
        except Answer.DoesNotExist:
            pass
    
    #Create a formset to hold the Questions and the choices from which to pick answers    
    QuestionAnswerFormSet = modelformset_factory(Question, form=AnswerForm, extra=0)
    
    #This is an answer
    if request.method == "POST":
        formset = QuestionAnswerFormSet(request.POST)
        if formset.is_valid():
            if request.user.is_anonymous():
                #a hack to display the results for users not participating in the contest
                answer = Answer.objects.create()
                add_choices_to_answer(formset, answer)
                return results(request, answer)
            else:
                answer, created = Answer.objects.get_or_create(user=request.user)
                if not created:
                    answer.choices.clear()
                add_choices_to_answer(formset, answer)
                return redirect_to(request, reverse('questions_results'), False)   
    
    else:
        formset = QuestionAnswerFormSet()
        
    #The questions should be rendered
    return render_to_request_context_response(request, "questions.html", {"formset": formset, })
예제 #5
0
def register(request):
    if request.method == "POST":
        next_url = request.POST.get("next")
        form = forms.UserCreationFormUniqueEmail(request.POST)
        if form.is_valid():
            try:
                user = form.save(commit=False)
                user.is_new_social = False
                user.save()
                messages.success(request, _("Rekisterointi onnistui"))
                user = authenticate(username=request.POST['username'],
                                    password=request.POST['password1'])
                login(request,user)
                return redirect_to(request, next_url, False)
            except ValidationError:
                pass
    else:
        next_url = request.GET.get("next")
        form = forms.UserCreationFormUniqueEmail()
    return render_to_request_context_response(request,"registration/register.html", {"form" : form, "next" : next_url})
예제 #6
0
def user_control_panel(request):
    if request.method == 'POST':
        #handle the possible username change
        userdetails_form = ChangeUserDetailsForm(request.POST,instance = request.user)
        if userdetails_form.is_valid():
            if userdetails_form.cleaned_data['username']:
                request.user.username = userdetails_form.cleaned_data['username']
            if userdetails_form.cleaned_data['first_name']:
                request.user.first_name = userdetails_form.cleaned_data['first_name']
            if userdetails_form.cleaned_data['last_name']:
                request.user.last_name = userdetails_form.cleaned_data['last_name']
            request.user.save()
            messages.success(request, _("Tietojen muutos onnistui."))
        
    else:
        #create an empty username form
        userdetails_form = ChangeUserDetailsForm(instance = request.user)
            
    return render_to_request_context_response(request, "user_control_panel.html", 
                {'userdetails_form':userdetails_form});
예제 #7
0
def home(request):
    return render_to_request_context_response(request, "main_page.html", {"user" : request.user})
예제 #8
0
def show_messages(request):
    messages = Message.objects.filter(Q(sender=request.user) | Q(receiver=request.user))
    return render_to_request_context_response(request, 'messages.html', {'messages':messages});