Related
I am utilizing Django 4.1.3, templated_docs, and Jsignature. I am hoping to be able to place the signature image generated by jsignature directly to the document.
I have attempted almost every combination of utilizing Draw_signature, templated_docs {% image %}, and even {{form.media}} {{form.field}} noted in Jsignature.
any insight here is helpful.
from templated_docs import fill_template
from templated_docs.http import FileResponse
from django.http import HttpResponseNotFound
from invoiceManager.models import Invoice
from jsignature.utils import draw_signature
def invoice_view(request, **kwargs):
pk = kwargs.get('pk', None)
if pk:
# try:
invoice = Invoice.objects.get(pk=pk)
information = {
'repairDate': invoice.repair_date,
'claim': invoice.claim_authorization_number,
'customerName': invoice.customer.name,
'customerAddress': invoice.customer.address,
'customerCityStateZip': f'{invoice.customer.city} {invoice.customer.state.abbreviation}, {invoice.customer.zip_code}',
'customerPhone': invoice.customer.phone_number,
'insuranceName': invoice.insurance.name,
'policyNumber': f'policy Number: {invoice.policy_number}',
'VIN': f'VIN: {invoice.vehicle_information.vin}',
'YMM': f'{invoice.vehicle_information.year} {invoice.vehicle_information.make} {invoice.vehicle_information.model}',
'customerSignature': draw_signature(invoice.signature),
'technician': invoice.technician.name,
'location': invoice.business_location.name,
'submissionDate': invoice.invoice_submission_date,
'amount':invoice.invoice_amount,
}
repairLocations = {f'RL{x}':"Repair Made" for x in invoice.location_of_repairs.all().values_list('id', flat=True).order_by().distinct()}
information.update(repairLocations)
filename = fill_template(
'docs/base_invoice.odt', information,
output_format=".pdf")
visible_filename = 'invoice{}'.format(".pdf")
return FileResponse(filename, visible_filename)
I rewrote the image tag to accept a path allowing the use of jsignature.draw_siganture(, as_file=True). Creating a temp file that can be referenced by PIL and then redrawn -- less than ideal, but allowed to maintain aspect ratio/size of the jsignature field.
from PIL import Image
from django.utils.html import escape
from django import template
register = template.Library()
PIXEL_TO_CM = 0.00846666
class ImageNode(template.Node):
def __init__(self, value):
self.value = template.Variable(value)
def render(self, context):
self.value = self.value.resolve(context)
images = context.dicts[0].setdefault('ootemplate_imgs', {})
id = len(images)
z_index = id + 3 # Magic
photo = Image.open(self.value)
width = photo.size[0] * PIXEL_TO_CM
height = photo.size[0] * PIXEL_TO_CM
return ('<draw:frame draw:style-name="gr%(z_index)s" '
'draw:name="Signature" '
'text:anchor-type="char" svg:width="%(width)fcm" '
'svg:height="%(height)fcm" draw:z-index="%(z_index)s">'
f'<draw:image xlink:href="{self.value}" '
'xlink:type="simple" xlink:show="embed" '
'xlink:actuate="onLoad"/></draw:frame>') % locals()
#register.tag
def template_image(parser, token):
_, path = token.split_contents()
return ImageNode(path)
I wanted to post image processing from views to celery, but it shows me a JSON-related problem
"Object of type ImageForm is not JSON serializable"
Here the code from models.py
class ImageModel(models.Model):
title = models.CharField(max_length=20, null=True)
sec_title = models.CharField(max_length=20, null=True)
img = models.ImageField(upload_to='upload_p/', blank=False)
slug = models.SlugField(max_length=250, null=True)
def delete(self, *args, **kwargs):
self.img.delete()
super().delete(*args, **kwargs)
forms.py
class ImageForm(forms.ModelForm):
class Meta:
model = ImageModel
fields = ('title', 'img', 'sec_title')
views.py
def upload_image(request):
if request.method == 'POST':
form_w = ImageForm(request.POST, request.FILES)
if form_w.is_valid():
water_mark.delay(form_w)
else:
form = ImageForm()
return render(request, 'Luki/upload_img.html', {
'form': form,
})
tasks.py
#shared_task
def water_mark(form_w):
instance = form_w.save(commit=False)
cd = form_w.cleaned_data['img']
if instance.img:
im = Image.open(instance.img)
width, height = im.size
draw = ImageDraw.Draw(im)
text = "TEST WATERMARK"
font = ImageFont.truetype('arial.ttf', 36)
textwidth, textheight = draw.textsize(text, font)
# calculate the x,y coordinates of the text
margin = 10
x = width - textwidth - margin
y = height - textheight - margin
draw.text((x, y), text, font=font)
thumb_io = BytesIO()
print('BYTES IO: ', thumb_io)
im.save(thumb_io, im.format, quality=100)
instance.img.save(str(cd), ContentFile(thumb_io.getvalue()), save=False)
instance.save()
return redirect('Luki:gallery')
Of course, all the libraries are imported and the code from the views without celera is executed and it looks like this. This works so that the photo you add in the form gets a watermark, saves it and takes you to the gallery.
views.py
def upload_image(request):
if request.method == 'POST':
form_w = ImageForm(request.POST, request.FILES)
if form_w.is_valid():
instance = form_w.save(commit=False)
cd = form_w.cleaned_data['img']
if instance.img:
im = Image.open(instance.img)
width, height = im.size
draw = ImageDraw.Draw(im)
text = "TEST WATERMARK"
font = ImageFont.truetype('arial.ttf', 36)
textwidth, textheight = draw.textsize(text, font)
# calculate the x,y coordinates of the text
margin = 10
x = width - textwidth - margin
y = height - textheight - margin
# draw watermark in the bottom right corner
draw.text((x, y), text, font=font)
thumb_io = BytesIO()
im.save(thumb_io, im.format, quality=100)
instance.img.save(str(cd), ContentFile(thumb_io.getvalue()), save=False)
instance.save()
return redirect('Luki:gallery')
else:
form_w = ImageForm()
return render(request, 'Luki/upload_img.html', {
'form_w': form_w,
})
As per this blog, you can not pass object in celery.
Since Celery is a distributed system, you can't know in which process,
or even on what machine the task will run. So you shouldn't pass
Django model objects as arguments to tasks, its almost always better
to re-fetch the object from the database instead, as there are
possible race conditions involved.
So in your case, save the image in model in view itself and pass pk of the model to celery task, and then fetch those details again from the model.
This is what the code with tasks.py looks like and I still have an error.
def upload_image(request):
if request.method == 'POST':
form_w = ImageForm(request.POST, request.FILES)
# files = request.FILES.getlist('img')
if form_w.is_valid():
wm = form_w.save()
water_mark.delay(wm.pk)
return redirect('Luki:gallery')
else:
form_w = ImageForm()
return render(request, 'Luki/upload_img.html', {
'form_w': form_w,
})
And tasks.py
def water_mark(wm):
instance = ImageModel.objects.get(id=wm)
# im = Image.open(instance.img)
# print('im before saving: ', im)
# Writes the image correctly
# im.save('Luki/media/upload_p/zzzta.jpg')
if instance.img:
# upload_p/img-8945.jpg
print('img before opening: ', instance.img)
# returns upload_p/img-8945.jpg
im = Image.open(instance.img)
print('img after: ', im)
# returns <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1920x1080 at 0x5AD75F0>
width, height = im.size
draw = ImageDraw.Draw(im)
text = "TEST WATERMARK"
font = ImageFont.truetype('arial.ttf', 36)
textwidth, textheight = draw.textsize(text, font)
# calculate the x,y coordinates of the text
margin = 10
x = width - textwidth - margin
y = height - textheight - margin
# draw watermark in the bottom right corner
draw.text((x, y), text, font=font)
thumb_io = BytesIO()
print('BYTES IO: ', thumb_io)
im.save(thumb_io, im.format, quality=100)
# FieldFile.save(name, content, save=True)
instance.img.save(str('zzjca.jpg'), ContentFile(thumb_io.getvalue()), save=False)
instance.save()
And it returns an error
File "f:\python\portfoliolk\venv\lib\site-packages\eventlet\greenio\base.py", line 100, in set_nonblocking raise NotImplementedError("set_nonblocking() on a file object " NotImplementedError: set_nonblocking() on a file object with no setblocking() method (Windows pipes don't support non-blocking I/O)
I don't know if it's not windows fault...
I have written the code for Undo & Redo actions for QTableView by using QUndoStack class in PyQt4.
The Undo action works fine; but when I perform Redo action, I am getting "RuntimeError: maximum recursion depth exceeded while calling a Python object."
I have used QTableView's pressed & dataChanged signals to retrieve the texts from model.
Following is the code sample.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtSql import *
import os
import sys
import random
# SQL Query:---------------------------------------------------------------------------------------------
def StudentQuery():
StudentQuery = QSqlQuery()
StudentQuery.exec_("DROP TABLE STUDENTS")
StudentQuery.exec_("""CREATE TABLE STUDENTS (
s1 REAL NULL,
s2 REAL NULL,
s3 REAL NULL)""")
Students = ("STD1", "STD2", "STD3")
StudentQuery.prepare("INSERT INTO STUDENTS (s1, s2, s3) VALUES (?, ?, ?)")
for Student in Students:
s1 = random.randint(0, 25)
s2 = random.randint(0, 25)
s3 = random.randint(0, 25)
StudentQuery.addBindValue(QVariant(s1))
StudentQuery.addBindValue(QVariant(s2))
StudentQuery.addBindValue(QVariant(s3))
StudentQuery.exec_()
QApplication.processEvents()
# Ui Dialog:------------------------------------------------------------------------------------------------
class Ui_Student(QDialog):
def __init__(self, parent=None):
super(Ui_Student, self).__init__(parent)
self.setFixedSize(340, 170)
self.setWindowTitle("STUDENT")
self.model = QSqlTableModel(self)
self.model.setTable("STUDENTS")
self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
self.model.select()
self.view = QTableView(self)
self.view.setGeometry(QRect(10, 10, 320, 120))
self.view.setSelectionBehavior(QAbstractItemView.SelectItems)
self.view.setFocusPolicy(Qt.StrongFocus)
self.view.setModel(self.model)
self.view.installEventFilter(self)
QSqlDatabase.database().commit()
# Button Box:---------------------------------------------------------------------------------------------------
self.buttonBox = QDialogButtonBox(self)
self.buttonBox.setGeometry(QRect(118, 127, 100, 45))
self.buttonBox.setOrientation(Qt.Horizontal)
self.buttonBox.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
# SIGNAL & SLOT: -----------------------------------------------------------------------------------------------
QObject.connect(self.buttonBox, SIGNAL("accepted()"), self.accept)
QObject.connect(self.buttonBox, SIGNAL("rejected()"), self.reject)
# Code For Undo/Redo:---------------------------------------------------------------------------------------------------
self.undoStack = QUndoStack(self)
self.undoStack.setUndoLimit(10)
self.L_Row = []
self.L_Column = []
self.L_Text0 = []
self.L_Text1 = []
self.view.pressed.connect(self.InitialText)
self.view.model().dataChanged.connect(self.FinalText)
def InitialText(self, signal):
self.Row = signal.row()
self.Column = signal.column()
Model = self.view.model()
self.Text0 = Model.data(Model.index(self.Row, self.Column), 0).toString()
self.L_Row.append(self.Row)
self.L_Column.append(self.Column)
self.L_Text0.append(self.Text0)
# print self.L_Text0
def FinalText(self):
View = self.view
Model = self.view.model()
self.Text1 = Model.data(Model.index(self.Row, self.Column), 0).toString()
self.L_Text1.append(self.Text1)
for i in range(len(self.L_Text0)):
if not (self.L_Text0[i] == self.L_Text1[i]):
command = CommandEdit(View, Model, self.L_Row[i], self.L_Column[i],
self.L_Text0[i], self.L_Text1[i], "ABC")
self.undoStack.push(command)
# ContextMenu:---------------------------------------------------------------------------------------------------
def contextMenuEvent(self, event):
menu = QMenu(self)
UndoAction = self.undoStack.createUndoAction(self)
UndoAction.setText("&Undo")
menu.addAction(UndoAction)
UndoAction.setShortcuts(QKeySequence.Undo)
self.connect(UndoAction, SIGNAL("triggered()"), self.undoStack.undo)
RedoAction = self.undoStack.createUndoAction(self)
RedoAction.setText("&Redo")
menu.addAction(RedoAction)
RedoAction.setShortcuts(QKeySequence.Redo)
self.connect(RedoAction, SIGNAL("triggered()"), self.undoStack.redo)
menu.exec_(event.globalPos())
# QUndoCommand Class:---------------------------------------------------------------------------------------------------
class CommandEdit(QUndoCommand):
def __init__(self, View, Model, RowIndex, ColumnIndex, Text0, Text1, description):
super(CommandEdit, self).__init__(description)
self.view = View
self.model = Model
self.rowIndex = RowIndex
self.columnIndex = ColumnIndex
self.text0 = Text0
self.text1 = Text1
def undo(self):
self.model.setData(self.model.index(self.rowIndex, self.columnIndex), QVariant(self.text0))
def redo(self): # Error occurred while executing this function.
self.model.setData(self.model.index(self.rowIndex, self.columnIndex), QVariant(self.text1))
# Code Execute:---------------------------------------------------------------------------------------------------
if __name__ == "__main__":
app = QApplication(sys.argv)
filename = os.path.join(os.path.dirname(__file__), "STD.db")
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(filename)
db.open()
StudentQuery()
form = Ui_Student()
form.show()
sys.exit(app.exec_())
Am I doing something wrong while defining def InitialText & def FinalText?
Non-coder here. I have an activity I'm editing for beginning students. It was created a year ago by someone else. The project is pre-created for students. They are supposed deploy it, create a bucket, and upload some files to the bucket from within the app. When I try it, I get this error:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~lively-armor-126415/1.391710117126333360/main.py", line 205, in get
for imagefile in gcs.listbucket(bucket_path(), delimiter='/'):
File "/base/data/home/apps/s~lively-armor-126415/1.391710117126333360/main.py", line 114, in bucket_path
return '/' + bucket_name + '/'
TypeError: cannot concatenate 'str' and 'NoneType' objects
Here is (part of) main.py:
#!/usr/bin/env python
import webapp2
import sys
import os
import logging
import urllib
import zipfile
import StringIO
import jinja2
import datetime
import mimetypes
import json
from google.appengine.api import users
from google.appengine.api import mail
from google.appengine.api import xmpp
from google.appengine.api import channel
from google.appengine.api import app_identity
from google.appengine.api import images
from google.appengine.api import memcache
from google.appengine.api import taskqueue
from google.appengine.api import search
from google.appengine.ext import ndb
from google.appengine.datastore.datastore_query import Cursor
sys.path.insert(0, 'libs')
libpath = os.path.join(os.path.dirname(__file__), 'lib')
sys.path.append(libpath)
from wtforms import Form
from wtforms import StringField,TextAreaField,SelectField,DecimalField
from wtforms import FileField
from wtforms import SubmitField
from wtforms import validators
import cloudstorage as gcs
from datamodels import Product, ProductCategory
JINJA_ENV = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True
)
# Add custom filter for currency output in JINJA2
def currencyformat(value):
template = "${:.2f}"
currency_string = template.format(value)
return currency_string
JINJA_ENV.filters['currencyformat'] = currencyformat
PRODUCT_GROUPS = [
('1','Bathroom'),
('2','Decor'),
('3','Lumber'),
('4','Materials'),
('5','Outdoors'),
('6','Tools')]
def login_html():
# Load differently based on whether logged in to Google account
user = users.get_current_user()
if user:
url = users.create_logout_url('/')
username = user.nickname()
else:
url = users.create_login_url('/')
username = ''
template_values = {
'url': url,
'username': username
}
greeting_template = JINJA_ENV.get_template('html/greeting.htm')
greeting_html = greeting_template.render(template_values)
return greeting_html
def store_product(prodcode, title, price, category, description):
logging.info('Add product %s to category %s in database', title, category)
category_key = ndb.Key(ProductCategory, category)
product = Product(
parent=category_key,
id=prodcode,
title=title,
price=price,
category=category,
desc=description
)
product.put()
try:
# Create a searchable document to use with Search API
document = search.Document(
doc_id = prodcode,
fields=[
search.TextField(name='title', value=title),
search.TextField(name='category', value=category),
search.HtmlField(name='desc', value=description),
search.NumberField(name='price', value=float(price)),
])
index = search.Index(name="ProductIndex")
index.put(document)
except:
logging.exception("Unable to store search document for " + prodcode)
def file_extension(filename):
return os.path.splitext(filename)[-1]
def bucket_path():
bucket_name = app_identity.get_default_gcs_bucket_name()
return '/' + bucket_name + '/'
class EditProductForm(Form):
# Test and message for currency format
cur_regex = '^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$'
pricemsg = 'Enter a price with up to two decimal places (no dollar symbol)'
prodcode = StringField(
'* Product Code:',
[validators.Length(min=1, max=10)])
price = StringField(
'* Product Price:',
[validators.Regexp(cur_regex, message=pricemsg)])
title = StringField(
'* Product Title:',
[validators.Length(min=1, max=500)])
category = SelectField(
'* Product Group:',
choices=PRODUCT_GROUPS,
default='Hardware')
description = TextAreaField(
'* Product Description:',
[validators.Required()])
submitbtn = SubmitField('Save Product')
class EditImagesForm(Form):
image = FileField('File to Upload:')
submitbtn = SubmitField('Upload')
class BucketImageHandler(webapp2.RequestHandler):
# Return image from cloud storage
def get(self, image_file):
self.response.headers['Content-Type'] = 'image/png'
# Get complete file name
filename = bucket_path() + image_file
cache_name = 'productimages:{}'.format(image_file)
# Get image data from memcache
filedata = memcache.get(cache_name)
if filedata is None:
try:
# Get image from cloud storage
gcs_file = gcs.open(filename)
filedata = gcs_file.read()
memcache.add(cache_name, filedata, 3600)
except:
# Get placeholder image from static images
self.redirect('/images/image_placeholder.png')
self.response.out.write(filedata)
class UploadHandler(webapp2.RequestHandler):
# Display upload page
def get(self):
# Allow only for admin users
if users.is_current_user_admin():
# Delete image if one is passed in
# (in finished site, add a prompt to confirm)
image_filename = self.request.get('del')
if image_filename != '':
datastore_filename = bucket_path() + image_filename
logging.info('>>> DELETED FILE %s', image_filename)
try:
gcs.delete(datastore_filename)
except:
pass
# Gather image data to pass in to HTML template
MAX_IMAGES = 10
image_count = 0
reached_end = True
last_image = 1
start = self.request.get('s')
if start is '':
first_image = 1
else:
first_image = int(start)
if first_image < 1:
first_image = 1
# Get images from Cloud Storage
image_gallery = []
for imagefile in gcs.listbucket(bucket_path(), delimiter='/'):
image_count += 1
reached_first_image = (image_count >= first_image)
reached_last_image = (image_count >= first_image + MAX_IMAGES)
if reached_first_image and not reached_last_image:
# Files to show for this page
filename = imagefile.filename.split('/')[-1]
if file_extension(filename) == '.png':
this_image = dict(
name=filename,
size=imagefile.st_size,
safename=urllib.quote_plus(filename)
)
image_gallery.append(this_image)
last_image = image_count
back_start_index = first_image - MAX_IMAGES
next_start_index = last_image + 1
# Prepare image edit form for HTML template
new_images_form = EditImagesForm()
# Populate batch upload page
template_values = {
'admin_mode': users.is_current_user_admin(),
'greeting_html': login_html(),
'editform': new_images_form,
'gallery': image_gallery,
'start_image_index': first_image,
'end_image_index': last_image,
'image_count': image_count,
'back_start_index': back_start_index,
'next_start_index': next_start_index
}
image_mgr_template = JINJA_ENV.get_template('html/uploadmgr.htm')
image_mgr_html = image_mgr_template.render(template_values)
self.response.write(image_mgr_html)
else:
# Unauthorized user - raise an error
self.abort(401)
# Post new image or batch update to the gallery
def post(self):
# Allow batch upload only for admin users
if users.is_current_user_admin():
file_data = self.request.get('image')
upload_filename = ''
try:
upload_filename = os.path.basename(self.request.POST['image'].filename)
except:
logging.info('NO FILE SPECIFIED')
self.redirect('/upload')
upload_file_extension = file_extension(upload_filename)
datastore_filename = bucket_path() + upload_filename
logging.info('Store file to %s', datastore_filename)
if upload_file_extension == '.png':
# Write image to cloud storage
if len(file_data) > 0:
gcs_file = gcs.open(
datastore_filename,
'w',content_type='image/png')
file_data = images.resize(file_data, 400, 400)
gcs_file.write(file_data)
gcs_file.close()
# Upload done -- return to gallery
self.redirect('/upload')
elif upload_file_extension == '.zip':
# Save uploaded Zip file to Google Cloud Storage
gcs_file = gcs.open(
datastore_filename,
'w',content_type='application/zip')
gcs_file.write(file_data)
gcs_file.close()
logging.info('>>> STORED ZIP FILE %s', datastore_filename)
# Start background task to extract the Zip file
client_id = 'bgmsg-' + users.get_current_user().user_id()
email_address = users.get_current_user().email()
taskqueue.add(
url='/processuploads',
method="POST",
params={'zipfile': datastore_filename,
'address': email_address,
'clientid': client_id,
'starttime': datetime.datetime.now() }
)
# Upload done -- return to gallery
self.redirect('/upload')
else:
# Ignore other file types
self.redirect('/upload')
else:
# Unauthorized user - raise an error
self.abort(401)
class BatchProcessBackgroundHandler(webapp2.RequestHandler):
def post(self):
# Task queue handler - Extract and process uploaded Zip file
# Check header to ensure request came from inside App Engine platform
if 'X-AppEngine-TaskName' in self.request.headers:
zip_file_name = self.request.get('zipfile')
address = self.request.get('address')
client_id = self.request.get('clientid')
start_time = self.request.get('starttime')
# logging.info('>>> EXTRACTING ZIP FILE %s', zip_file_name)
# Get zip data from cloud storage
gcs_file = gcs.open(zip_file_name)
gcs_data = gcs_file.read()
zip_data = StringIO.StringIO(gcs_data)
# Open the archive for reading
zip_file = zipfile.ZipFile(zip_data, 'r')
# Extract each file in the archive and process based on extension
for extracted_file_name in zip_file.namelist():
extracted_file_extension = file_extension(extracted_file_name)
if extracted_file_extension == '.png':
# Read Zip file data as StringIO
extracted_image_data = zip_file.read(extracted_file_name)
# Resize images no wider or taller than 400 pixels
extracted_image_data = images.resize(
extracted_image_data,
400,
400)
datastore_filename = bucket_path() + extracted_file_name
gcs_file = gcs.open(
datastore_filename,
'w',
content_type='image/png')
gcs_file.write(extracted_image_data)
gcs_file.close()
elif extracted_file_extension == '.txt':
extracted_data = zip_file.read(extracted_file_name)
lines = extracted_data.split('\r\n')
for line in lines:
if line:
line_values = line.split('\t')
category = line_values[0]
prodcode = line_values[1]
title = line_values[2]
price = line_values[3]
description = line_values[4]
store_product(
prodcode,
title,
price,
category,
description)
# Close the Zip file
zip_file.close()
# Delete the Zip file when done
gcs.delete(zip_file_name)
# Compose success message
notify_title = 'Batch Update Successfully Completed'
message_body = 'Batch file ' + zip_file_name + '\n'
message_body += 'Started at ' + start_time + '\n'
message_body += 'Finished at ' + str(datetime.datetime.now()) + '\n'
message_body += 'Refresh your browser to see the product updates.\n'
# Send message by email
mail.send_mail(
sender = 'WW Admin <admin#wwheelhouse.com>',
to = address,
subject = notify_title,
body = message_body
)
# Send message by XMPP
user_address = address
chat_message_sent = False
msg = message_body
status_code = xmpp.send_message(user_address, msg)
chat_message_sent = (status_code == xmpp.NO_ERROR)
# Send message to web client via channel API
channel.send_message(
client_id,
msg
)
else:
# Report forbidden operation
self.error(403)
class DeleteProductHandler(webapp2.RequestHandler):
def get(self):
if users.is_current_user_admin():
# Get product code from query string passed in to page
prodcode = self.request.get('edit')
category = self.request.get('cat')
logging.info('>>> GET prodcode=%s and cat=%s', prodcode, category)
try:
# Get product from the datastore
parent_key = ndb.Key('ProductCategory', category)
product = Product.get_by_id(prodcode, parent=parent_key)
# Delete the entity
product.key.delete()
except:
pass
# Redirect back to main product view
self.redirect('/?cat=' + category)
else:
# Report forbidden operation
self.error(403)
class SearchHandler(webapp2.RequestHandler):
def post(self):
# Process search
search_text = self.request.get('q')
search_category = self.request.get('scat')
query_string = "title:" + search_text
query_string += " OR desc:" + search_text
if search_category != '' and search_category != '0':
query_string += " AND category=" + search_category
found_products = ''
num_found = 0
if search_text != '':
index = search.Index(name="ProductIndex")
found_products = index.search(query_string)
num_found = found_products.number_found
# Populate search results page
template_values = {
'admin_mode': users.is_current_user_admin(),
'greeting_html': login_html(),
'prod_categories': PRODUCT_GROUPS,
'selected': search_category,
'search_text': search_text,
'product_list': found_products,
'num_found': num_found
}
results_template = JINJA_ENV.get_template('html/searchresults.htm')
search_html = results_template.render(template_values)
self.response.write(search_html)
class MainHandler(webapp2.RequestHandler):
def get(self):
editcode = self.request.get('edit')
prod_category = self.request.get('cat',default_value='0')
in_edit = (prod_category and editcode)
if in_edit:
product = Product.query_get_product(prod_category, editcode)
new_product_form = EditProductForm(
prodcode=editcode,
title=product.title,
price=product.price,
category=prod_category,
description=product.desc
)
else:
# Produce empty product editing form
new_product_form = EditProductForm()
self.response.write(self.catalog_html(new_product_form))
# logging.info("ENVIRONMENT: %s", os.environ)
def post(self):
if users.is_current_user_admin():
# Get data submitted in form and validate user input
prodcode = self.request.get('prodcode')
title = self.request.get('title')
price = self.request.get('price')
category = self.request.get('category')
description = self.request.get('description')
new_product_form = EditProductForm(
prodcode=prodcode,
title=title,
price=price,
category=category,
description=description
)
if new_product_form.validate():
store_product(prodcode, title, price, category, description)
self.redirect('/?cat='+category+'&viewproduct='+prodcode)
else:
html = self.catalog_html(new_product_form)
self.response.write(html)
else:
# Unauthorized user -- raise an error
self.abort(401)
def catalog_html(self, editform):
""" Return HTML for the product catalog """
PRODUCTS_PER_PAGE = 4
viewcode = self.request.get('viewproduct')
editcode = self.request.get('edit')
category = self.request.get('cat', default_value='0')
in_edit = (category and editcode) # Show Edit mode only if category and editcode provided
in_one_product_view = viewcode != ''
# If one product view or in edit, show single product
if in_one_product_view or in_edit:
# RETURN SINGLE PRODUCT VIEW
if in_edit:
# Query to get the product specified for editing
product = Product.query_get_product(category, editcode)
else:
# Query to get the product specified for viewing
product = Product.query_get_product(category, viewcode)
# Populate catalog page
template_values = {
'admin_mode': users.is_current_user_admin(),
'greeting_html': login_html(),
'prod_categories': PRODUCT_GROUPS,
'selected': category,
'product': product,
'editform': editform
}
one_product_template = JINJA_ENV.get_template('html/oneproduct.htm')
one_product_html = one_product_template.render(template_values)
return one_product_html
else:
# MULTIPLE PRODUCT VIEW
if category == '0':
# Show all products in all categories
q_forward = Product.query_all_categories_sort_newest()
q_backward = Product.query_all_categories_sort_oldest()
else:
# Show products in one category
q_forward = Product.query_by_category_sort_newest(category)
q_backward = Product.query_by_category_sort_oldest(category)
page_nav = ''
products = None
num_products_in_query = q_forward.count()
num_pages_to_show = num_products_in_query / PRODUCTS_PER_PAGE
if (num_products_in_query % PRODUCTS_PER_PAGE) > 0:
num_pages_to_show += 1
cursor_was_passed_in = self.request.get('cursor') is not ''
page_num = self.request.get('pg',default_value='1')
prev_cur = ''
next_cur = ''
if num_products_in_query > 0:
# Read the cursor passed in from the previous page
cursor = Cursor(urlsafe=self.request.get('cursor'))
# Fetch a forward cursor
products, next_cursor, more_after = q_forward.fetch_page(
PRODUCTS_PER_PAGE,
start_cursor=cursor )
# Fetch a backward cursor
prev_products, prev_cursor, more_before = q_backward.fetch_page(
PRODUCTS_PER_PAGE,
start_cursor=cursor.reversed() )
if cursor_was_passed_in and prev_cursor:
prev_cur = prev_cursor.urlsafe()
if more_after and next_cursor:
next_cur = next_cursor.urlsafe()
# Populate catalog page
template_values = {
'admin_mode': users.is_current_user_admin(),
'greeting_html': login_html(),
'catalog': products,
'prod_categories': PRODUCT_GROUPS,
'selected': category,
'editform': editform,
'prev_cur': prev_cur,
'next_cur': next_cur,
'page_num': page_num,
'page_num_prev': int(page_num)-1,
'page_num_next': int(page_num)+1,
'num_pages_to_show': num_pages_to_show
}
catalog_template=JINJA_ENV.get_template('html/catalog.htm')
return catalog_template.render(template_values)
...
Please help. I don't have a clue how to fix this, but I need to get this project working for the students to use.
Thanks so much,
Chrys
That's because there's no default bucket attached to your current project.
You can create a bucket with gsutil or the cloud console and then hardcode the value in your bucket_path function.
Also you can keep your current bucket_path helper function that returns the bucket_name from app_identity.get_default_gcs_bucket_name() and then create a default bucket in the new console by visiting your app engine's application settings in the cloud console https://console.cloud.google.com/appengine/settings?project=
I have problem with getting additional resized images from croped image. I am using django-cropper 0.1.
Originally model for Cropped Image have part of code like this:
class Cropped(models.Model):
def __unicode__(self):
return u'%s-%sx%s' % (self.original, self.w, self.h)
def upload_image(self, filename):
return '%s/crop-%s' % (settings.ROOT, filename)
def save(self, *args, **kwargs): #force_insert=False, force_update=False, using=None):
source = self.original.image.path
target = self.upload_image(os.path.basename(source))
Image.open(source).crop([
self.x, # Left
self.y, # Top
self.x + self.w, # Right
self.y + self.h # Bottom
]).save(django_settings.MEDIA_ROOT + os.sep + target)
self.image = target
super(Cropped, self).save(*args, **kwargs)
But i want to have additional resized images from cropped image so I change code a little and now it looks like that:
class Cropped(models.Model):
def __unicode__(self):
return u'%s-%sx%s' % (self.original, self.w, self.h)
def _get_average_path(self):
return _add_average(self.path)
average_path = property(_get_average_path)
def _get_average_url(self):
return _add_average(self.url)
average_url = property(_get_average_url)
def _get_large_path(self):
return _add_large(self.path)
large_path = property(_get_large_path)
def _get_large_url(self):
return _add_large(self.url)
large_url = property(_get_large_url)
def upload_image(self, filename):
return '%s/crop-%s' % (settings.ROOT, filename)
def save(self, *args, **kwargs): #force_insert=False, force_update=False, using=None):
source = self.original.image.path
target = self.upload_image(os.path.basename(source))
img = Image.open(source).crop([
self.x, # Left
self.y, # Top
self.x + self.w, # Right
self.y + self.h # Bottom
]).save(django_settings.MEDIA_ROOT + os.sep + target)
self.image = target
super(Cropped, self).save(*args, **kwargs)
img = Image.open(self.image.path)
img = img.resize((180, 180), Image.ANTIALIAS)
img.save(self.large_path, 'JPEG')
img = img.resize((104, 104), Image.ANTIALIAS)
img.save(self.average_path, 'JPEG')
But it still doing the basic job. Can someone help me and give any suggestion of what should I fix in this code?
I answer my own question because i found solution and maybe it someone. Maybe it is now the best solution, but it works. First it is needed to add additional field for additional, resized image:
average = models.ImageField(
blank=True, null=True,
verbose_name = _('Image'),
upload_to = upload_image,
editable = False,
)
Then it is needed to chnge save() method to something like this:
def save(self, *args, **kwargs): #force_insert=False, force_update=False, using=None):
source = self.original.image.path
target = self.upload_image(os.path.basename(source))
Image.open(source).crop([
self.x, # Left
self.y, # Top
self.x + self.w, # Right
self.y + self.h # Bottom
]).save(django_settings.MEDIA_ROOT + os.sep + target)
self.image = target
splited_target = target.split("/")
avepath = "/average-".join(splited_target)
self.average = avepath
Image.open(self.image).resize((104, 104), Image.ANTIALIAS).save(django_settings.MEDIA_ROOT + avepath, 'JPEG')
super(Cropped, self).save(*args, **kwargs)
And that is all.