def preview_newsletter(request, key): newsletter = Newsletter.get(key) gifts = [] 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) except Exception: pass managers = [] users = UserProfile.all().filter("is_send_newsletter =", True).filter("email !=", None) if newsletter.type != 999: users = users.filter("newsletter_type =", newsletter.type) files = [] for file_key in newsletter.n_price_list: file_ = File.get(file_key) if file_: files.append(file_) return render_to_response( "postman/admin/newsletters/preview.html", {"newsletter": newsletter, "gifts": gifts, "key": key, "files": files, "managers": managers, "users": users}, )
def delete_newsletter_file(request, key, file_key): if request.method == "POST": newsletter = Newsletter.get(key) if not newsletter: return redirect("/admin/postman/newsletters/") for i, file_key_ in enumerate(newsletter.n_price_list): if file_key_ == file_key_: file = File.get(file_key) if file: file.delete() del newsletter.n_price_list[i] newsletter.put() break return redirect(url_for("admin/edit_newsletter", key=key))
def edit_newsletter(request, key): newsletter = Newsletter.get(key) form = AddNewsletter(instance=newsletter) if request.method == "POST" and request.form and form.validate(request.form, request.files): newsletter = form.save(commit=False) newsletter.put() return redirect("/admin/postman/newsletters/") files = [] for file_key in newsletter.n_price_list: file_ = File.get(file_key) if file_: files.append(file_) return render_to_response( "postman/admin/newsletters/edit.html", {"form": form.as_widget(), "key": key, "files": files} )
def send_newsletter(request, key): letter = Newsletter.get(key) if letter.type == -1: return redirect("/admin/postman/newsletters/") users = UserProfile.all().filter("is_send_newsletter =", True) if letter.type != 999: users = users.filter("newsletter_type =", letter.type) for user in users: def txn(): taskqueue.add( url=url_for("postman/send_newsletter_task", user_key=str(user.key()), newsletter_key=key), transactional=True, ) db.run_in_transaction(txn) return redirect("/admin/postman/newsletters/")
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 add_newsletter_file(request, key): newsletter = Newsletter.get(key) url = blobstore.create_upload_url(url_for("admin/add_newsletter_file", key=key)) form = NewsletterFileForm(action=url) form.csrf_protected = False if request.method == "POST" and form.validate(request.form): upload_files = get_uploads(request, "file") if not len(upload_files): return redirect(url_for("admin/add_newsletter_file", key=key)) blob_info = upload_files[0] bl = blobstore.BlobInfo.get(blob_info.key()) filename = os.path.basename(bl.filename.replace("\\", "/")) file_ = File( filename=filename, content_type=blob_info.content_type, blob_key=str(blob_info.key()), length=blob_info.size ) file_.put() newsletter.n_price_list.append(str(file_.key())) newsletter.put() return redirect(url_for("admin/edit_newsletter", key=key)) return render_to_response( "postman/admin/newsletters/add_file.html", {"form": form.as_widget(), "newsletter": newsletter, "key": key} )
def delete_newsletter(request, key): newsletter = Newsletter.get(key) newsletter.delete() return redirect("/admin/postman/newsletters/")
def newsletters(request): newsletters_list = Newsletter.all().order("-modif_time") return render_to_response("postman/admin/newsletters.html", {"newsletters": newsletters_list})