You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
143 lines
4.7 KiB
143 lines
4.7 KiB
import csv
|
|
import datetime
|
|
import pytils
|
|
import weasyprint
|
|
from decimal import Decimal
|
|
|
|
from django.conf import settings
|
|
from django.contrib import admin
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.http import HttpResponse
|
|
from django.template.loader import render_to_string
|
|
|
|
from jet.admin import CompactInline
|
|
from jet.filters import DateRangeFilter
|
|
from rangefilter.filter import DateTimeRangeFilter
|
|
|
|
from core.admin import SafeModelAdmin
|
|
from core.models import Certificate
|
|
from eshop_project.settings.base import PAY_REQUISITES
|
|
from .models import (
|
|
Offer, SupplyType,
|
|
Currency, Buying,
|
|
SupplyTarget,
|
|
Order, Discount,
|
|
Client)
|
|
|
|
|
|
class ProductOfferInlineAdmin(CompactInline):
|
|
model = Offer
|
|
exclude = ('status',)
|
|
extra = 1
|
|
show_change_link = 1
|
|
max_num = 1
|
|
|
|
# Supply admins
|
|
|
|
@admin.register(SupplyType)
|
|
class SupplyTypeAdmin(admin.ModelAdmin):
|
|
list_display = ('name', 'min_term', 'max_term')
|
|
search_fields = ('name', 'min_term', 'max_term')
|
|
|
|
|
|
@admin.register(SupplyTarget)
|
|
class SupplyTargetAdmin(admin.ModelAdmin):
|
|
list_display = ('name', 'slug')
|
|
search_fields = ('name', 'slug',)
|
|
|
|
|
|
@admin.register(Discount)
|
|
class DiscountAdmin(admin.ModelAdmin):
|
|
list_display = ['code', 'valid_from', 'valid_to', 'value', 'active']
|
|
list_filter = ['valid_from', 'valid_to', 'active']
|
|
search_field = ['code']
|
|
|
|
|
|
# Offer admins
|
|
@admin.register(Offer)
|
|
class ProductOfferAdmin(SafeModelAdmin):
|
|
list_display = ('product', 'price', 'amount', 'currency')
|
|
search_fields = ('product__name',)
|
|
list_filter = ('currency',)
|
|
|
|
|
|
@admin.register(Buying)
|
|
class BuyingAdmin(SafeModelAdmin):
|
|
def export_buyings_to_csv(self, buying, queryset):
|
|
opts = buying._meta
|
|
response = HttpResponse(content_type='text/csv')
|
|
response['Content-Disposition'] = 'attachment; filename=Orders-{}.csv'.format(
|
|
datetime.datetime.now().strftime("%d/%m/%Y"))
|
|
writer = csv.writer(response)
|
|
|
|
fields = [field for field in opts.get_fields() if not field.many_to_many and not field.one_to_many]
|
|
|
|
writer.writerow([field.verbose_name for field in fields])
|
|
|
|
for obj in queryset:
|
|
data_row = []
|
|
for field in fields:
|
|
value = getattr(obj, field.name)
|
|
if isinstance(value, datetime.datetime):
|
|
value = value.strftime('%d/%m/%Y')
|
|
|
|
data_row.append(value)
|
|
writer.writerow(data_row)
|
|
return response
|
|
export_buyings_to_csv.short_description = _('экспортировать CSV')
|
|
|
|
def print_order_in_pdf(self,buyings):
|
|
verb_price = pytils.numeral.in_words(round(buyings.total_price))
|
|
verb_cur = pytils.numeral.choose_plural(round(buyings.total_price), ("рубль", "рубля", "рублей"))
|
|
html = render_to_string('bootstrap/pdf/buyings.html', {
|
|
**PAY_REQUISITES, 'order': buyings, 'verb_cur': verb_cur, 'verb_price': verb_price
|
|
})
|
|
rendered_html = html.encode(encoding="UTF-8")
|
|
response = HttpResponse(content_type='application/pdf')
|
|
response['Content-Disposition'] = 'filename=order_{}.pdf'.format(buyings.id)
|
|
|
|
|
|
weasyprint.HTML(
|
|
string=rendered_html,
|
|
base_url=self.request.build_absolute_uri()
|
|
).write_pdf(
|
|
response,
|
|
stylesheets = [
|
|
weasyprint.CSS(settings.STATIC_ROOT + '/css/bootstrap.min.css')
|
|
]
|
|
)
|
|
return response
|
|
print_order_in_pdf.short_description = _('Распечатать заказ в pdf')
|
|
|
|
def mark_buyings_as_paid(self, request, queryset):
|
|
for buying in queryset:
|
|
user_profile = buying.user.profile
|
|
if user_profile.parent:
|
|
parent_profile = user_profile.parent.profile
|
|
parent_profile.user_points += round(buying.total_price * Decimal(0.01))
|
|
parent_profile.save()
|
|
buying.status = BUYING_STATUS_PAID
|
|
buying.save()
|
|
mark_buyings_as_paid.short_description = _('Отметить как оплаченные')
|
|
|
|
|
|
inlines = ()
|
|
list_display = ('user', 'offer', 'status', 'amount', 'total_price')
|
|
search_fields = ('user', 'offer',)
|
|
list_filter = (
|
|
('create_at', DateRangeFilter), ('updated_at', DateTimeRangeFilter)
|
|
)
|
|
|
|
actions = (export_buyings_to_csv, print_order_in_pdf, mark_buyings_as_paid)
|
|
|
|
|
|
@admin.register(Order)
|
|
class OrderAdmin(SafeModelAdmin):
|
|
list_display = ('order_code', 'customer_user', 'customer_name', 'customer_email','phone')
|
|
|
|
|
|
@admin.register(Client)
|
|
class ClientAdmin(SafeModelAdmin):
|
|
list_display = ('name','image','status',)
|
|
search_fields = ('name',)
|
|
list_filter = ('status',)
|
|
|