def new_postcard_form(): form = PostcardForm(request.form) if request.method == 'POST' and form.validate(): postcard = Postcard() postcard.user = form.username.data postcard.date = form.date.data postcard.country = form.origin_country.data postcard.latitude = form.origin_latitude.data postcard.longitude = form.origin_longitude.data postcard.front = form.front.data postcard.back = form.back.data postcard.deleted = False db.session.add(postcard) for tag in (x.strip() for x in form.tags.data.split(',')): t = Tag() t.tag = tag postcard.tags.append(t) db.session.commit() generate_thumbnails(postcard.id) flash('postcard added!') return redirect('/postcard/new') return render_template('postcard_new.html', form=form)
def generate_thumbnails(postcard_id, width=70, height=70): dimensions = dict(small=(215, 215), full=(800, 800)) postcard = Postcard._byID(postcard_id) if postcard.front: postcard.front_thumb, _ = make_smaller_version_of_image(postcard.front) if postcard.back: postcard.back_thumb, _ = make_smaller_version_of_image(postcard.back) image_info = {} for size in ('small', 'full'): image_info[size] = {} for side in ('front', 'back'): full_image_url = getattr(postcard, side) if not full_image_url: continue img_data = make_smaller_version_of_image(full_image_url, dimensions[size]) filename, (width, height) = img_data image_info[size][side] = dict(filename=filename, width=width, height=height) postcard.json_image_info = json.dumps(image_info) postcard._commit()
def submit_link_to_postcard(postcard_id): postcard = Postcard._byID(postcard_id) if not getattr(postcard, "json_image_info", None): return country = pycountry.countries.get(alpha2=postcard.country) title_components = ["[Postcard] sent in from", country.name] if postcard.date != DEFAULT_DATE: title_components += ["on", postcard.date.strftime('%d-%b-%Y')] if "nsfw" in postcard.text_tags: title_components += ["(NSFW)"] title = " ".join(title_components) postcard_url = 'http://www.reddit.com/about/postcards/view/%d' % postcard.id thumbnail = postcard.front_thumb or postcard.back_thumb assert thumbnail args = [app.config['REDDIT_USER'], app.config['REDDIT_SUBREDDIT'], title, postcard_url, s3_url_from_filename(thumbnail)] postcard.submission = run_reddit_script('submit_link', args) postcard._commit()
def delete(): id = int(request.form['postcard-id']) postcard = Postcard._byID(id) if postcard.deleted or postcard.published: abort(403) postcard.deleted = True db.session.commit() flash('postcard deleted!') return redirect('/')
def remove_all_images(postcard_id): postcard = Postcard._byID(postcard_id) s3 = S3Connection(app.config['S3_ACCESS_KEY'], app.config['S3_SECRET_KEY']) bucket = s3.get_bucket(app.config['S3_BUCKET'], validate=False) for imagetype in ("front", "back", "front_thumb", "back_thumb"): filename = getattr(postcard, imagetype, None) if not filename: continue bucket.delete_key(filename)
def generate_thumbnails(postcard_id, width=70, height=70): postcard = Postcard._byID(postcard_id) if postcard.front: postcard.front_thumb, _ = make_smaller_version_of_image(postcard.front) if postcard.back: postcard.back_thumb, _ = make_smaller_version_of_image(postcard.back) postcard._commit()
def publish(): id = int(request.form['postcard-id']) postcard = Postcard._byID(id) if postcard.deleted or postcard.published: abort(403) postcard.published = True db.session.commit() generate_jsonp() submit_link_to_postcard(postcard.id) send_gold_claim_message(postcard.id) enflair_user(postcard.user) flash('postcard published!') return redirect('/')
def delete(id): postcard = Postcard._byID(id) if postcard.deleted: abort(403) postcard.deleted = True db.session.commit() # clean up if necessary if postcard.published: remove_all_images(id) generate_jsonp() if request.headers.get('X-Requested-With') == 'XMLHttpRequest': return 'success!' else: flash('postcard deleted!') return redirect('/')
def publish(id): postcard = Postcard._byID(id) if postcard.deleted or postcard.published: abort(403) postcard.published = True db.session.commit() generate_jsonp() submit_link_to_postcard(postcard.id) send_gold_claim_message(postcard.id) enflair_user(postcard.user) if request.headers['X-Requested-With'] == 'XMLHttpRequest': return 'success!' else: flash('postcard published!') return redirect('/')
def view(id): postcard = Postcard._byID(id) return render_template("postcard.html", postcard=postcard)
def send_gold_claim_message(postcard_id): postcard = Postcard._byID(postcard_id) assert postcard.submission run_reddit_script('send_claim_message', [postcard.user, postcard.submission])