Skip to content

Commit

Permalink
create export order to pdf file in admin panel + template
Browse files Browse the repository at this point in the history
  • Loading branch information
behshadrhp committed Nov 10, 2023
1 parent 4482940 commit e80ede6
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 5 deletions.
13 changes: 11 additions & 2 deletions order/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import csv
import datetime
from django.http import HttpResponse

from django.urls import reverse
from django.http import HttpResponse
from django.contrib import admin
from django.utils.safestring import mark_safe

Expand Down Expand Up @@ -47,6 +48,13 @@ def export_to_csv_file(admin_model, request, queryset):
export_to_csv_file.short_description = 'Export To CSV'


def order_export_to_pdf_file(obj):
url = reverse('order:admin_order_pdf', args=[obj.id])
return mark_safe(f'<a href="{url}">PDF</a>')

order_export_to_pdf_file.short_description = 'Invoice'


class OrderItemInline(admin.TabularInline):
model = OrderItem
raw_id_fields = ['product']
Expand All @@ -61,9 +69,10 @@ class OrderAdmin(admin.ModelAdmin):
'city',
'paid',
order_payment,
'create_at'
order_export_to_pdf_file
]

list_filter = ['email', 'paid', 'create_at', 'update_at']
ordering = ['-paid', '-create_at']
inlines = [OrderItemInline]
actions = [export_to_csv_file]
3 changes: 2 additions & 1 deletion order/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
app_name = 'order'

urlpatterns = [
path('create/', views.OrderCreate.as_view(), name='order_create'),
path('create/', views.OrderCreate.as_view(), name='order_create'),
path('/admin/order/<int:order_id>/pdf/', views.admin_order_export_to_pdf, name='admin_order_pdf'),
]
25 changes: 23 additions & 2 deletions order/views.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
from django.conf import settings
from django.http import HttpResponse
from django.urls import reverse
from django.views import View
from django.shortcuts import render, redirect
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.admin.views.decorators import staff_member_required
from django.template.loader import render_to_string

from .models import OrderItem
import weasyprint

from .models import OrderItem, Order
from .forms import OrderCerateForm
from cart.cart import Cart
from .tasks import order_created
Expand Down Expand Up @@ -46,3 +52,18 @@ def post(self, request):

context = {'order': order}
return render(request, 'order/create.html', context)


@staff_member_required
def admin_order_export_to_pdf(request, order_id):
'''
this class is for export data to pdf file.
'''

order = get_object_or_404(Order, id=order_id)
html = render_to_string('export/pdf.html', {'order': order})
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = f'filename=order_{order.id}.pdf'
weasyprint.HTML(string=html).write_pdf(response)

return response
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ psycopg2-binary==2.9.6
python-dotenv==1.0.0
flower==2.0.1
whitenoise==6.5.0
weasyprint==60.1
stripe==7.3.0
121 changes: 121 additions & 0 deletions templates/export/pdf.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
font-family: 'Arial', sans-serif;
margin: 20px;
}

h1 {
color: #333;
border-bottom: 2px solid #333;
padding-bottom: 5px;
}

p {
margin-bottom: 10px;
}

.secondary {
color: #777;
}

h3 {
color: #333;
margin-top: 20px;
}

table {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}

th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}

th {
background-color: #f2f2f2;
}

.row1 {
background-color: #f9f9f9;
}

.row2 {
background-color: #e6e6e6;
}

.total {
background-color: #333;
color: #fff;
}

.num {
text-align: right;
}

.paid {
color: green;
font-weight: bold;
}

.pending {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<h1>My Shop</h1>
<p>
Invoice no. {{ order.id }}<br>
<span class="secondary">{{ order.created|date:"M d, Y" }}</span>
</p>

<h3>Bill to</h3>
<p>
{{ order.first_name }} {{ order.last_name }}<br>
{{ order.email }}<br>
{{ order.address }}<br>
{{ order.postal_code }}, {{ order.city }}
</p>

<h3>Items bought</h3>
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
{% for item in order.items.all %}
<tr class="row{{ forloop.counter|divisibleby:2|yesno:"2,1" }}">
<td>{{ item.product.title }}</td>
<td class="num">${{ item.price }}</td>
<td class="num">{{ item.quantity }}</td>
<td class="num">${{ item.get_cost }}</td>
</tr>
{% endfor %}
<tr class="total">
<td colspan="3">Total</td>
<td class="num">${{ order.get_total_cost }}</td>
</tr>
</tbody>
</table>

<span class="{% if order.paid %}paid{% else %}pending{% endif %}">
{% if order.paid %}Paid{% else %}Pending payment{% endif %}
</span>
</body>
</html>

0 comments on commit e80ede6

Please sign in to comment.