def delete_image(request): locstore_key = request.values.get('locstore_key', None) image_key = request.values.get('image_key', None) locstore = None if locstore_key: locstore = memcache.get('get_locstore_%s' % locstore_key) if not locstore: locstore = LocStore.get(locstore_key) if locstore is None: return redirect('/admin/locstore/list/') img = ThumbImage.get(image_key) if img is None: locstore.images.remove(image_key) locstore.put() memcache.set('get_locstore_%s' % locstore.key(), locstore, MEMCACHE_TIMEOUT) return redirect('/admin/locstore/edit/%s/' % locstore.key()) locstore.images.remove(image_key) if locstore.main_image == image_key: if len(locstore.images): locstore.main_image=locstore.images[0] else: locstore.main_image = '' img.delete() if not len(locstore.images): locstore.main_image = '' locstore.put() memcache.set('get_locstore_%s' % locstore.key(), locstore, MEMCACHE_TIMEOUT) return redirect('/admin/locstore/edit/%s/' % locstore.key())
def internal_thumb_image(value, size="700x700"): if value is None: return '' thumb = ThumbImage.get(value) if thumb: try: att = getattr(thumb, "thumb_%s" % size) except AttributeError: return '' return Image.get(att).image return ''
def get_img_url(key): img = ThumbImage.get(key) img_url = '' if img: try: att = getattr(img, "thumb_700x700") i = Image.get(att) img_url = i.get_link() except AttributeError: img_url = '' if img_url: return 'www.5studio.ru%s' % img_url return ''
def get_thumb_url(key, thumb_size = "400x400"): thumb_size_ = "thumb_%s" % thumb_size thumb = ThumbImage.get(key) thumb_url = '' if thumb: try: att = getattr(thumb, thumb_size_) i = Image.get(att) thumb_url = i.get_link() except AttributeError: thumb_url = '' if thumb_url: return 'www.5studio.ru%s' % thumb_url return ''
def get_mini_main_thumb_url(gift): thumb_url = '' if gift.main_thumb: thumb = ThumbImage.get(gift.main_thumb) if thumb: try: att = getattr(thumb, "thumb_100x100") i = Image.get(att) thumb_url = i.get_link() except AttributeError: pass if thumb_url: return 'www.5studio.ru%s' % thumb_url return ''
def get_main_img_url(gift): img_url = '' if gift.main_thumb: img = ThumbImage.get(gift.main_thumb) if img: try: att = getattr(img, "thumb_700x700") i = Image.get(att) img_url = i.get_link() except AttributeError: pass if img_url: return 'www.5studio.ru%s' % img_url return ''
def send_newsletter_task(request, user_key, newsletter_key): newsletter = Newsletter.get(newsletter_key) managers = [] message = mail.EmailMessage(sender=settings.DEFAULT_MAIL_FROM, subject=newsletter.title) user = UserProfile.get(user_key) message.to = user.email if not message.to: return Response("Ok") gifts = [] gifts_images = {} if newsletter.gifts: gifts_split = newsletter.gifts.split(",") for gift in gifts_split: try: gift = gift.strip() obj = Gift.all().filter("uid =", gift).get() if obj: gifts.append(obj) if obj.main_thumb: try: img = ThumbImage.get(obj.main_thumb) img = Image.get(img.thumb_200x200) if img: gifts_images[obj.uid] = base64.b64encode(str(img.image)) except Exception: pass except Exception: pass message_text = render_to_string( "postman/send_newsletter.html", {"newsletter": newsletter, "gifts": gifts, "managers": managers, "gifts_images": gifts_images}, ) message.html = message_text attachments = [] for file_key in newsletter.n_price_list: file_ = File.get(file_key) if file_ and file_.blob_key: bi = blobstore.BlobInfo.get(file_.blob_key) if bi: attachments.append(("%s" % file_.title_filename, bi.open().read())) message.attachments = attachments message.send() return Response("OK")
def thumb_image(environment, value, size="200x200", prefix=''): if value is None: return '' thumb = memcache.get(value) if thumb is None: thumb = ThumbImage.get(value) if thumb: memcache.add(value, thumb, 7200) if thumb: try: att_name = value + "thumb_%s" % size att = memcache.get(att_name) if att is None: att = getattr(thumb, "thumb_%s" % size) memcache.add(att_name, att, 7200) except AttributeError: return '' return img_tag(environment, att, prefix) return ''
def url_thumb(environment, value, size="700x700"): if value is None: return '' thumb = memcache.get(value) if thumb is None: thumb = ThumbImage.get(value) if thumb: memcache.add(value, thumb, 7200) if thumb: try: att_name = value + "thumb_%s" % size att = memcache.get(att_name) if att is None: att = getattr(thumb, "thumb_%s" % size) memcache.add(att_name, att, 7200) except AttributeError: return '' return link(environment, att) return ''
def change_main_thumb(request): gift_key = request.values.get('gift_key') thumb_key = request.values.get('thumb_key') gift = Gift.get(gift_key) if gift is None: return redirect('/') thumb = ThumbImage.get(thumb_key) if thumb is None: gift.thumbs.remove(thumb_key) gift.put() get_gift(gift.uid, True) return redirect('/g/%s' % gift.uid) if gift.main_thumb == thumb_key: return redirect('/g/%s' % gift.uid) else: gift.main_thumb = thumb_key gift.put() get_gift(gift.uid, True) return redirect('/g/%s' % gift.uid)
def delete_thumb(request): gift_key = request.values.get('gift_key') thumb_key = request.values.get('thumb_key') gift = Gift.get(gift_key) if gift is None: return redirect('/') thumb = ThumbImage.get(thumb_key) if thumb is None: gift.thumbs.remove(thumb_key) gift.put() get_gift(gift.uid, True) return redirect('/g/%s' % gift.uid) if gift.main_thumb == thumb_key: gift.main_thumb = None gift.thumbs.remove(thumb_key) thumb.delete() if not len(gift.thumbs): gift.main_thumb = '' gift.put() get_gift(gift.uid, True) return redirect('/g/%s' % gift.uid)
def write_category(wb, category, all=False): if category.gifts: ws = wb.add_sheet(category.name) if all: gifts = category.gifts else: gifts = category.gifts.filter('in_trash =', False).filter('leftovers >', 0) write_head(ws) for i, g in enumerate(gifts): url = 'http://www.5studio.ru/g/%s/' % g.uid ws.write(i + 1, 0, xlwt.Formula('HYPERLINK("%s")' % url)) if g.main_thumb: img = ThumbImage.get(g.main_thumb) if img and getattr(img, 'thumb_100x100'): img = "http://www.5studio.ru%s" % Image.get(img.thumb_100x100).get_link() ws.write(i + 1, 1, xlwt.Formula('HYPERLINK("%s")' % img)) ws.write(i + 1, 2, g.catalogue_id) ws.write(i + 1, 3, g.barcode) ws.write(i + 1, 4, g.name) ws.write(i + 1, 5, g.category.name) ws.write(i + 1, 6, g.group.name) ws.write(i + 1, 7, g.brand) ws.write(i + 1, 8, g.price) ws.write(i + 1, 9, g.real_price)
def change_main_image(request): locstore_key = request.values.get('locstore_key', None) image_key = request.values.get('image_key', None) locstore = None if locstore_key: locstore = memcache.get('get_locstore_%s' % locstore_key) if not locstore: locstore = LocStore.get(locstore_key) if locstore is None: return redirect('/admin/locstore/list/') thumb = ThumbImage.get(image_key) if thumb is None: locstore.images.remove(image_key) locstore.put() memcache.set('get_locstore_%s' % locstore.key(), locstore, MEMCACHE_TIMEOUT) return redirect('/admin/locstore/edit/%s/' % locstore.key()) if locstore.main_image == image_key: return redirect('/admin/locstore/edit/%s/' % locstore.key()) else: locstore.main_image = image_key locstore.put() memcache.set('get_locstore_%s' % locstore.key(), locstore, MEMCACHE_TIMEOUT) return redirect('/admin/locstore/edit/%s/' % locstore.key())
def add_manager_photo(request): if request.method == 'POST': manager_key = request.values.get('manager_key') manager = Manager.get(manager_key) if manager is None: return redirect('/admin/managers') new_th_form = ManagerPhoto() if request.form and new_th_form.validate(request.form, request.files): if manager.photo: thumb = ThumbImage.get(manager.photo) thumb.delete() manager.photo = None thumb = new_th_form['img'] content_type = 'image/jpeg' thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(200, 200,), content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(100, 100,), content_type=content_type) thumb_img.put() manager.photo = str(thumb_img.key()) manager.put() return redirect('/admin/edit_manager/%s' % manager.key()) return redirect('/admin/managers')
def add_image_task(request, task_id): data = memcache.get(task_id) memcache.delete(task_id) if not data or not data.get('gift_id') or not data.get('img_url'): return render_to_response('empty.html') img = urlfetch.fetch(data['img_url']) if img.status_code == 200: thumb = img.content gift = Gift.get_by_id(data['gift_id']) if gift: title = gift.name.replace('"', '"') content_type = 'image/jpeg' thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(700, 700, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(400, 400, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(200, 200, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(100, 100, ), title=title, content_type=content_type) thumb_img.put() gift.thumbs.append(str(thumb_img.key())) if not gift.main_thumb: gift.main_thumb = str(thumb_img.key()) gift.put() return render_to_response('empty.html')
def send_newsletter_task2(request, email, newsletter_key_id): newsletter = Newsletter2.get_by_id(newsletter_key_id) managers = [] for mgr_id in newsletter.managers: mgr = Manager.get_by_id(mgr_id) if mgr: managers.append(mgr) message = mail.EmailMessage(sender=settings.DEFAULT_MAIL_FROM, subject=newsletter.title) message.to = email if not message.to: return Response("Ok") mangers_emails = [manager.email for manager in managers] if mangers_emails: message.reply_to = ",".join(mangers_emails) gifts = [] gifts_images = {} if newsletter.gifts: gifts_split = newsletter.gifts.split(",") for gift in gifts_split: try: gift = gift.strip() obj = Gift.all().filter("uid =", gift).get() if obj: gifts.append(obj) if obj.main_thumb: try: img = ThumbImage.get(obj.main_thumb) img = Image.get(img.thumb_200x200) if img: gifts_images[obj.uid] = base64.b64encode(str(img.image)) except Exception: pass except Exception: pass message_text = render_to_string( "postman/send_newsletter2.html", {"newsletter": newsletter, "gifts": gifts, "managers": managers, "gifts_images": gifts_images}, ) message.html = message_text attachments = [] for file_key in newsletter.n_price_list: file_ = File.get(file_key) if file_ and file_.blob_key: bi = blobstore.BlobInfo.get(file_.blob_key) if bi: attachments.append(("%s" % file_.title_filename, bi.open().read())) if attachments: message.attachments = attachments try: message.send() except apiproxy_errors.OverQuotaError: def txn(): taskqueue.add( url=url_for("postman/send_newsletter_task2", email=email, newsletter_key_id=newsletter_key_id), transactional=True, ) db.run_in_transaction(txn) return Response("OK")
def upload_new_thumb(request): if request.method == 'POST': gift_key = request.values.get('gift_key') gift = Gift.get(gift_key) if gift is None: return redirect('/') new_th_form = AddNewThumb() if request.form and new_th_form.validate(request.form, request.files): thumb = new_th_form['img'] content_type = 'image/jpeg' if gift.name: title = gift.name.replace('"', '"') else: title = '' thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(700, 700, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(400, 400, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(200, 200, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(100, 100, ), title=title, content_type=content_type) thumb_img.put() gift.thumbs.append(str(thumb_img.key())) gift.put() get_gift(gift.uid, True) return redirect('/g/%s' % gift.uid) return redirect('/')
thumb_img.put() obj.thumbs.append(str(thumb_img.key())) obj.main_thumb = str(thumb_img.key()) except Exception, ex: print obj.catalogue_id print ex for f in file_names: try: content_type = content_type = 'image/jpeg' full_file_name = os.path.join(cat_path, f) img_700 = pilImage.open(full_file_name) buf = StringIO.StringIO() img_700.save(buf, format=CONVERT_RULES_PIL[content_type][2]) img_700 = buf.getvalue() thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=img_700, thumb_size=(700, 700, ), is_using_pil=True, title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=img_700, thumb_size=(400, 400, ), is_using_pil=True, title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=img_700, thumb_size=(200, 200, ), is_using_pil=True, title=title, content_type=content_type)
def delete(self, **kwargs): if self.photo: thumb = ThumbImage.get(self.photo) if thumb: thumb.delete() super(Manager, self).delete(**kwargs)
def upload_new_image(request): if request.method == 'POST': locstore_key = request.values.get('locstore_key', None) locstore = None if locstore_key: locstore = LocStore.get(locstore_key) if locstore is None: return redirect('admin/locstore/list/') new_th_form = AddNewThumb() if request.form and new_th_form.validate(request.form, request.files): thumb = new_th_form['img'] content_type = 'image/jpeg' if locstore.name: title = locstore.name.replace('"', '"') else: title = '' thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(700, 700, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(400, 400, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(200, 200, ), title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=thumb, thumb_size=(100, 100, ), title=title, content_type=content_type) thumb_img.put() locstore.images.append(str(thumb_img.key())) if len(locstore.images) == 1: locstore.main_image = str(thumb_img.key()) locstore.put() memcache.delete('get_locstore_%s' % locstore.key()) return redirect('admin/locstore/edit/%s/' % locstore.key()) return redirect('/')
def fill_obj(obj, fred_path): if not obj or not obj.catalogue_id: return False title = obj.name.replace('"', '"') cat_path = os.path.join(fred_path, obj.catalogue_id) if not os.path.exists(cat_path): return False file_names = os.listdir(cat_path) if file_names: for i in obj.thumbs: thumb = ThumbImage.get(i) if thumb: thumb.delete() obj.thumbs = [] else: return file_names.sort() first = None for fn in file_names: if fn[0] == '_': first = fn file_names.remove(fn) break if first is not None: try: full_file_name = os.path.join(cat_path, first) img_700 = pilImage.open(full_file_name) content_type = 'image/jpeg' buf = StringIO.StringIO() img_700.save(buf, format=CONVERT_RULES_PIL[content_type][2]) img_700 = buf.getvalue() thumb_img = ThumbImage() thumb_img.add_new_thumb(blob_img=img_700, thumb_size=(700, 700, ), is_using_pil=True, title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=img_700, thumb_size=(400, 400, ), is_using_pil=True, title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=img_700, thumb_size=(200, 200, ), is_using_pil=True, title=title, content_type=content_type) thumb_img.add_new_thumb(blob_img=img_700, thumb_size=(100, 100, ), is_using_pil=True, title=title, content_type=content_type) thumb_img.put() obj.thumbs.append(str(thumb_img.key())) obj.main_thumb = str(thumb_img.key()) except Exception, ex: print obj.catalogue_id print ex
def delete(self, **kwargs): for i in self.thumbs: thumb = ThumbImage.get(i) if thumb: thumb.delete() super(Gift, self).delete(**kwargs)