Skip to content

Commit

Permalink
order history added
Browse files Browse the repository at this point in the history
  • Loading branch information
Marshmello88 committed Oct 24, 2020
1 parent 58b9ee5 commit 17d4fec
Show file tree
Hide file tree
Showing 28 changed files with 357 additions and 54 deletions.
2 changes: 1 addition & 1 deletion checkout/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OrderLineItemAdminInline(admin.TabularInline):
class OrderAdmin(admin.ModelAdmin):
inlines = (OrderLineItemAdminInline,)

readonly_fields = ('order_number', 'date',
readonly_fields = ('order_number', 'user_profile', 'date',
'delivery_cost', 'order_total',
'total', 'original_cart',
'stripe_pid')
Expand Down
27 changes: 18 additions & 9 deletions checkout/templates/checkout/checkout_success.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,24 +181,33 @@ <h2 class="logo-font mb-4">Thank You</h2>

<div class="row">
<div class="col-12 col-md-4">
<p class="mb-0 text-black font-weight-bold">Grand Total</p>
<p class="mb-0 text-black font-weight-bold"> Total</p>
</div>
<div class="col-12 col-md-8 text-md-right">
<p class="mb-0">{{ order.grand_total }}</p>
<p class="mb-0">{{ order.total }}</p>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 col-lg-7 text-right">
<a href="{% url 'products' %}?category=new_arrivals,deals,clearance" class="btn btn-black rounded-0 my-2">
<span class="icon mr-2">
<i class="fas fa-gifts"></i>
</span>
<span class="text-uppercase">Now check out the latest deals!</span>
</a>
{% if from_profile %}
<a href="{% url 'profile' %}" class="btn btn-black rounded-0 my-2">
<span class="icon mr-2">
<i class="fas fa-angle-left"></i>
</span>
<span class="text-uppercase">Back to Profile</span>
</a>
{% else %}
<a href="{% url 'products' %}?category=new_arrivals,deals,clearance" class="btn btn-black rounded-0 my-2">
<span class="icon mr-2">
<i class="fas fa-gifts"></i>
</span>
<span class="text-uppercase">Now check out the latest deals!</span>
</a>
{% endif %}
</div>
</div>
</div>
{% endblock %}
{% endblock %}
26 changes: 25 additions & 1 deletion checkout/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from .forms import OrderForm
from .models import Order, OrderLineItem
from catalog.models import Product
from profiles.models import UserProfile
from profiles.forms import UserProfileForm
from cart.contexts import cart_contents

import stripe
Expand Down Expand Up @@ -116,6 +118,28 @@ def checkout_success(request, order_number):
"""
save_info = request.session.get('save_info')
order = get_object_or_404(Order, order_number=order_number)

if request.user.is_authenticated:
profile = UserProfile.objects.get(user=request.user)
# Attach the user's profile to the order
order.user_profile = profile
order.save()

# Save the user's info
if save_info:
profile_data = {
'default_phone_number': order.phone_number,
'default_country': order.country,
'default_postcode': order.postcode,
'default_town_or_city': order.town_or_city,
'default_street_address1': order.street_address1,
'default_street_address2': order.street_address2,
'default_county': order.county,
}
user_profile_form = UserProfileForm(profile_data, instance=profile)
if user_profile_form.is_valid():
user_profile_form.save()

messages.success(request, f'Order successfully processed! \
Your order number is {order_number}. A confirmation \
email will be sent to {order.email}.')
Expand All @@ -128,4 +152,4 @@ def checkout_success(request, order_number):
'order': order,
}

return render(request, template, context)
return render(request, template, context)
36 changes: 36 additions & 0 deletions profiles/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django import forms
from .models import UserProfile


class UserProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
exclude = ('user',)

def __init__(self, *args, **kwargs):
"""
Add placeholders and classes, remove auto-generated
labels and set autofocus on first field
"""
super().__init__(*args, **kwargs)
placeholders = {
'default_phone_number': 'Phone Number',
'default_postcode': 'Postal Code',
'default_town_or_city': 'Town or City',
'default_street_address1': 'Street Address 1',
'default_street_address2': 'Street Address 2',
'default_county': 'County, State or Locality',
}

self.fields['default_phone_number'].widget.attrs['autofocus'] = True
for field in self.fields:
if field != 'default_country':
if self.fields[field].required:
placeholder = f'{placeholders[field]} *'
else:
placeholder = placeholders[field]
self.fields[field].widget.attrs['placeholder'] = placeholder
self.fields[field].widget.attrs['class'] = ('border-black '
'rounded-0 '
'profile-form-input')
self.fields[field].label = False
6 changes: 3 additions & 3 deletions profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class UserProfile(models.Model):
"""
user = models.OneToOneField(User, on_delete=models.CASCADE)
default_phone_number = models.CharField(max_length=20, null=True, blank=True)
default_country = CountryField(blank_label='Country *', null=True, blank=True)
default_postcode = models.CharField(max_length=20, null=True, blank=True)
default_town_or_city = models.CharField(max_length=40, null=True, blank=True)
default_street_address1 = models.CharField(max_length=80, null=True, blank=True)
default_street_address2 = models.CharField(max_length=80, null=True, blank=True)
default_town_or_city = models.CharField(max_length=40, null=True, blank=True)
default_county = models.CharField(max_length=80, null=True, blank=True)
default_postcode = models.CharField(max_length=20, null=True, blank=True)
default_country = CountryField(blank_label='Country', null=True, blank=True)

def __str__(self):
return self.user.username
Expand Down
21 changes: 21 additions & 0 deletions profiles/static/profiles/css/profile.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.order-history {
max-height: 416px; /* height of profile form + submit button */
overflow-y: auto;
}

#profile-update-form .form-control {
color: #000;
}

#profile-update-form input::placeholder {
color: #aab7c4;
}

#id_default_country,
#id_default_country option:not(:first-child) {
color: #000;
}

#id_default_country option:first-child {
color: #aab7c4;
}
12 changes: 12 additions & 0 deletions profiles/static/profiles/js/countryfield.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let countrySelected = $('#id_default_country').val();
if(!countrySelected) {
$('#id_default_country').css('color', '#aab7c4');
};
$('#id_default_country').change(function() {
countrySelected = $(this).val();
if(!countrySelected) {
$(this).css('color', '#aab7c4');
} else {
$(this).css('color', '#000');
}
});
54 changes: 54 additions & 0 deletions profiles/templates/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,58 @@ <h2 class="logo-font mb-4">My Profile</h2>
<hr>
</div>
</div>
<div class="row">
<div class="col-12 col-lg-6">
<p class="text-muted">Default Delivery Information</p>
<form class="mt-3" action="{% url 'profile' %}" method="POST" id="profile-update-form">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-black rounded-0 text-uppercase float-right">Update Information</button>
</form>
</div>
<div class="col-12 col-lg-6">
<p class="text-muted">Order History</p>
<div class="order-history table-responsive">
<table class="table table-sm table-borderless">
<thead>
<tr>
<th>Order Number</th>
<th>Date</th>
<th>Items</th>
<th>Order Total</th>
</tr>
</thead>
<tbody>
{% for order in orders %}
<tr>
<td>
<a href="{% url 'order_history' order.order_number %}"
title="{{ order.order_number }}">
{{ order.order_number|truncatechars:6 }}
</a>
</td>
<td>{{ order.date }}</td>
<td>
<ul class="list-unstyled">
{% for item in order.lineitems.all %}
<li class="small">
{{ item.product.name }} x{{ item.quantity }}
</li>
{% endfor %}
</ul>
</td>
<td>${{ order.total }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock %}

{% block postloadjs %}
{{ block.super }}
<script type="text/javascript" src="{% static 'profiles/js/countryfield.js' %}"></script>
{% endblock %}
1 change: 1 addition & 0 deletions profiles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

urlpatterns = [
path('', views.profile, name='profile'),
path('order_history/<order_number>', views.order_history, name='order_history'),
]
46 changes: 44 additions & 2 deletions profiles/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from .models import UserProfile
from .forms import UserProfileForm

from checkout.models import Order


@login_required
def profile(request):
""" Display the user's profile. """
profile = get_object_or_404(UserProfile, user=request.user)

if request.method == 'POST':
form = UserProfileForm(request.POST, instance=profile)
if form.is_valid():
form.save()
messages.success(request, 'Profile updated successfully')
else:
messages.error(request,
('Update failed. Please ensure '
'the form is valid.'))
else:
form = UserProfileForm(instance=profile)
orders = profile.orders.all()

template = 'profile.html'
context = {}
context = {
'form': form,
'orders': orders,
'on_profile_page': True
}

return render(request, template, context)


def order_history(request, order_number):
order = get_object_or_404(Order, order_number=order_number)

messages.info(request, (
f'This is a past confirmation for order number {order_number}. '
'A confirmation email was sent on the order date.'
))

template = 'checkout/checkout_success.html'
context = {
'order': order,
'from_profile': True,
}

return render(request, template, context)
54 changes: 54 additions & 0 deletions static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,58 @@ p {
.carttitle {
text-align: center;
padding-top: 4rem;
}

/* Allauth form formatting */

.allauth-form-inner-content p {
margin-top: 1.5rem; /* mt-4 */
color: #6c757d; /* text-secondary */
}

.allauth-form-inner-content input {
border-color: #000;
border-radius: 0;
}

.allauth-form-inner-content label:not([for='id_remember']) {
display: none;
}

.allauth-form-inner-content input::placeholder {
color: #aab7c4;
}

.allauth-form-inner-content button,
.allauth-form-inner-content input[type='submit'] {
/* btn */
display: inline-block;
font-weight: 400;
color: #fff;
text-align: center;
vertical-align: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-color: #000;
border: 1px solid #000;
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: 0;

/* standard bootstrap btn transitions */
transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}

.allauth-form-inner-content button:hover,
.allauth-form-inner-content input[type='submit']:hover {
color: #fff;
background-color: #222;
border-color: #222;
}

.allauth-form-inner-content a {
color: #17a2b8; /* text-info */
}
6 changes: 4 additions & 2 deletions templates/allauth/account/account_inactive.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

{% block head_title %}{% trans "Account Inactive" %}{% endblock %}

{% block content %}
<h1>{% trans "Account Inactive" %}</h1>
{% block inner_content %}
<hr>
<h2 class="logo-font mb-4">{% trans "Account Inactive" %}</h2>
<hr>

<p>{% trans "This account is inactive." %}</p>
{% endblock %}
Loading

0 comments on commit 17d4fec

Please sign in to comment.