# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
import unicodecsv as csv
import datetime
from django.contrib import admin
from .models import Order, OrderItem
def ExportToCSV(modeladmin, request, queryset):
opts = modeladmin.model._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
ExportToCSV.short_description = 'Export CSV'
class OrderItemInline(admin.TabularInline):
model = OrderItem
raw_id_field = ['product']
class OrderAdmin(admin.ModelAdmin):
list_display = ['id', 'first_name', 'last_name', 'email', 'addres',
'postal_code', 'city', 'paid', 'created', 'update']
list_filter = ['paid', 'created', 'update']
inlines = [OrderItemInline]
actions = [ExportToCSV]
admin.site.register(Order, OrderAdmin)
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from shop.models import Product
class Order(models.Model):
first_name = models.CharField(max_length=50, verbose_name='Имя')
last_name = models.CharField(max_length=50, verbose_name='Фамилия')
email = models.EmailField(verbose_name='Email')
addres = models.CharField(max_length=200, verbose_name='Адрес')
postal_code = models.CharField(max_length=20, verbose_name='Почтовый код')
city = models.CharField(max_length=200, verbose_name='Город')
created = models.DateTimeField(auto_now_add=True, verbose_name='Создан')
update = models.DateTimeField(auto_now=True, verbose_name='Обновлен')
paid = models.BooleanField(default=False, verbose_name='Оплачен')
class Meta:
ordering = ('-created', )
verbose_name = 'Заказ'
verbose_name_plural = 'Заказы'
def __unicode__(self):
return 'Заказ: {}'.format(self.id)
def get_total_cost(self):
return sum(item.get_cost() for item in self.items.all())
class OrderItem(models.Model):
order = models.ForeignKey(Order, related_name='Items')
product = models.ForeignKey(Product, related_name='order_items')
price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='Цена')
quantity = models.PositiveIntegerField(default=1, verbose_name='Количество')
def __unicode__(self):
return '{}'.format(self.id)
def get_cost(self):
return self.price * self.quantity
#social a:hover {
background-color: transparent;opacity:0.7;
}
#social img {
-moz-transition: all 0.8s ease-in-out;
-webkit-transition: all 0.8s ease-in-out;
-o-transition: all 0.8s ease-in-out;
-ms-transition: all 0.8s ease-in-out;
transition: all 0.8s ease-in-out;
}
#social img:hover {
-moz-transform: rotate(360deg);
-webkit-transform: rotate(360deg);
-o-transform: rotate(360deg);
-ms-transform: rotate(360deg);
transform: rotate(360deg);
}
<div id="social">
<div class="clip_frame grpelem" id="u13464" data-muse-uid="U13464" data-muse-type="img_frame"><!-- image -->
<a href="https://vk.com/odrova8" target="_blank"><img class="block" id="u13464_img" title="Страница В контакте" src="{% static 'colyn/images/vkontakte-48x48.png' %}" alt="" width="48" height="48" data-muse-src="{% static 'colyn/images/vkontakte-48x48.png' %}"/></a>
</div>
<!-- /m_editable -->
<!-- m_editable region-id="editable-static-tag-U13449-BP_infinity" template="index.html" data-type="image" -->
<div class="clip_frame grpelem" id="u13449" data-muse-uid="U13449" data-muse-type="img_frame"><!-- image -->
<a href="https://www.facebook.com/groups/132544753956741/" target="_blank"><img class="block" id="u13449_img" title="Страница в Facebook" src="{% static 'colyn/images/facebook-48x48.png' %}" alt="" width="48" height="48" data-muse-src="{% static 'colyn/images/facebook-48x48.png' %}"/></a>
</div>
<!-- /m_editable -->
<!-- m_editable region-id="editable-static-tag-U13457-BP_infinity" template="index.html" data-type="image" -->
<div class="clip_frame grpelem" id="u13457" data-muse-uid="U13457" data-muse-type="img_frame"><!-- image -->
<a href="https://m.ok.ru/group/53238553706648" target="_blank"><img class="block" id="u13457_img" title="Страница в Одноклассниках" src="{% static 'colyn/images/ok.png' %}" alt="" width="48" height="48" data-muse-src="{% static 'colyn/images/ok.png' %}"/></a>
</div>
</div>