Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaoshan committed Apr 5, 2022
0 parents commit 5ce84dc
Show file tree
Hide file tree
Showing 606 changed files with 201,220 additions and 0 deletions.
Empty file added ajaximage/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions ajaximage/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class AjaximageConfig(AppConfig):
name = 'ajaximage'
58 changes: 58 additions & 0 deletions ajaximage/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#-*- coding: utf-8 -*-
from django.core.files.storage import default_storage
from django.db.models.fields.files import FileDescriptor, FieldFile
from django.db.models import Field
from django.conf import settings
from .widgets import AjaxImageWidget


class AjaxImageField(Field):

storage = default_storage
attr_class = FieldFile
descriptor_class = FileDescriptor

def __init__(self, *args, **kwargs):
upload_to = kwargs.pop('upload_to', '')
max_height = kwargs.pop('max_height', 0)
max_width = kwargs.pop('max_width', 0)
crop = kwargs.pop('crop', False)
crop = 1 if crop is True else 0

if crop is 1 and (max_height is 0 or max_width is 0):
raise Exception('Both max_width and max_height are needed if cropping')

self.widget = AjaxImageWidget(
upload_to=upload_to,
max_width=max_width,
max_height=max_height,
crop=crop
)
super(AjaxImageField, self).__init__(*args, **kwargs)

def contribute_to_class(self, cls, name, virtual_only=False):
super(AjaxImageField, self).contribute_to_class(cls, name, virtual_only)
setattr(cls, self.name, self.descriptor_class(self))

def get_prep_value(self, value):
"""Returns field's value prepared for saving into a database."""
# Need to convert File objects provided via a form to unicode for database insertion
if value is None:
return None
return str(value)

def get_internal_type(self):
return "TextField"

def formfield(self, **kwargs):
defaults = {'widget': self.widget}
defaults.update(kwargs)
return super(AjaxImageField, self).formfield(**defaults)

def url(self):
return 'a'


if 'south' in settings.INSTALLED_APPS:
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^ajaximage\.fields\.AjaxImageField"])
5 changes: 5 additions & 0 deletions ajaximage/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django import forms


class FileForm(forms.Form):
file = forms.FileField()
44 changes: 44 additions & 0 deletions ajaximage/image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import os
from PIL import Image, ImageOps
try:
from StringIO import StringIO as IO
except ImportError:
from io import BytesIO as IO

from django.core.files.base import ContentFile
from django.core.files.uploadedfile import SimpleUploadedFile


def resize(file_, max_width=0, max_height=0, crop=0):
max_width = int(max_width)
max_height = int(max_height)
crop = int(crop)

if(max_width == 0 and max_height == 0):
return file_

max_width = 9999 if max_width == 0 else max_width
max_height = 9999 if max_height == 0 else max_height

size = (max_width, max_height)
image = Image.open(file_)

if(image.mode == 'RGBA'):
image.load()
background = Image.new('RGB', image.size, (255, 255, 255))
background.paste(image, mask=image.split()[3])
image = background

temp = IO()

if(crop == 1):
image = ImageOps.fit(image, size, Image.ANTIALIAS)
else:
image.thumbnail(size, Image.ANTIALIAS)

image.save(temp, 'jpeg')
temp.seek(0)

return SimpleUploadedFile(file_.name,
temp.read(),
content_type='image/jpeg')
Binary file added ajaximage/locale/ru/LC_MESSAGES/django.mo
Binary file not shown.
25 changes: 25 additions & 0 deletions ajaximage/locale/ru/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# Sergey Panasenko <[email protected]>, 2018.
#
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2018-04-19 20:35+0300\n"
"PO-Revision-Date: 2018-04-18 12:02+0300\n"
"Last-Translator: Sergey Panasenko <[email protected]>\n"
"Language-Team: \n"
"Language: ru\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=4; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
"%10<=4 && (n%100<12 || n%100>14) ? 1 : n%10==0 || (n%10>=5 && n%10<=9) || (n"
"%100>=11 && n%100<=14)? 2 : 3);\n"
"X-Generator: Poedit 1.8.7.1\n"

#: ajaximage/widgets.py:24
msgid "Remove"
msgstr "Удалить"
26 changes: 26 additions & 0 deletions ajaximage/static/ajaximage/css/bootstrap-progress.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions ajaximage/static/ajaximage/css/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.ajaximage {
float: left;
}

.ajaximage .progress {
background: #333;
width: 200px;
}

.ajaximage img {
width: 200px;
padding: 5px 0;
display: block;
margin-bottom: 3px;
}

.ajaximage a.file-link:hover img {
opacity: 0.8;
}

.ajaximage .progress,
.ajaximage .file-link,
.ajaximage .file-remove,
.ajaximage .file-input {
display: none;
}

.ajaximage.progress-active .progress,
.ajaximage.img-active .file-link,
.ajaximage.img-active .file-remove,
.ajaximage.form-active .file-input {
display: block;
}
Loading

0 comments on commit 5ce84dc

Please sign in to comment.