import re

from django import forms
from django.forms import ValidationError

from .models import NewUser, Item, ShoppingCartItem


# form for creating a new user
class NewUserForm(forms.ModelForm):
    def clean_first_name(self):
        pattern = re.compile("[a-zA-Z]+$")
        if not pattern.match(self.cleaned_data['first_name']):
            raise ValidationError("Invalid first name")
        return self.cleaned_data['first_name']

    def clean_last_name(self):
        pattern = re.compile("[a-zA-Z]+$")
        if not pattern.match(self.cleaned_data['last_name']):
            raise ValidationError("Invalid last name")
        return self.cleaned_data['last_name']

    class Meta:
        model = NewUser
        fields = [
            'username',
            'first_name',
            'last_name',
            'password',
        ]
        widgets = {
            'password': forms.PasswordInput(),
        }


# form for modifying a user
class UpdateUserInfoForm(forms.ModelForm):
    def clean_credit_card(self):
        pattern = re.compile("^[0-9]{16}$")
        if not pattern.match(self.cleaned_data['credit_card']):
            if self.cleaned_data['credit_card']:
                raise ValidationError("Invalid credit card")
        return self.cleaned_data['credit_card']

    def clean_phone_number(self):
        pattern = re.compile("^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$")
        if not pattern.match(self.cleaned_data['phone_number']):
            if self.cleaned_data['phone_number']:
                raise ValidationError("Invalid phone number")
        return self.cleaned_data['phone_number']

    def clean_address(self):
        pattern = re.compile("[0-9a-zA-Z# ]+$")
        if not pattern.match(self.cleaned_data['address']):
            if self.cleaned_data['address']:
                raise ValidationError("Invalid address")
        return self.cleaned_data['address']

    def clean_first_name(self):
        pattern = re.compile("[a-zA-Z]+$")
        if not pattern.match(self.cleaned_data['first_name']):
            raise ValidationError("Invalid first name")
        return self.cleaned_data['first_name']

    def clean_last_name(self):
        pattern = re.compile("[a-zA-Z]+$")
        if not pattern.match(self.cleaned_data['last_name']):
            raise ValidationError("Invalid last name")
        return self.cleaned_data['last_name']

    class Meta:
        model = NewUser
        fields = [
            'username',
            'first_name',
            'last_name',
            'email',
            'address',
            'phone_number',
            'credit_card',
        ]


# form for searching for items
class ItemSearchForm(forms.ModelForm):
    class Meta:
        model = Item
        fields = [
            'name'
        ]


# form for adding an item to a shopping cart
class NewShoppingCartItemForm(forms.ModelForm):
    class Meta:
        model = ShoppingCartItem
        fields = [
            'user_key',
            'item_key',
            'quantity',
        ]


# form for removing an item from a shopping cart
class RemoveCartItemForm(forms.ModelForm):
    class Meta:
        model = ShoppingCartItem
        fields = [
            'user_key',
            'item_key',
        ]


# form for updating a shopping cart item
class UpdateCartItemForm(forms.ModelForm):
    class Meta:
        model = ShoppingCartItem
        fields = [
            'user_key',
            'item_key',
            'quantity',
        ]

    def validate_unique(self):
        # do nothing as to allow modifying original
        pass


# form for confirming that a user wants to checkout
class ConfirmCheckoutForm(forms.ModelForm):
    cardholder = forms.CharField()
    cvv = forms.CharField()
    exp_date = forms.CharField()

    def clean_cardholder(self):
        pattern = re.compile("^[a-zA-Z ]*$")
        if not pattern.match(self.cleaned_data['cardholder']):
            raise ValidationError("Invalid Cardholder Name")
        return self.cleaned_data['cardholder']

    def clean_cvv(self):
        pattern = re.compile("^[0-9]{3,4}$")
        if not pattern.match(self.cleaned_data['cvv']):
            raise ValidationError("Invalid CVV")
        return self.cleaned_data['cvv']

    def clean_exp_date(self):
        pattern = re.compile("^[0-9]{2}[/][0-9]{2}$")
        if not pattern.match(self.cleaned_data['exp_date']):
            raise ValidationError("Invalid Expiration Date")
        return self.cleaned_data['exp_date']

    def clean_credit_card(self):
        pattern = re.compile("^[0-9]{16}$")
        if not pattern.match(self.cleaned_data['credit_card']):
            raise ValidationError("Invalid credit card")
        return self.cleaned_data['credit_card']

    def clean_address(self):
        pattern = re.compile("[0-9a-zA-Z# ]+$")
        if not pattern.match(self.cleaned_data['address']):
            raise ValidationError("Invalid address")
        return self.cleaned_data['address']

    class Meta:
        model = NewUser
        fields = [
            'address',
            'credit_card',
        ]

93fcc5e37bf4cbd49f22b7765b9ce21ac5ae756e
<mask token>


class UpdateUserInfoForm(forms.ModelForm):
    <mask token>

    def clean_phone_number(self):
        pattern = re.compile('^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$')
        if not pattern.match(self.cleaned_data['phone_number']):
            if self.cleaned_data['phone_number']:
                raise ValidationError('Invalid phone number')
        return self.cleaned_data['phone_number']
    <mask token>
    <mask token>
    <mask token>


    class Meta:
        model = NewUser
        fields = ['username', 'first_name', 'last_name', 'email', 'address',
            'phone_number', 'credit_card']


class ItemSearchForm(forms.ModelForm):


    class Meta:
        model = Item
        fields = ['name']


class NewShoppingCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key', 'quantity']


class RemoveCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key']


class UpdateCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key', 'quantity']

    def validate_unique(self):
        pass


class ConfirmCheckoutForm(forms.ModelForm):
    cardholder = forms.CharField()
    cvv = forms.CharField()
    exp_date = forms.CharField()

    def clean_cardholder(self):
        pattern = re.compile('^[a-zA-Z ]*$')
        if not pattern.match(self.cleaned_data['cardholder']):
            raise ValidationError('Invalid Cardholder Name')
        return self.cleaned_data['cardholder']

    def clean_cvv(self):
        pattern = re.compile('^[0-9]{3,4}$')
        if not pattern.match(self.cleaned_data['cvv']):
            raise ValidationError('Invalid CVV')
        return self.cleaned_data['cvv']

    def clean_exp_date(self):
        pattern = re.compile('^[0-9]{2}[/][0-9]{2}$')
        if not pattern.match(self.cleaned_data['exp_date']):
            raise ValidationError('Invalid Expiration Date')
        return self.cleaned_data['exp_date']

    def clean_credit_card(self):
        pattern = re.compile('^[0-9]{16}$')
        if not pattern.match(self.cleaned_data['credit_card']):
            raise ValidationError('Invalid credit card')
        return self.cleaned_data['credit_card']

    def clean_address(self):
        pattern = re.compile('[0-9a-zA-Z# ]+$')
        if not pattern.match(self.cleaned_data['address']):
            raise ValidationError('Invalid address')
        return self.cleaned_data['address']


    class Meta:
        model = NewUser
        fields = ['address', 'credit_card']

<mask token>


class UpdateUserInfoForm(forms.ModelForm):
    <mask token>

    def clean_phone_number(self):
        pattern = re.compile('^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$')
        if not pattern.match(self.cleaned_data['phone_number']):
            if self.cleaned_data['phone_number']:
                raise ValidationError('Invalid phone number')
        return self.cleaned_data['phone_number']
    <mask token>

    def clean_first_name(self):
        pattern = re.compile('[a-zA-Z]+$')
        if not pattern.match(self.cleaned_data['first_name']):
            raise ValidationError('Invalid first name')
        return self.cleaned_data['first_name']
    <mask token>


    class Meta:
        model = NewUser
        fields = ['username', 'first_name', 'last_name', 'email', 'address',
            'phone_number', 'credit_card']


class ItemSearchForm(forms.ModelForm):


    class Meta:
        model = Item
        fields = ['name']


class NewShoppingCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key', 'quantity']


class RemoveCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key']


class UpdateCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key', 'quantity']

    def validate_unique(self):
        pass


class ConfirmCheckoutForm(forms.ModelForm):
    cardholder = forms.CharField()
    cvv = forms.CharField()
    exp_date = forms.CharField()

    def clean_cardholder(self):
        pattern = re.compile('^[a-zA-Z ]*$')
        if not pattern.match(self.cleaned_data['cardholder']):
            raise ValidationError('Invalid Cardholder Name')
        return self.cleaned_data['cardholder']

    def clean_cvv(self):
        pattern = re.compile('^[0-9]{3,4}$')
        if not pattern.match(self.cleaned_data['cvv']):
            raise ValidationError('Invalid CVV')
        return self.cleaned_data['cvv']

    def clean_exp_date(self):
        pattern = re.compile('^[0-9]{2}[/][0-9]{2}$')
        if not pattern.match(self.cleaned_data['exp_date']):
            raise ValidationError('Invalid Expiration Date')
        return self.cleaned_data['exp_date']

    def clean_credit_card(self):
        pattern = re.compile('^[0-9]{16}$')
        if not pattern.match(self.cleaned_data['credit_card']):
            raise ValidationError('Invalid credit card')
        return self.cleaned_data['credit_card']

    def clean_address(self):
        pattern = re.compile('[0-9a-zA-Z# ]+$')
        if not pattern.match(self.cleaned_data['address']):
            raise ValidationError('Invalid address')
        return self.cleaned_data['address']


    class Meta:
        model = NewUser
        fields = ['address', 'credit_card']

<mask token>


class UpdateUserInfoForm(forms.ModelForm):
    <mask token>

    def clean_phone_number(self):
        pattern = re.compile('^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$')
        if not pattern.match(self.cleaned_data['phone_number']):
            if self.cleaned_data['phone_number']:
                raise ValidationError('Invalid phone number')
        return self.cleaned_data['phone_number']

    def clean_address(self):
        pattern = re.compile('[0-9a-zA-Z# ]+$')
        if not pattern.match(self.cleaned_data['address']):
            if self.cleaned_data['address']:
                raise ValidationError('Invalid address')
        return self.cleaned_data['address']

    def clean_first_name(self):
        pattern = re.compile('[a-zA-Z]+$')
        if not pattern.match(self.cleaned_data['first_name']):
            raise ValidationError('Invalid first name')
        return self.cleaned_data['first_name']
    <mask token>


    class Meta:
        model = NewUser
        fields = ['username', 'first_name', 'last_name', 'email', 'address',
            'phone_number', 'credit_card']


class ItemSearchForm(forms.ModelForm):


    class Meta:
        model = Item
        fields = ['name']


class NewShoppingCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key', 'quantity']


class RemoveCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key']


class UpdateCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key', 'quantity']

    def validate_unique(self):
        pass


class ConfirmCheckoutForm(forms.ModelForm):
    cardholder = forms.CharField()
    cvv = forms.CharField()
    exp_date = forms.CharField()

    def clean_cardholder(self):
        pattern = re.compile('^[a-zA-Z ]*$')
        if not pattern.match(self.cleaned_data['cardholder']):
            raise ValidationError('Invalid Cardholder Name')
        return self.cleaned_data['cardholder']

    def clean_cvv(self):
        pattern = re.compile('^[0-9]{3,4}$')
        if not pattern.match(self.cleaned_data['cvv']):
            raise ValidationError('Invalid CVV')
        return self.cleaned_data['cvv']

    def clean_exp_date(self):
        pattern = re.compile('^[0-9]{2}[/][0-9]{2}$')
        if not pattern.match(self.cleaned_data['exp_date']):
            raise ValidationError('Invalid Expiration Date')
        return self.cleaned_data['exp_date']

    def clean_credit_card(self):
        pattern = re.compile('^[0-9]{16}$')
        if not pattern.match(self.cleaned_data['credit_card']):
            raise ValidationError('Invalid credit card')
        return self.cleaned_data['credit_card']

    def clean_address(self):
        pattern = re.compile('[0-9a-zA-Z# ]+$')
        if not pattern.match(self.cleaned_data['address']):
            raise ValidationError('Invalid address')
        return self.cleaned_data['address']


    class Meta:
        model = NewUser
        fields = ['address', 'credit_card']

import re
from django import forms
from django.forms import ValidationError
from .models import NewUser, Item, ShoppingCartItem


class NewUserForm(forms.ModelForm):

    def clean_first_name(self):
        pattern = re.compile('[a-zA-Z]+$')
        if not pattern.match(self.cleaned_data['first_name']):
            raise ValidationError('Invalid first name')
        return self.cleaned_data['first_name']

    def clean_last_name(self):
        pattern = re.compile('[a-zA-Z]+$')
        if not pattern.match(self.cleaned_data['last_name']):
            raise ValidationError('Invalid last name')
        return self.cleaned_data['last_name']


    class Meta:
        model = NewUser
        fields = ['username', 'first_name', 'last_name', 'password']
        widgets = {'password': forms.PasswordInput()}


class UpdateUserInfoForm(forms.ModelForm):

    def clean_credit_card(self):
        pattern = re.compile('^[0-9]{16}$')
        if not pattern.match(self.cleaned_data['credit_card']):
            if self.cleaned_data['credit_card']:
                raise ValidationError('Invalid credit card')
        return self.cleaned_data['credit_card']

    def clean_phone_number(self):
        pattern = re.compile('^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$')
        if not pattern.match(self.cleaned_data['phone_number']):
            if self.cleaned_data['phone_number']:
                raise ValidationError('Invalid phone number')
        return self.cleaned_data['phone_number']

    def clean_address(self):
        pattern = re.compile('[0-9a-zA-Z# ]+$')
        if not pattern.match(self.cleaned_data['address']):
            if self.cleaned_data['address']:
                raise ValidationError('Invalid address')
        return self.cleaned_data['address']

    def clean_first_name(self):
        pattern = re.compile('[a-zA-Z]+$')
        if not pattern.match(self.cleaned_data['first_name']):
            raise ValidationError('Invalid first name')
        return self.cleaned_data['first_name']

    def clean_last_name(self):
        pattern = re.compile('[a-zA-Z]+$')
        if not pattern.match(self.cleaned_data['last_name']):
            raise ValidationError('Invalid last name')
        return self.cleaned_data['last_name']


    class Meta:
        model = NewUser
        fields = ['username', 'first_name', 'last_name', 'email', 'address',
            'phone_number', 'credit_card']


class ItemSearchForm(forms.ModelForm):


    class Meta:
        model = Item
        fields = ['name']


class NewShoppingCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key', 'quantity']


class RemoveCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key']


class UpdateCartItemForm(forms.ModelForm):


    class Meta:
        model = ShoppingCartItem
        fields = ['user_key', 'item_key', 'quantity']

    def validate_unique(self):
        pass


class ConfirmCheckoutForm(forms.ModelForm):
    cardholder = forms.CharField()
    cvv = forms.CharField()
    exp_date = forms.CharField()

    def clean_cardholder(self):
        pattern = re.compile('^[a-zA-Z ]*$')
        if not pattern.match(self.cleaned_data['cardholder']):
            raise ValidationError('Invalid Cardholder Name')
        return self.cleaned_data['cardholder']

    def clean_cvv(self):
        pattern = re.compile('^[0-9]{3,4}$')
        if not pattern.match(self.cleaned_data['cvv']):
            raise ValidationError('Invalid CVV')
        return self.cleaned_data['cvv']

    def clean_exp_date(self):
        pattern = re.compile('^[0-9]{2}[/][0-9]{2}$')
        if not pattern.match(self.cleaned_data['exp_date']):
            raise ValidationError('Invalid Expiration Date')
        return self.cleaned_data['exp_date']

    def clean_credit_card(self):
        pattern = re.compile('^[0-9]{16}$')
        if not pattern.match(self.cleaned_data['credit_card']):
            raise ValidationError('Invalid credit card')
        return self.cleaned_data['credit_card']

    def clean_address(self):
        pattern = re.compile('[0-9a-zA-Z# ]+$')
        if not pattern.match(self.cleaned_data['address']):
            raise ValidationError('Invalid address')
        return self.cleaned_data['address']


    class Meta:
        model = NewUser
        fields = ['address', 'credit_card']

import re

from django import forms
from django.forms import ValidationError

from .models import NewUser, Item, ShoppingCartItem


# form for creating a new user
class NewUserForm(forms.ModelForm):
    def clean_first_name(self):
        pattern = re.compile("[a-zA-Z]+$")
        if not pattern.match(self.cleaned_data['first_name']):
            raise ValidationError("Invalid first name")
        return self.cleaned_data['first_name']

    def clean_last_name(self):
        pattern = re.compile("[a-zA-Z]+$")
        if not pattern.match(self.cleaned_data['last_name']):
            raise ValidationError("Invalid last name")
        return self.cleaned_data['last_name']

    class Meta:
        model = NewUser
        fields = [
            'username',
            'first_name',
            'last_name',
            'password',
        ]
        widgets = {
            'password': forms.PasswordInput(),
        }


# form for modifying a user
class UpdateUserInfoForm(forms.ModelForm):
    def clean_credit_card(self):
        pattern = re.compile("^[0-9]{16}$")
        if not pattern.match(self.cleaned_data['credit_card']):
            if self.cleaned_data['credit_card']:
                raise ValidationError("Invalid credit card")
        return self.cleaned_data['credit_card']

    def clean_phone_number(self):
        pattern = re.compile("^[0-9]{3}[-][0-9]{3}[-][0-9]{4}$")
        if not pattern.match(self.cleaned_data['phone_number']):
            if self.cleaned_data['phone_number']:
                raise ValidationError("Invalid phone number")
        return self.cleaned_data['phone_number']

    def clean_address(self):
        pattern = re.compile("[0-9a-zA-Z# ]+$")
        if not pattern.match(self.cleaned_data['address']):
            if self.cleaned_data['address']:
                raise ValidationError("Invalid address")
        return self.cleaned_data['address']

    def clean_first_name(self):
        pattern = re.compile("[a-zA-Z]+$")
        if not pattern.match(self.cleaned_data['first_name']):
            raise ValidationError("Invalid first name")
        return self.cleaned_data['first_name']

    def clean_last_name(self):
        pattern = re.compile("[a-zA-Z]+$")
        if not pattern.match(self.cleaned_data['last_name']):
            raise ValidationError("Invalid last name")
        return self.cleaned_data['last_name']

    class Meta:
        model = NewUser
        fields = [
            'username',
            'first_name',
            'last_name',
            'email',
            'address',
            'phone_number',
            'credit_card',
        ]


# form for searching for items
class ItemSearchForm(forms.ModelForm):
    class Meta:
        model = Item
        fields = [
            'name'
        ]


# form for adding an item to a shopping cart
class NewShoppingCartItemForm(forms.ModelForm):
    class Meta:
        model = ShoppingCartItem
        fields = [
            'user_key',
            'item_key',
            'quantity',
        ]


# form for removing an item from a shopping cart
class RemoveCartItemForm(forms.ModelForm):
    class Meta:
        model = ShoppingCartItem
        fields = [
            'user_key',
            'item_key',
        ]


# form for updating a shopping cart item
class UpdateCartItemForm(forms.ModelForm):
    class Meta:
        model = ShoppingCartItem
        fields = [
            'user_key',
            'item_key',
            'quantity',
        ]

    def validate_unique(self):
        # do nothing as to allow modifying original
        pass


# form for confirming that a user wants to checkout
class ConfirmCheckoutForm(forms.ModelForm):
    cardholder = forms.CharField()
    cvv = forms.CharField()
    exp_date = forms.CharField()

    def clean_cardholder(self):
        pattern = re.compile("^[a-zA-Z ]*$")
        if not pattern.match(self.cleaned_data['cardholder']):
            raise ValidationError("Invalid Cardholder Name")
        return self.cleaned_data['cardholder']

    def clean_cvv(self):
        pattern = re.compile("^[0-9]{3,4}$")
        if not pattern.match(self.cleaned_data['cvv']):
            raise ValidationError("Invalid CVV")
        return self.cleaned_data['cvv']

    def clean_exp_date(self):
        pattern = re.compile("^[0-9]{2}[/][0-9]{2}$")
        if not pattern.match(self.cleaned_data['exp_date']):
            raise ValidationError("Invalid Expiration Date")
        return self.cleaned_data['exp_date']

    def clean_credit_card(self):
        pattern = re.compile("^[0-9]{16}$")
        if not pattern.match(self.cleaned_data['credit_card']):
            raise ValidationError("Invalid credit card")
        return self.cleaned_data['credit_card']

    def clean_address(self):
        pattern = re.compile("[0-9a-zA-Z# ]+$")
        if not pattern.match(self.cleaned_data['address']):
            raise ValidationError("Invalid address")
        return self.cleaned_data['address']

    class Meta:
        model = NewUser
        fields = [
            'address',
            'credit_card',
        ]
