from PIL import Image
def create_thumbnail(self):
if not self.path:
return
DJANGO_TYPE = self.path.file.content_type
if DJANGO_TYPE == 'image/jpeg':
PIL_TYPE = 'jpeg'
FILE_EXTENSION = 'jpg'
elif DJANGO_TYPE == 'image/png':
PIL_TYPE = 'png'
FILE_EXTENSION = 'png'
image = Image.open(StringIO(self.path.read()))
image.thumbnail((200, 200), Image.ANTIALIAS)
temp_handle = StringIO()
image.save(temp_handle, PIL_TYPE)
temp_handle.seek(0)
suf = SimpleUploadedFile(os.path.split(self.path.name)[-1],temp_handle.read(), content_type=DJANGO_TYPE)
self.thumbnail.save('%s.%s'%(os.path.splitext(suf.name)[0],FILE_EXTENSION), suf, save=False)
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
from django.contrib import admin
from claim.models import Claim
from claim.models import EntryImage
from django.db.models.query import QuerySet
from django.contrib.auth.models import User
import datetime
class ImageInline(admin.StackedInline):
model = EntryImage
class ClaimAdmin(admin.ModelAdmin):
list_display = ('client', 'item_product', 'date_add', 'shop', 'prodavec_show', 'claim', 'date_job', 'text_job', 'status')
inlines = (ImageInline,)
def add_view(self, request, form_url='', extra_context=None):
#self.exclude = None
if not request.user.is_superuser:
if request.user.groups.all()[0].id == 2:
self.exclude=('date_job', 'text_job', 'vendors_response', 'solution', 'response_buyer', 'status', 'user', 'shop')
elif request.user.groups.all()[0].id == 4:
self.exclude=('date_job', 'text_job', 'vendors_response', 'solution', 'response_buyer', 'status', 'user')
return super(ClaimAdmin, self).add_view(request, form_url='', extra_context=None)
def prodavec_show(self, obj):
records = User.objects.get(pk=obj.user_id)
return u"%s %s" % (records.last_name,records.first_name)
prodavec_show.short_description = u'Опубликовал'
prodavec_show.allow_tags = True
# сохранение
def save_model(self, request, obj, form, change):
if not obj.id:
if not request.user.is_superuser:
if request.user.groups.all()[0].id == 2:
obj.shop_id = request.user.usershops.shops.all()[0].id
obj.user_id = request.user.id
if request.user.groups.all()[0].id == 4:
obj.user_id = request.user.id
obj.save()
def __init__(self, *args, **kwargs):
super(ClaimAdmin, self).__init__(*args, **kwargs)
self.exclude = None
# связываем эту модель с моделью MkAdmin
admin.site.register(Claim, ClaimAdmin)