Skip to content

Task 008 #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -6,5 +6,5 @@ DB_PASSWORD=your-db-password
DB_HOST=localhost
STRIPE_LIVE_PUBLIC_KEY=your-live-public-key
STRIPE_LIVE_SECRET_KEY=your-live-secret-key
STRIPE_TEST_PUBLIC_KEY=your-test-public-key
STRIPE_TEST_SECRET_KEY=your-test-secret-key
STRIPE_TEST_PUBLIC_KEY=pk_test_51M6F30LGjE9fhQqPTS1xn299XD7lokUGfB28YhXT98EqRWpikN7nmKmm02G3Rhs3GzmzXBUAGB9U2wlj5GUF7mpA00MEZvJ96P
STRIPE_TEST_SECRET_KEY=sk_test_51M6F30LGjE9fhQqPY3kIzN9j6LmzUnrc9zy7viG4Iarpz7TvEZmr0etlIKpEUgaaSi3I3Db5C42QdnkXu2X2Qr8d00RbhctodS
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@ share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
.env
db.sqlite3
MANIFEST

# PyInstaller
@@ -58,8 +60,8 @@ cover/
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
#db.sqlite3
#db.sqlite3-journal

# Flask stuff:
instance/
8 changes: 8 additions & 0 deletions core/forms.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,11 @@
('P', 'PayPal')
)

DELIVERY_CHOICES = (
('ST', 'Standard (+ 3.00$)'),
('EX', 'Express (+ 6.00$)')
)


class CheckoutForm(forms.Form):
shipping_address = forms.CharField(required=False)
@@ -37,6 +42,9 @@ class CheckoutForm(forms.Form):
payment_option = forms.ChoiceField(
widget=forms.RadioSelect, choices=PAYMENT_CHOICES)

delivery_option = forms.ChoiceField(
widget=forms.RadioSelect, choices=DELIVERY_CHOICES)


class CouponForm(forms.Form):
code = forms.CharField(widget=forms.TextInput(attrs={
18 changes: 18 additions & 0 deletions core/migrations/0005_order_delivery_option.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.14 on 2022-11-21 01:43

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0004_auto_20190630_1408'),
]

operations = [
migrations.AddField(
model_name='order',
name='delivery_option',
field=models.CharField(max_length=20, null=True),
),
]
23 changes: 23 additions & 0 deletions core/migrations/0006_auto_20221120_2004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.2.14 on 2022-11-21 02:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0005_order_delivery_option'),
]

operations = [
migrations.AddField(
model_name='order',
name='deleteTag',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='order',
name='email',
field=models.CharField(max_length=50, null=True),
),
]
18 changes: 18 additions & 0 deletions core/migrations/0007_order_totalorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.14 on 2022-11-24 10:20

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0006_auto_20221120_2004'),
]

operations = [
migrations.AddField(
model_name='order',
name='totalOrder',
field=models.FloatField(null=True),
),
]
19 changes: 19 additions & 0 deletions core/migrations/0008_item_stock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 2.2.14 on 2022-11-28 20:09

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0007_order_totalorder'),
]

operations = [
migrations.AddField(
model_name='item',
name='stock',
field=models.PositiveIntegerField(default=10),
preserve_default=False,
),
]
25 changes: 23 additions & 2 deletions core/models.py
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@ class Item(models.Model):
label = models.CharField(choices=LABEL_CHOICES, max_length=1)
slug = models.SlugField()
description = models.TextField()
stock = models.PositiveIntegerField(default=0)
image = models.ImageField()

def __str__(self):
@@ -87,6 +88,10 @@ def get_final_price(self):
return self.get_total_discount_item_price()
return self.get_total_item_price()

def validate_stock(self):
if self.quantity > self.item.stock:
raise("Not enough stock for this item: ", self.item.title)


class Order(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
@@ -108,6 +113,10 @@ class Order(models.Model):
received = models.BooleanField(default=False)
refund_requested = models.BooleanField(default=False)
refund_granted = models.BooleanField(default=False)
delivery_option = models.CharField(max_length=20, null=True)
email = models.CharField(max_length=50, null=True)
deleteTag = models.BooleanField(default=False)
totalOrder = models.FloatField(null=True)

'''
1. Item added to cart
@@ -127,10 +136,22 @@ def get_total(self):
total = 0
for order_item in self.items.all():
total += order_item.get_final_price()
if self.coupon:
total -= self.coupon.amount
if total < 50.0:
if self.delivery_option == 'ST':
total += 3
if self.delivery_option == 'EX':
total += 6

self.totalOrder = total
self.save()
return total

def standard_delivery(self):
self.totalOrder = self.get_total() + 3

def express_delivery(self):
self.totalOrder = self.get_total() + 6


class Address(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
24 changes: 19 additions & 5 deletions core/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import random
import string

import stripe
from django.conf import settings
from django.contrib import messages
@@ -191,6 +190,13 @@ def post(self, *args, **kwargs):
messages.info(
self.request, "Please fill in the required billing address fields")

delivery_option = form.cleaned_data.get(
'delivery_option')
order.delivery_option = delivery_option
order.save()
order.get_total()
print(order.delivery_option)

payment_option = form.cleaned_data.get('payment_option')

if payment_option == 'S':
@@ -213,7 +219,7 @@ def get(self, *args, **kwargs):
context = {
'order': order,
'DISPLAY_COUPON_FORM': False,
'STRIPE_PUBLIC_KEY' : settings.STRIPE_PUBLIC_KEY
'STRIPE_PUBLIC_KEY': settings.STRIPE_PUBLIC_KEY
}
userprofile = self.request.user.userprofile
if userprofile.one_click_purchasing:
@@ -290,6 +296,9 @@ def post(self, *args, **kwargs):
order_items = order.items.all()
order_items.update(ordered=True)
for item in order_items:
'''product = item.item
product.stock = product.stock - item.quantity
product.save()'''
item.save()

order.ordered = True
@@ -383,9 +392,14 @@ def add_to_cart(request, slug):
# check if the order item is in the order
if order.items.filter(item__slug=item.slug).exists():
order_item.quantity += 1
order_item.save()
messages.info(request, "This item quantity was updated.")
return redirect("core:order-summary")
if order_item.quantity > item.stock:
popUp = "Not enough stock for item: " + item.title
messages.error(request, popUp)
return redirect("core:order-summary")
else:
order_item.save()
messages.info(request, "This item quantity was updated.")
return redirect("core:order-summary")
else:
order.items.add(order_item)
messages.info(request, "This item was added to your cart.")
Binary file modified db.sqlite3
Binary file not shown.
16 changes: 15 additions & 1 deletion djecommerce/settings/development.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .base import *

DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1']
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']

INSTALLED_APPS += [
'debug_toolbar'
@@ -45,3 +45,17 @@ def show_toolbar(request):

STRIPE_PUBLIC_KEY = config('STRIPE_TEST_PUBLIC_KEY')
STRIPE_SECRET_KEY = config('STRIPE_TEST_SECRET_KEY')

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {"class": "logging.StreamHandler"},
},
"loggers": {
"django": {
"handlers": ["console"],
"level": "INFO",
},
}
}
8 changes: 4 additions & 4 deletions static_in_env/css/mdb.min.css

Large diffs are not rendered by default.

39 changes: 37 additions & 2 deletions templates/account/login.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<style>

/* Sign In block */
.row.wow.fadeIn.animated {
margin-top: 60px!important;
}

/* Buttons */

.btn.forgot-password {
background: #b3b3b3!important;
color: #fff!important;
margin-left: 0!important;
margin-right: 0.375rem;
padding-left: 1.5rem;
padding-right: 1.5rem;
border-radius: 0.375rem;
}

.btn.sign-in {
background: #0071bc!important;
color: #fff!important;
margin-right: 0.375rem;
padding-left: 1.5rem;
padding-right: 1.5rem;
border-radius: 0.375rem;
}

</style>
</head>
</html>

{% extends "account/base.html" %}
{% load i18n %}
{% load account socialaccount %}
@@ -43,8 +78,8 @@ <h1>{% trans "Sign In" %}</h1>
{% if redirect_field_value %}
<input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
{% endif %}
<a class="btn btn-default" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a>
<button class="btn btn-primary" type="submit">{% trans "Sign In" %}</button>
<a class="btn forgot-password" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a>
<button class="btn sign-in" type="submit">{% trans "Sign In" %}</button>
</form>
</div>
</div>
42 changes: 35 additions & 7 deletions templates/account/password_reset.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<style>

.row.wow.fadeIn.animated {
margin-top: 60px!important;
}


</style>
</head>
</html>

{% extends "account/base.html" %}

{% load i18n %}
@@ -6,19 +20,33 @@
{% block head_title %}{% trans "Password Reset" %}{% endblock %}

{% block content %}

<h1>{% trans "Password Reset" %}</h1>
<body>
<h1 style="margin-top: 60px!important; margin-left: 450px; ">{% trans "Password Reset" %}</h1>
{% if user.is_authenticated %}
{% include "account/snippets/already_logged_in.html" %}
{% endif %}

<p>{% trans "Forgotten your password? Enter your e-mail address below, and we'll send you an e-mail allowing you to reset it." %}</p>
<p style="margin-left: 450px;">{% trans "Forgotten your password? Enter your e-mail address below, " %}</p>
<p style="margin-left: 450px;">{% trans " and we'll send you an e-mail allowing you to reset it." %}</p>

<form method="POST" action="{% url 'account_reset_password' %}" class="password_reset">
<form method="POST" action="{% url 'account_reset_password' %}" class="password_reset" style="margin-left: 450px;">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Reset My Password' %}" />
<input
style=
"
background: #b3b3b3!important;
color: #fff!important;
border-radius: 0.375rem;
box-shadow: 0 2px 5px 0 rgb(0 0 0 / 16%), 0 2px 10px 0 rgb(0 0 0 / 12%);
border: none;
padding: 0.84rem 2.14rem;
font-size: .81rem;
font-weight: 400;
"
class="btn-default" type="submit" value="{% trans 'RESET MY PASSWORD' %}" />
</form>

<p>{% blocktrans %}Please contact us if you have any trouble resetting your password.{% endblocktrans %}</p>
<p></p>
<p style="margin-left: 450px;">{% blocktrans %}Please contact us if you have any trouble resetting your password.{% endblocktrans %}</p>
</body>
{% endblock %}
Loading