Esempio n. 1
0
def submit_charity_score(request, charity_username):
    if not request.user.is_authenticated:
        # TODO Raise Authentication Error
        context = error_context_generate('Authentication Error',
                                         ' You are not Signed In!', '')
        template = loader.get_template('accounts/error_page.html')
        return HttpResponse(template.render(context, request))
    if request.user.is_charity:
        context = error_context_generate(
            'Account Type Error',
            'You Can\'t Submit Score for Another Charity!', '')
        # TODO Raise Account Type Error
        template = loader.get_template('accounts/error_page.html')
        return HttpResponse(template.render(context, request))
    benefactor = get_object(Benefactor, user=request.user)
    if benefactor.charity_set.get(user=get_object(
            User, username=request.POST.get('charity_username'))).count <= 0:
        context = error_context_generate(
            'No Cooperation Error',
            'You Cannot Submit Score for a Charity with Which You Had no Cooperation!',
            '')
        # TODO Raise No_Cooperation Error
        template = loader.get_template('accounts/error_page.html')
        return HttpResponse(template.render(context, request))
    try:
        charity = get_object(Charity,
                             user=get_object(User, username=charity_username))
        score = benefactor.charityscore_set.filter(benefactor=benefactor,
                                                   charity=charity).all()[0]
        if score is None:
            score = get_object(CharityScore,
                               charity=charity,
                               benefactor=get_object(Benefactor,
                                                     user=request.user))
        if float(request.POST.get('score')) > 10.0:
            score.score = 10.0
        else:
            score.score = float(request.POST.get('score'))
        score.save()
        Logger.submit_score(request.user, charity.user, None)
        return HttpResponseRedirect([])
    except:
        # TODO raise error
        context = error_context_generate(
            'Unexpected Error',
            'Some of the Data Needed for The Page is Lost or Damaged', '')
        template = loader.get_template('accounts/error_page.html')
        return HttpResponse(template.render(context, request))
Esempio n. 2
0
def submit_benefactor_score(request, ability_id):
    if not request.user.is_authenticated:
        # TODO Raise Authentication Error
        context = error_context_generate('Authentication Error',
                                         'You are not Signed In!', '')
        template = loader.get_template('accounts/error_page.html')
        return HttpResponse(template.render(context, request))
    if request.user.is_benefactor:
        # TODO Raise Account Type Error
        context = error_context_generate(
            'Account Type Error',
            'You Can\'t Submit Score for Another Benefactor!', '')
        template = loader.get_template('accounts/error_page.html')
        return HttpResponse(template.render(context, request))
    ability = get_object(Ability, id=ability_id)
    if ability is None:
        # TODO Raise Not Found Error
        context = error_context_generate('Not Found',
                                         'Requested Ability Cannot be Found',
                                         'accounts:charity_dashboard')
        template = loader.get_template('accounts/error_page.html')
        return HttpResponse(template.render(context, request))
    try:
        benefactor = ability.benefactor
        charity = ability.charity
        charity_projects = [
            nf_project for nf_project in charity.project_set
            if nf_project.type != 'financial'
        ]
        if len([
                project for project in NonFinancialProject.objects.all()
                if project.project in charity_projects
                and project.ability_type is ability.ability_type
        ]) <= 0:
            context = error_context_generate(
                'No Cooperation Error',
                'You Cannot Submit a Score for a Benefactor with Whom You Had no '
                + 'Cooperation On This Ability Type!',
                'accounts:charity_dashboard')
            # TODO Raise No_Cooperation Error
            template = loader.get_template('accounts/error_page.html')
            return HttpResponse(template.render(context, request))
        score = charity.benefactorscore_set.filter(benefactor=benefactor,
                                                   charity=charity).all()[0]
        if score is None:
            score = BenefactorScore.objects.create(ability_type=ability,
                                                   benefactor=benefactor,
                                                   charity=get_object(
                                                       Charity,
                                                       user=request.user))
        if float(request.POST.get('score')) > 10.0:
            score.score = 10.0
        else:
            score.score = float(request.POST.get('score'))
        score.save()
        Logger.submit_score(request.user, benefactor.user, None)
        return HttpResponseRedirect([])
    except:
        context = error_context_generate(
            'Unexpected Error',
            'Some of the Data Needed for The Page is Lost or Damaged', '')
        template = loader.get_template('accounts/error_page.html')
        return HttpResponse(template.render(context, request))