Skip to content
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

Feature order customer #28

Merged
merged 10 commits into from
Sep 16, 2021
11 changes: 6 additions & 5 deletions accounts/templates/accounts/customer/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ <h4 class="text-warning"> Profile</h4>
<p>First name : {{ request.user.first_name }}</p>
<p>Last name : {{ request.user.last_name }}</p>
<br>
<a href="{% url 'address:customer-address-list' %}">Address list</a><br><br>
<a href="{% url 'address:customer-address-create' %}">Create new address</a><br><br>
<a class="btn btn-primary" href="{% url 'address:customer-address-list' %}">Address list</a><br><br>
<a class="btn btn-primary" href="{% url 'order:customer-list' %}">Orders</a><br><br>
<a class="btn btn-primary" href="{% url 'address:customer-address-create' %}">Create new address</a><br><br>

<a href="{% url 'accounts:customer-profile-update' %}">Update profile</a><br><br>
<a class="btn btn-primary" href="{% url 'accounts:customer-profile-update' %}">Update profile</a><br><br>

{% if not request.user.password %}
<a href="{% url 'accounts:customer-set-password' %}">Set password</a>
<a class="btn btn-primary" href="{% url 'accounts:customer-set-password' %}">Set password</a>
<br><br>
{% else %}
<a href="{% url 'accounts:customer-change-password' %}">Change password</a>
<a class="btn btn-primary" href="{% url 'accounts:customer-change-password' %}">Change password</a>
<br><br>
{% endif %}

Expand Down
7 changes: 4 additions & 3 deletions order/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
list_display = ('invoice', 'status', 'is_delivered')
list_filter = ('status', 'is_delivered')
search_fields = ('invoice',)
list_display = ('customer', 'status')
list_filter = ('status',)
list_editable = ('status',)
search_fields = ('customer__phone_number',)
22 changes: 22 additions & 0 deletions order/migrations/0003_auto_20210913_1956.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2 on 2021-09-13 15:26

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('order', '0002_auto_20210913_1544'),
]

operations = [
migrations.RemoveField(
model_name='order',
name='is_delivered',
),
migrations.AlterField(
model_name='order',
name='status',
field=models.PositiveSmallIntegerField(choices=[(0, 'preparing food'), (0, 'sending'), (2, 'delivered')], default=0, verbose_name='status'),
),
]
18 changes: 18 additions & 0 deletions order/migrations/0004_alter_order_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2 on 2021-09-13 15:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('order', '0003_auto_20210913_1956'),
]

operations = [
migrations.AlterField(
model_name='order',
name='status',
field=models.PositiveSmallIntegerField(choices=[(0, 'preparing food'), (2, 'sending'), (2, 'delivered')], default=0, verbose_name='status'),
),
]
18 changes: 18 additions & 0 deletions order/migrations/0005_alter_order_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2 on 2021-09-13 15:40

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('order', '0004_alter_order_status'),
]

operations = [
migrations.AlterField(
model_name='order',
name='status',
field=models.PositiveSmallIntegerField(choices=[(0, 'preparing food'), (1, 'sending'), (2, 'delivered')], default=0, verbose_name='status'),
),
]
6 changes: 3 additions & 3 deletions order/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
class Order(BaseModel):
PREPARING_FOOD = 0
SENDING = 1

DELIVERED = 2
STATUS = (
(PREPARING_FOOD, _('preparing food')),
(PREPARING_FOOD, _('sending')),
(SENDING, _('sending')),
(DELIVERED, _('delivered'))
)
invoice = models.OneToOneField(Invoice, verbose_name=_('invoice'), related_name='order', on_delete=models.PROTECT)
customer = models.ForeignKey(Customer, verbose_name=_('customer'), related_name='orders', on_delete=models.PROTECT)
status = models.PositiveSmallIntegerField(verbose_name=_('status'), choices=STATUS, default=PREPARING_FOOD)
is_delivered = models.BooleanField(verbose_name=_('is delivered'), default=False)

class Meta:
verbose_name = _('Order')
Expand Down
38 changes: 38 additions & 0 deletions order/templates/order/customer/order_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{% extends 'base.html' %}
{% block title %} Order Detail {% endblock %}

{% block content %}
<h3> {{ order.invoice.cart.service }} </h3>
<br><br>
<p> status : {{ order.get_status_display }} </p>
<p> order date time : {{ order.created_time }} </p>
<p> address : {{ order.invoice.address.state.name }} - {{ order.invoice.address.city.name }}
- {{ order.invoice.address.area.name }} - {{ order.invoice.address.street }}
- {{ order.invoice.address.alley }} - {{ order.invoice.address.floor }}
- {{ order.invoice.address.plaque }}
</p>
<br><br>
<table class="table table-warning">
<thead>
<tr>
<th scope="col">Item</th>
<th scope="col">Quantity</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>

{% for line in order.invoice.cart.lines.all %}
<tr>
<td>{{ line.item }}</td>
<td>{{ line.quantity }}</td>
<td>$ {{ line.price }}</td>
</tr>
{% endfor %}

</tbody>
</table>

<h5 class="text-black">Total price : {{ order.invoice.price }}</h5>

{% endblock %}
45 changes: 45 additions & 0 deletions order/templates/order/customer/order_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{% extends 'base.html' %}
{% block title %} Orders {% endblock %}

{% block content %}
<h4 style="color: deeppink;" class="text-center">Active orders</h4>
{% if active_orders %}
<ul class="list-group">
{% for order in active_orders %}
<li class="list-group-item">
<a href="{% url 'item:list' order.invoice.cart.service.pk %}"
class="text-decoration-none text-black"><h5>{{ order.invoice.cart.service }}</h5></a>
<p>Status : {{ order.get_status_display }}</p>
<div align="right">
<a href="{% url 'order:customer-detail' order.pk %}"
class="btn btn-warning" style="margin-right: 10px">Detail</a>
</div>
</li>
{% endfor %}
</ul>
{% else %}
<br>
<h6 class="text-center">You have no active orders :(</h6>
{% endif %}
<h4 style="color: deeppink; padding-top: 20px" class="text-center">Delivered orders</h4>
{% if delivered_orders %}
<ul class="list-group">
{% for order in delivered_orders %}
<li class="list-group-item">
<a href="{% url 'item:list' order.invoice.cart.service.pk %}"
class="text-decoration-none text-black"><h5>{{ order.invoice.cart.service }}</h5></a>
<p>Status : {{ order.get_status_display }}</p>
<div align="right">
<a href="{% url 'order:customer-detail' order.pk %}"
class="btn btn-warning" style="margin-right: 10px">Detail</a>
<a href="#"
class="btn btn-primary" style="margin-right: 10px">Leave Comment</a>
</div>
</li>
{% endfor %}
</ul>
{% else %}
<br>
<h6 class="text-center">You have no delivered orders </h6>
{% endif %}
{% endblock %}
10 changes: 10 additions & 0 deletions order/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path

from order.views import CustomerOrdersListView, CustomerOrderDetailView

app_name = 'order'

urlpatterns = [
path('customer/list/', CustomerOrdersListView.as_view(), name='customer-list'),
path('customer/<int:pk>/detail/', CustomerOrderDetailView.as_view(), name='customer-detail'),
]
43 changes: 41 additions & 2 deletions order/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
from django.views.decorators.http import require_http_methods
from django.views.generic import ListView, DetailView

# Create your views here.
from accounts.models import Customer
from accounts.utils import IsCustomer
from library.utils import CustomUserPasses
from order.models import Order


@method_decorator(require_http_methods(['GET']), name='dispatch')
@method_decorator(login_required(login_url=reverse_lazy('accounts:customer-login-register')), name='dispatch')
class CustomerOrdersListView(IsCustomer, ListView):
template_name = 'order/customer/order_list.html'
context_object_name = 'active_orders'

def get_queryset(self):
return Order.objects.select_related('invoice__cart').filter(customer=self.request.user, status__in=(0, 1))

def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(object_list=None, **kwargs)
context['delivered_orders'] = Order.objects.select_related('invoice__cart').filter(customer=self.request.user,
status=2)
return context


@method_decorator(require_http_methods(['GET']), name='dispatch')
@method_decorator(login_required(login_url=reverse_lazy('accounts:customer-login-register')), name='dispatch')
class CustomerOrderDetailView(CustomUserPasses, DetailView):
model = Order
template_name = 'order/customer/order_detail.html'
context_object_name = 'order'

def test_func(self):
user = self.request.user
if not isinstance(user, Customer):
return False
if self.get_object().customer != self.request.user:
return False
return True
2 changes: 1 addition & 1 deletion payment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __str__(self):

def get_request_handler_data(self):
return dict(
amount=self.price, description=self.description, user_email='mohammad@agmil.com',
amount=self.price, description=self.description, user_email=getattr(self.customer, 'email', None),
user_phone_number=self.customer.phone_number, REQUEST_URL=self.gateway.gateway_request_url,
MERCHANT_ID=self.gateway.auth_data, CALL_BACK=CALL_BACK,
)
Expand Down
1 change: 1 addition & 0 deletions snapp_food/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
path('item/', include('item.urls', namespace='item')),
path('cart/', include('cart.urls', namespace='cart')),
path('payment/', include('payment.urls', namespace='payment')),
path('order/', include('order.urls', namespace='order')),

]

Expand Down
2 changes: 1 addition & 1 deletion templates/inc/customer_navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<a class="nav-link" href="{% url 'accounts:customer-profile' %}">Profile</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Order</a>
<a class="nav-link" href="{% url 'order:customer-list' %}">Orders</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'address:customer-address-list' %}">Addresses</a>
Expand Down