<commit_before># -*- coding: utf-8 -*-

"""
byceps.blueprints.shop.forms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2015 <NAME>
"""

from wtforms import DateField, SelectField, StringField
from wtforms.validators import InputRequired, Length

from ...util.l10n import LocalizedForm

from .models import Cart, CartItem, Orderer


class OrderForm(LocalizedForm):
    first_names = StringField('Vorname(n)', validators=[Length(min=2)])
    last_name = StringField('Nachname', validators=[Length(min=2)])
    date_of_birth = DateField('Geburtsdatum', format='%d.%m.%Y', validators=[InputRequired()])
    zip_code = StringField('PLZ', validators=[Length(min=5, max=5)])
    city = StringField('Stadt', validators=[Length(min=2)])
    street = StringField('Straße', validators=[Length(min=2)])

    def get_orderer(self, user):
        return Orderer(
            user,
            self.first_names.data.strip(),
            self.last_name.data.strip(),
            self.date_of_birth.data,
            self.zip_code.data.strip(),
            self.city.data.strip(),
            self.street.data.strip(),
        )


def assemble_articles_order_form(article_compilation):
    """Dynamically extend the order form with one field per article."""

    class ArticlesOrderForm(OrderForm):

        def get_cart(self, articles):
            cart = Cart()
            for item in self.get_cart_items(articles):
                cart.add_item(item)
            return cart

        def get_cart_items(self, article_compilation):
            for item in article_compilation:
                field_name = 'article_{}'.format(item.article.id)
                field = getattr(self, field_name)
                quantity = field.data
                if quantity > 0:
                    yield CartItem(item.article, quantity)


    validators = [InputRequired()]
    for item in article_compilation:
        field_name = 'article_{}'.format(item.article.id)
        choices = _create_choices(item.article)
        field = SelectField('Anzahl', validators, coerce=int, choices=choices)
        setattr(ArticlesOrderForm, field_name, field)

    return ArticlesOrderForm
<commit_msg>Extracted method to generate article form field name.<commit_after># -*- coding: utf-8 -*-

"""
byceps.blueprints.shop.forms
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2015 <NAME>
"""

from wtforms import DateField, SelectField, StringField
from wtforms.validators import InputRequired, Length

from ...util.l10n import LocalizedForm

from .models import Cart, CartItem, Orderer


class OrderForm(LocalizedForm):
    first_names = StringField('Vorname(n)', validators=[Length(min=2)])
    last_name = StringField('Nachname', validators=[Length(min=2)])
    date_of_birth = DateField('Geburtsdatum', format='%d.%m.%Y', validators=[InputRequired()])
    zip_code = StringField('PLZ', validators=[Length(min=5, max=5)])
    city = StringField('Stadt', validators=[Length(min=2)])
    street = StringField('Straße', validators=[Length(min=2)])

    def get_orderer(self, user):
        return Orderer(
            user,
            self.first_names.data.strip(),
            self.last_name.data.strip(),
            self.date_of_birth.data,
            self.zip_code.data.strip(),
            self.city.data.strip(),
            self.street.data.strip(),
        )


def assemble_articles_order_form(article_compilation):
    """Dynamically extend the order form with one field per article."""

    class ArticlesOrderForm(OrderForm):

        def get_cart(self, articles):
            cart = Cart()
            for item in self.get_cart_items(articles):
                cart.add_item(item)
            return cart

        def get_cart_items(self, article_compilation):
            for item in article_compilation:
                field_name = _generate_field_name(item.article)
                field = getattr(self, field_name)
                quantity = field.data
                if quantity > 0:
                    yield CartItem(item.article, quantity)


    validators = [InputRequired()]
    for item in article_compilation:
        field_name = _generate_field_name(item.article)
        choices = _create_choices(item.article)
        field = SelectField('Anzahl', validators, coerce=int, choices=choices)
        setattr(ArticlesOrderForm, field_name, field)

    return ArticlesOrderForm


def _generate_field_name(article):
    return 'article_{}'.format(article.id)

{'fraction_non_alphanumeric': 0.12595591542959964, 'fraction_numerical': 0.008097165991902834, 'mean_word_length': 2.945874001774623, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3732998', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1278, 'n_words': 267}