<commit_before>from django.conf import settings
from django.core.urlresolvers import reverse
from django.template import RequestContext, loader
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404

from djforms.processors.forms import TrustCommerceForm as CreditCardForm
from djforms.giving.forms import *
from djforms.core.models import Promotion
from djtools.utils.convert import str_to_class
from djtools.utils.mail import send_mail
from djtools.fields import TODAY

YEAR = TODAY.year
BRICK_PRICES = ["150","500",YEAR-2000+100,YEAR-2000+300]

import os
import logging
logger = logging.getLogger(__name__)

SUBJECT = """Thank you, {} {}, for your donation to Carthage"""

def giving_form(request, transaction, campaign=None):
    """
    multipurpose function to handle various types of donations
    """
    # recipients
    if settings.DEBUG or not settings.TC_LIVE:
        BCC = settings.MANAGERS
    else:
        BCC = settings.GIVING_DONATIONS_BCC

    trans_cap = transaction.capitalize()
    ct_form_name = trans_cap + "ContactForm"
    or_form_name = trans_cap + "OrderForm"
    or_form = str_to_class(
        "djforms.giving.forms", or_form_name
    )
    ct_form = str_to_class(
        "djforms.giving.forms", ct_form_name
    )
    # just checking for bad requests
    if not or_form or not ct_form:
        raise Http404
    # giving campaigns
    if campaign:
        campaign = get_object_or_404(Promotion, slug=campaign)
    else:
        campaign = ""
    status = None
    years = None
    if request.POST:
        ct_form = str_to_class(
            "djforms.giving.forms", ct_form_name
        )(request.POST, prefix="ct")
        or_form = str_to_class(
            "djforms.giving.forms", or_form_name
        )(request.POST, prefix="or")
        if ct_form.is_valid() and or_form.is_valid():
            contact = ct_form.save()
            or_data = or_form.save(commit=False)
            or_data.status = "In Process"
            or_data.operator = "DJForms%s" % trans_cap
            or_data.avs = 0
            # deal with commemorative brick options
            if transaction == "brick" and contact.class_of == str(YEAR):
                if or_data.total == 150:
                    or_data.total = BRICK_PRICES[2]
                elif or_data.total == 500:
                    or_data.total = BRICK_PRICES[3]
                else:
                    raise Http404
            # deal with payments if they have chosen to pledge
            if transaction != "brick" and request.POST.get("or-pledge") != "":
                or_data.payments = request.POST["or-payments"]
                or_data.auth = "store"
                or_data.grand_total = or_data.total
                or_data.total = or_data.total / int(or_data.payments)
                or_data.cycle = "1m"
            else:
                or_data.payments = None
            if campaign:
                or_data.promotion = campaign
            or_data.save()
            contact.order.add(or_data)
            email = contact.email
            cc_form = CreditCardForm(
                or_data, contact, request.POST, prefix="cc"
            )
            if cc_form.is_valid():
                # save and update order
                r = cc_form.processor_response
                or_data.status = r.msg['status']
                or_data.transid = r.msg['transid']
                or_data.billingid = r.msg.get('billingid')
                or_data.cc_name = cc_form.name
                or_data.cc_4_digits = cc_form.card[-4:]
                or_data.save()
                # sendmail
                or_data.contact = contact
                data = {'order':or_data,'years':years}
                subject = SUBJECT.format(contact.first_name, contact.last_name)
                send_mail(
                    request, [email,], subject, email,
                    'giving/%s_email.html' % transaction, data, BCC
                )
                # redirect
                if campaign:
                    url = reverse(
                        'giving_success_campaign',
                        args=[transaction,campaign.slug]
                    )
                else:
                    url = reverse(
                        'giving_success_generic',
                        args=[transaction]
                    )
                return HttpResponseRedirect(url)
            else:
                r = cc_form.processor_response
                if r:
                    or_data.status = r.status
                else:
                    or_data.status = "Blocked"
                or_data.cc_name = cc_form.name
                if cc_form.card:
                    or_data.cc_4_digits = cc_form.card[-4:]
                status = or_data.status
                or_data.save()
                if settings.DEBUG:
                    or_data.contact = contact
                    data = {'order':or_data,'years':years,}
                    subject = SUBJECT.format(
                        contact.first_name, contact.last_name
                    )
                    send_mail(
                        request, [email,], subject, email,
                        'giving/%s_email.html' % transaction, data, BCC
                    )
        else:
            cc_form = CreditCardForm(None, request.POST, prefix="cc")
            cc_form.is_valid()
    else:
        # order form
        init = {}
        if request.GET.get("amount"):
            try:
                # simple way to guard against malicious data
                init["total"] = float(request.GET.get("amount"))
            except:
                pass
        or_form = str_to_class(
            "djforms.giving.forms", or_form_name
        )(prefix="or", initial=init)
        # contact form
        ct_form = str_to_class(
            "djforms.giving.forms", ct_form_name
        )(prefix="ct")
        # credit card
        cc_form = CreditCardForm(prefix="cc")

    # build our template path
    template = 'giving/'
    if campaign:
        template += 'campaigns/{}/'.format(campaign.slug)
    template += '{}_form.html'.format(transaction)

    if not os.path.isfile(os.path.join(settings.ROOT_DIR, "templates", template)):
        raise Http404, "Page not found: {}".format(template)

    return render_to_response(
        template,
        {
            'ct_form': ct_form, 'or_form': or_form, 'form_proc': cc_form,
            'status': status, 'campaign': campaign,'year':str(YEAR)
        },<commit_msg>formatted the amount with two decimal places instead of one 0<commit_after>from django.conf import settings
from django.core.urlresolvers import reverse
from django.template import RequestContext, loader
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render_to_response, get_object_or_404

from djforms.processors.forms import TrustCommerceForm as CreditCardForm
from djforms.giving.forms import *
from djforms.core.models import Promotion
from djtools.utils.convert import str_to_class
from djtools.utils.mail import send_mail
from djtools.fields import TODAY

YEAR = TODAY.year
BRICK_PRICES = ["150","500",YEAR-2000+100,YEAR-2000+300]

import os
import logging
logger = logging.getLogger(__name__)

SUBJECT = """Thank you, {} {}, for your donation to Carthage"""

def giving_form(request, transaction, campaign=None):
    """
    multipurpose function to handle various types of donations
    """
    # recipients
    if settings.DEBUG or not settings.TC_LIVE:
        BCC = settings.MANAGERS
    else:
        BCC = settings.GIVING_DONATIONS_BCC

    trans_cap = transaction.capitalize()
    ct_form_name = trans_cap + "ContactForm"
    or_form_name = trans_cap + "OrderForm"
    or_form = str_to_class(
        "djforms.giving.forms", or_form_name
    )
    ct_form = str_to_class(
        "djforms.giving.forms", ct_form_name
    )
    # just checking for bad requests
    if not or_form or not ct_form:
        raise Http404
    # giving campaigns
    if campaign:
        campaign = get_object_or_404(Promotion, slug=campaign)
    else:
        campaign = ""
    status = None
    years = None
    if request.POST:
        ct_form = str_to_class(
            "djforms.giving.forms", ct_form_name
        )(request.POST, prefix="ct")
        or_form = str_to_class(
            "djforms.giving.forms", or_form_name
        )(request.POST, prefix="or")
        if ct_form.is_valid() and or_form.is_valid():
            contact = ct_form.save()
            or_data = or_form.save(commit=False)
            or_data.status = "In Process"
            or_data.operator = "DJForms%s" % trans_cap
            or_data.avs = 0
            # deal with commemorative brick options
            if transaction == "brick" and contact.class_of == str(YEAR):
                if or_data.total == 150:
                    or_data.total = BRICK_PRICES[2]
                elif or_data.total == 500:
                    or_data.total = BRICK_PRICES[3]
                else:
                    raise Http404
            # deal with payments if they have chosen to pledge
            if transaction != "brick" and request.POST.get("or-pledge") != "":
                or_data.payments = request.POST["or-payments"]
                or_data.auth = "store"
                or_data.grand_total = or_data.total
                or_data.total = or_data.total / int(or_data.payments)
                or_data.cycle = "1m"
            else:
                or_data.payments = None
            if campaign:
                or_data.promotion = campaign
            or_data.save()
            contact.order.add(or_data)
            email = contact.email
            cc_form = CreditCardForm(
                or_data, contact, request.POST, prefix="cc"
            )
            if cc_form.is_valid():
                # save and update order
                r = cc_form.processor_response
                or_data.status = r.msg['status']
                or_data.transid = r.msg['transid']
                or_data.billingid = r.msg.get('billingid')
                or_data.cc_name = cc_form.name
                or_data.cc_4_digits = cc_form.card[-4:]
                or_data.save()
                # sendmail
                or_data.contact = contact
                data = {'order':or_data,'years':years}
                subject = SUBJECT.format(contact.first_name, contact.last_name)
                send_mail(
                    request, [email,], subject, email,
                    'giving/%s_email.html' % transaction, data, BCC
                )
                # redirect
                if campaign:
                    url = reverse(
                        'giving_success_campaign',
                        args=[transaction,campaign.slug]
                    )
                else:
                    url = reverse(
                        'giving_success_generic',
                        args=[transaction]
                    )
                return HttpResponseRedirect(url)
            else:
                r = cc_form.processor_response
                if r:
                    or_data.status = r.status
                else:
                    or_data.status = "Blocked"
                or_data.cc_name = cc_form.name
                if cc_form.card:
                    or_data.cc_4_digits = cc_form.card[-4:]
                status = or_data.status
                or_data.save()
                if settings.DEBUG:
                    or_data.contact = contact
                    data = {'order':or_data,'years':years,}
                    subject = SUBJECT.format(
                        contact.first_name, contact.last_name
                    )
                    send_mail(
                        request, [email,], subject, email,
                        'giving/%s_email.html' % transaction, data, BCC
                    )
        else:
            cc_form = CreditCardForm(None, request.POST, prefix="cc")
            cc_form.is_valid()
    else:
        # order form
        init = {}
        if request.GET.get("amount"):
            try:
                # simple way to guard against malicious data
                init["total"] = "{0:.2f}".format(int(request.GET.get("amount")))
            except:
                pass
        or_form = str_to_class(
            "djforms.giving.forms", or_form_name
        )(prefix="or", initial=init)
        # contact form
        ct_form = str_to_class(
            "djforms.giving.forms", ct_form_name
        )(prefix="ct")
        # credit card
        cc_form = CreditCardForm(prefix="cc")

    # build our template path
    template = 'giving/'
    if campaign:
        template += 'campaigns/{}/'.format(campaign.slug)
    template += '{}_form.html'.format(transaction)

    if not os.path.isfile(os.path.join(settings.ROOT_DIR, "templates", template)):
        raise Http404, "Page not found: {}".format(template)

    return render_to_response(
        template,
        {
            'ct_form': ct_form, 'or_form': or_form, 'form_proc': cc_form,
            'status': status, 'campaign': campaign,'year':str(YEAR)
        },
{'fraction_non_alphanumeric': 0.09279210925644917, 'fraction_numerical': 0.008118361153262518, 'mean_word_length': 1.651579159122913, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1743531', 'n_tokens_mistral': 3918, 'n_tokens_neox': 3599, 'n_words': 794}