diff --git a/accounts/admin.py b/accounts/admin.py index 20b4b3f..12b4437 100644 --- a/accounts/admin.py +++ b/accounts/admin.py @@ -2,7 +2,7 @@ from django.contrib import admin from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin -from .models import Address, User +from .models import User from .forms import AccountCreateForm, AccountUpdateForm class UserAdmin(UserAdmin): @@ -12,5 +12,4 @@ class UserAdmin(UserAdmin): list_display = ['email', 'username',] -admin.site.register(Address) admin.site.register(User, UserAdmin) diff --git a/accounts/apps.py b/accounts/apps.py index 734d6a1..2606800 100644 --- a/accounts/apps.py +++ b/accounts/apps.py @@ -9,3 +9,4 @@ class AccountsConfig(AppConfig): from .signals import ( user_saved ) + diff --git a/accounts/forms.py b/accounts/forms.py index 84852f6..9762acc 100644 --- a/accounts/forms.py +++ b/accounts/forms.py @@ -2,21 +2,7 @@ from django import forms from django.contrib.auth.forms import UserCreationForm, UserChangeForm from allauth.account.forms import SignupForm from captcha.fields import CaptchaField -from .models import Address, User - - -class AddressForm(forms.ModelForm): - class Meta: - model = Address - fields = ( - 'first_name', - 'last_name', - 'street_address_1', - 'street_address_2', - 'city', - 'state', - 'postal_code', - ) +from .models import User class AccountCreateForm(UserCreationForm): @@ -32,23 +18,26 @@ class AccountUpdateForm(UserChangeForm): 'first_name', 'last_name', 'email', - 'default_shipping_address', - 'addresses', + 'shipping_street_address_1', + 'shipping_street_address_2', + 'shipping_city', + 'shipping_state', + 'shipping_postal_code', ) class CustomerUpdateForm(forms.ModelForm): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.fields['default_shipping_address'].queryset = kwargs['instance'].addresses - class Meta: model = User fields = ( 'first_name', 'last_name', 'email', - 'default_shipping_address', + 'shipping_street_address_1', + 'shipping_street_address_2', + 'shipping_city', + 'shipping_state', + 'shipping_postal_code', ) @@ -62,3 +51,4 @@ class UserSignupForm(SignupForm): widget=forms.TextInput(attrs={'placeholder': 'Last name'}) ) captcha = CaptchaField() + diff --git a/accounts/migrations/0005_remove_user_addresses_and_more.py b/accounts/migrations/0005_remove_user_addresses_and_more.py new file mode 100644 index 0000000..3411df5 --- /dev/null +++ b/accounts/migrations/0005_remove_user_addresses_and_more.py @@ -0,0 +1,29 @@ +# Generated by Django 4.1.6 on 2023-07-15 03:27 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0008_remove_order_billing_address_and_more'), + ('accounts', '0004_transfer_address_data_to_user_model'), + ] + + operations = [ + migrations.RemoveField( + model_name='user', + name='addresses', + ), + migrations.RemoveField( + model_name='user', + name='default_billing_address', + ), + migrations.RemoveField( + model_name='user', + name='default_shipping_address', + ), + migrations.DeleteModel( + name='Address', + ), + ] diff --git a/accounts/models.py b/accounts/models.py index 0911905..8716633 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -5,75 +5,7 @@ from django.contrib.auth.models import AbstractUser from localflavor.us.us_states import USPS_CHOICES -class Address(models.Model): - first_name = models.CharField(max_length=256, blank=True) - last_name = models.CharField(max_length=256, blank=True) - street_address_1 = models.CharField(max_length=256, blank=True) - street_address_2 = models.CharField(max_length=256, blank=True) - city = models.CharField(max_length=256, blank=True) - state = models.CharField( - max_length=2, - choices=USPS_CHOICES, - blank=True - ) - postal_code = models.CharField(max_length=20, blank=True) - - def as_stripe_dict(self): - return { - 'name': f'{self.first_name} {self.last_name}', - 'address': { - 'line1': self.street_address_1, - 'line2': self.street_address_2, - 'city': self.city, - 'state': self.state, - 'postal_code': self.postal_code - } - } - - def __str__(self): - return f""" - {self.first_name} {self.last_name} - {self.street_address_1} - {self.street_address_2} - {self.city}, {self.state}, {self.postal_code} - """ - - def __iter__(self): - yield ('address_line_1', self.street_address_1), - yield ('address_line_2', self.street_address_2), - yield ('admin_area_2', self.city), - yield ('admin_area_1', self.state), - yield ('postal_code', self.postal_code), - yield ('country_code', 'US') - - class Meta: - constraints = [ - models.UniqueConstraint( - name='accounts_address_all_key', - fields=[ - 'first_name', - 'last_name', - 'street_address_1', - 'street_address_2', - 'city', - 'state', - 'postal_code' - ], - violation_error_message='Duplicate: Address already exists.' - ) - ] - - class User(AbstractUser): - addresses = models.ManyToManyField( - Address, blank=True, related_name="user_addresses" - ) - default_shipping_address = models.ForeignKey( - Address, related_name="+", null=True, blank=True, on_delete=models.SET_NULL - ) - default_billing_address = models.ForeignKey( - Address, related_name="+", null=True, blank=True, on_delete=models.SET_NULL - ) stripe_id = models.CharField(max_length=255, blank=True) # Shipping address @@ -87,6 +19,16 @@ class User(AbstractUser): ) shipping_postal_code = models.CharField(max_length=20, blank=True) + @property + def has_shipping_address(self): + if (self.shipping_street_address_1 != '' + and self.shipping_street_address_2 != '' + and self.shipping_city != '' + and self.shipping_state != '' + and self.shipping_postal_code != ''): + return True + return False + def get_or_create_stripe_id(self): if not self.stripe_id: response = stripe.Customer.create( diff --git a/accounts/signals.py b/accounts/signals.py index 02a14db..be22b39 100644 --- a/accounts/signals.py +++ b/accounts/signals.py @@ -5,7 +5,7 @@ from django.dispatch import receiver from django.db import models from django.conf import settings -from .models import Address, User +from .models import User logger = logging.getLogger(__name__) stripe.api_key = settings.STRIPE_API_KEY @@ -16,3 +16,4 @@ def user_saved(sender, instance, created, **kwargs): logger.info('User was saved') if created or not instance.stripe_id: instance.get_or_create_stripe_id() + diff --git a/accounts/utils.py b/accounts/utils.py index 8154408..f41c16c 100644 --- a/accounts/utils.py +++ b/accounts/utils.py @@ -1,26 +1,9 @@ -from allauth.account.models import EmailAddress - -from .models import Address, User -from .tasks import send_account_created_email +from .models import User def get_or_create_customer(request, shipping_address): - address, a_created = Address.objects.get_or_create( - first_name=shipping_address['first_name'], - last_name=shipping_address['last_name'], - street_address_1=shipping_address['street_address_1'], - street_address_2=shipping_address['street_address_2'], - city=shipping_address['city'], - state=shipping_address['state'], - postal_code=shipping_address['postal_code'] - ) - if request.user.is_authenticated: user = request.user - user.addresses.add(address) - if not user.default_shipping_address: - user.default_shipping_address = address - user.save() else: user, u_created = User.objects.get_or_create( email=shipping_address['email'].lower(), @@ -29,27 +12,17 @@ def get_or_create_customer(request, shipping_address): 'is_staff': False, 'is_active': True, 'is_superuser': False, - 'first_name': address.first_name, - 'last_name': address.last_name, - 'default_shipping_address': address, + 'first_name': shipping_address['first_name'], + 'last_name': shipping_address['last_name'], + 'shipping_street_address_1': shipping_address['street_address_1'], + 'shipping_street_address_2': shipping_address['street_address_2'], + 'shipping_city': shipping_address['city'], + 'shipping_state': shipping_address['state'], + 'shipping_postal_code': shipping_address['postal_code'] } ) if u_created: - password = User.objects.make_random_password() - user.set_password(password) - user.addresses.add(address) + user.make_random_password() user.save() - EmailAddress.objects.create( - user=user, email=user.email, primary=True, verified=False - ) - - u = { - 'full_name': user.get_full_name(), - 'email': user.email, - 'password': password - } - - send_account_created_email.delay(u) - - return user, address + return user diff --git a/core/migrations/0006_order_shipping_first_name_order_shipping_last_name_and_more.py b/core/migrations/0006_order_shipping_first_name_order_shipping_last_name_and_more.py new file mode 100644 index 0000000..0acb697 --- /dev/null +++ b/core/migrations/0006_order_shipping_first_name_order_shipping_last_name_and_more.py @@ -0,0 +1,33 @@ +# Generated by Django 4.1.6 on 2023-07-15 03:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0005_transfer_address_data_to_order_and_subscription_models'), + ] + + operations = [ + migrations.AddField( + model_name='order', + name='shipping_first_name', + field=models.CharField(blank=True, max_length=256), + ), + migrations.AddField( + model_name='order', + name='shipping_last_name', + field=models.CharField(blank=True, max_length=256), + ), + migrations.AddField( + model_name='subscription', + name='shipping_first_name', + field=models.CharField(blank=True, max_length=256), + ), + migrations.AddField( + model_name='subscription', + name='shipping_last_name', + field=models.CharField(blank=True, max_length=256), + ), + ] diff --git a/core/migrations/0007_transfer_address_first_and_last_names_to_order_and_subscriptions.py b/core/migrations/0007_transfer_address_first_and_last_names_to_order_and_subscriptions.py new file mode 100644 index 0000000..3db25d8 --- /dev/null +++ b/core/migrations/0007_transfer_address_first_and_last_names_to_order_and_subscriptions.py @@ -0,0 +1,35 @@ +# Generated by Django 4.1.6 on 2023-07-15 03:20 + +from django.db import migrations + + +def copy_address_to_order(apps, schema_editor): + Order = apps.get_model("core", "Order") + + for order in Order.objects.all(): + if order.shipping_address: + order.shipping_first_name = order.shipping_address.first_name + order.shipping_last_name = order.shipping_address.last_name + order.save() + + +def copy_address_to_subscription(apps, schema_editor): + Subscription = apps.get_model("core", "Subscription") + + for subscription in Subscription.objects.all(): + if subscription.shipping_address: + subscription.shipping_first_name = subscription.shipping_address.first_name + subscription.shipping_last_name = subscription.shipping_address.last_name + subscription.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0006_order_shipping_first_name_order_shipping_last_name_and_more'), + ] + + operations = [ + migrations.RunPython(copy_address_to_order), + migrations.RunPython(copy_address_to_subscription) + ] diff --git a/core/migrations/0008_remove_order_billing_address_and_more.py b/core/migrations/0008_remove_order_billing_address_and_more.py new file mode 100644 index 0000000..5cf6af4 --- /dev/null +++ b/core/migrations/0008_remove_order_billing_address_and_more.py @@ -0,0 +1,25 @@ +# Generated by Django 4.1.6 on 2023-07-15 03:27 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0007_transfer_address_first_and_last_names_to_order_and_subscriptions'), + ] + + operations = [ + migrations.RemoveField( + model_name='order', + name='billing_address', + ), + migrations.RemoveField( + model_name='order', + name='shipping_address', + ), + migrations.RemoveField( + model_name='subscription', + name='shipping_address', + ), + ] diff --git a/core/models.py b/core/models.py index ad5c5ae..8b9a480 100644 --- a/core/models.py +++ b/core/models.py @@ -18,7 +18,7 @@ from django.forms.models import model_to_dict from django_measurement.models import MeasurementField from localflavor.us.us_states import USPS_CHOICES -from accounts.models import User, Address +from accounts.models import User from . import ( DiscountValueType, @@ -338,14 +338,9 @@ class Order(models.Model): default=OrderStatus.UNFULFILLED, choices=OrderStatus.CHOICES ) - billing_address = models.ForeignKey( - Address, - related_name="+", - editable=False, - null=True, - on_delete=models.SET_NULL - ) # Shipping address + shipping_first_name = models.CharField(max_length=256, blank=True) + shipping_last_name = models.CharField(max_length=256, blank=True) shipping_street_address_1 = models.CharField(max_length=256, blank=True) shipping_street_address_2 = models.CharField(max_length=256, blank=True) shipping_city = models.CharField(max_length=256, blank=True) @@ -355,13 +350,6 @@ class Order(models.Model): blank=True ) shipping_postal_code = models.CharField(max_length=20, blank=True) - shipping_address = models.ForeignKey( - Address, - related_name="+", - editable=False, - null=True, - on_delete=models.SET_NULL - ) coupon = models.ForeignKey( Coupon, related_name='orders', @@ -556,6 +544,8 @@ class Subscription(models.Model): null=True ) # Shipping address + shipping_first_name = models.CharField(max_length=256, blank=True) + shipping_last_name = models.CharField(max_length=256, blank=True) shipping_street_address_1 = models.CharField(max_length=256, blank=True) shipping_street_address_2 = models.CharField(max_length=256, blank=True) shipping_city = models.CharField(max_length=256, blank=True) @@ -565,13 +555,6 @@ class Subscription(models.Model): blank=True ) shipping_postal_code = models.CharField(max_length=20, blank=True) - shipping_address = models.ForeignKey( - Address, - related_name='+', - editable=False, - null=True, - on_delete=models.SET_NULL - ) items = ArrayField( models.JSONField(blank=True, null=True), default=list diff --git a/core/signals.py b/core/signals.py index 14bac23..c740788 100644 --- a/core/signals.py +++ b/core/signals.py @@ -53,13 +53,13 @@ def transaction_created(sender, instance, created, **kwargs): 'shipping_total': str(instance.order.shipping_total), 'total_amount': str(instance.order.total_amount), 'shipping_address': { - 'first_name': instance.order.shipping_address.first_name, - 'last_name': instance.order.shipping_address.last_name, - 'street_address_1': instance.order.shipping_address.street_address_1, - 'street_address_2': instance.order.shipping_address.street_address_2, - 'city': instance.order.shipping_address.city, - 'state': instance.order.shipping_address.state, - 'postal_code': instance.order.shipping_address.postal_code + 'first_name': instance.order.shipping_first_name, + 'last_name': instance.order.shipping_last_name, + 'street_address_1': instance.order.shipping_street_address_1, + 'street_address_2': instance.order.shipping_street_address_2, + 'city': instance.order.shipping_city, + 'state': instance.order.shipping_state, + 'postal_code': instance.order.shipping_postal_code }, 'line_items': list( format_order_lines(instance.order.lines.all()) diff --git a/dashboard/templates/dashboard/customer/detail.html b/dashboard/templates/dashboard/customer/detail.html index c0828fc..fe2a932 100644 --- a/dashboard/templates/dashboard/customer/detail.html +++ b/dashboard/templates/dashboard/customer/detail.html @@ -26,18 +26,11 @@