<commit_before># -*- coding: utf-8 -*-
import xmlrpclib
import datetime
import decimal
import tablib
import uuid
import logging

from django.core.cache import get_cache
from django.conf import settings
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _

from redis_cache import get_redis_connection

from . import settings as app_settings
from . import tasks


LOG = logging.getLogger(__name__)


def validate_vatid(own_vatid, other_vatid):
    # TODO: Replace evatr or make it optional using a setting
    try:
        server = xmlrpclib.Server('https://evatr.bff-online.de/')
        data = {}
        for item in xmlrpclib.loads(server.evatrRPC(
                own_vatid.replace(' ', ''),
                other_vatid.replace(' ', ''), '', '', '', '', ''))[0]:
            data[item[0]] = item[1]
        return data['ErrorCode'] == '200'
    except:
        return False


def complete_purchase(request, purchase):
    """
    This method finalizes a purchase, clears voucher locks and sends the
    confirmation email.
    """
    if purchase.payment_method == 'invoice':
        purchase.state = 'new'
    else:
        # Credit card payments are automatically marked as payment received
        # if they enter this stage.
        purchase.state = 'payment_received'

    for ticket in purchase.ticket_set.filter(voucher__isnull=False).all():
        voucher = ticket.voucher
        voucher.is_used = True
        voucher.save()
        unlock_voucher(request, voucher)
    purchase.save()
    purchase.invoice_number = generate_invoice_number()
    purchase.save()
    send_purchase_confirmation_mail(purchase)
    tasks.render_invoice.delay(purchase_id=purchase.id)
    return HttpResponseRedirect(reverse('attendees_purchase_done'))


def send_purchase_confirmation_mail(purchase, recipients=None):
    from . import models
    if recipients is None:
        recipients = [purchase.email, settings.DEFAULT_FROM_EMAIL]
    terms_of_use_url = (settings.PURCHASE_TERMS_OF_USE_URL
                        if (hasattr(settings, 'PURCHASE_TERMS_OF_USE_URL')
                        and settings.PURCHASE_TERMS_OF_USE_URL) else '')
    send_mail(
        _('Ticket successfully purchased'),
        render_to_string('attendees/mail_purchase_completed.html', {
            'purchase': purchase,
            'conference': purchase.conference,
            'rounded_vat': round_money_value(purchase.payment_tax),
            'payment_method': dict(models.PAYMENT_METHOD_CHOICES).get(
                purchase.payment_method),
            'terms_of_use_url': terms_of_use_url
        }),
        settings.DEFAULT_FROM_EMAIL, recipients,<commit_msg>Don't send purchase confirmation to DEFAULT_FROM email<commit_after># -*- coding: utf-8 -*-
import xmlrpclib
import datetime
import decimal
import tablib
import uuid
import logging

from django.core.cache import get_cache
from django.conf import settings
from django.core.mail import send_mail
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.template.loader import render_to_string
from django.utils.translation import gettext_lazy as _

from redis_cache import get_redis_connection

from . import settings as app_settings
from . import tasks


LOG = logging.getLogger(__name__)


def validate_vatid(own_vatid, other_vatid):
    # TODO: Replace evatr or make it optional using a setting
    try:
        server = xmlrpclib.Server('https://evatr.bff-online.de/')
        data = {}
        for item in xmlrpclib.loads(server.evatrRPC(
                own_vatid.replace(' ', ''),
                other_vatid.replace(' ', ''), '', '', '', '', ''))[0]:
            data[item[0]] = item[1]
        return data['ErrorCode'] == '200'
    except:
        return False


def complete_purchase(request, purchase):
    """
    This method finalizes a purchase, clears voucher locks and sends the
    confirmation email.
    """
    if purchase.payment_method == 'invoice':
        purchase.state = 'new'
    else:
        # Credit card payments are automatically marked as payment received
        # if they enter this stage.
        purchase.state = 'payment_received'

    for ticket in purchase.ticket_set.filter(voucher__isnull=False).all():
        voucher = ticket.voucher
        voucher.is_used = True
        voucher.save()
        unlock_voucher(request, voucher)
    purchase.save()
    purchase.invoice_number = generate_invoice_number()
    purchase.save()
    send_purchase_confirmation_mail(purchase)
    tasks.render_invoice.delay(purchase_id=purchase.id)
    return HttpResponseRedirect(reverse('attendees_purchase_done'))


def send_purchase_confirmation_mail(purchase, recipients=None):
    from . import models
    if recipients is None:
        recipients = [purchase.email]
    terms_of_use_url = (settings.PURCHASE_TERMS_OF_USE_URL
                        if (hasattr(settings, 'PURCHASE_TERMS_OF_USE_URL')
                        and settings.PURCHASE_TERMS_OF_USE_URL) else '')
    send_mail(
        _('Ticket successfully purchased'),
        render_to_string('attendees/mail_purchase_completed.html', {
            'purchase': purchase,
            'conference': purchase.conference,
            'rounded_vat': round_money_value(purchase.payment_tax),
            'payment_method': dict(models.PAYMENT_METHOD_CHOICES).get(
                purchase.payment_method),
            'terms_of_use_url': terms_of_use_url
        }),
        settings.DEFAULT_FROM_EMAIL, recipients,
{'fraction_non_alphanumeric': 0.09108527131782945, 'fraction_numerical': 0.0024665257223396757, 'mean_word_length': 3.122730573710966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7360754', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1664, 'n_words': 376}