Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/follow #23

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.env
instagram_web/static/.webassets-cache
instagram_web/static/gen
node_modules
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: python migrate.py; gunicorn start: app --preload
4 changes: 4 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from flask_wtf.csrf import CSRFProtect
import os
import config
from flask import Flask
from models.base_model import db


web_dir = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'instagram_web')

csrf = CSRFProtect()
app = Flask('NEXTAGRAM', root_path=web_dir)
csrf.init_app(app)

if os.getenv('FLASK_ENV') == 'production':
app.config.from_object("config.ProductionConfig")
Expand Down
5 changes: 5 additions & 0 deletions code snippet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{ % if current_user.is_following(i) % }
{% else%}
<div > </div >

{% endif%}
6 changes: 6 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class Config(object):
CSRF_ENABLED = True
SECRET_KEY = os.environ.get(
'SECRET_KEY') or os.urandom(32)
AWS_ACCESS_KEY = os.getenv('AWS_ACCESS_KEY')
AWS_SECRET_KEY = os.getenv('AWS_SECRET_KEY')
AWS_BUCKET_NAME = os.getenv('AWS_BUCKET_NAME')


class ProductionConfig(Config):
Expand All @@ -21,10 +24,13 @@ class StagingConfig(Config):


class DevelopmentConfig(Config):
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_SECRET_ID")
DEVELOPMENT = True
DEBUG = True
ASSETS_DEBUG = False


class TestingConfig(Config):
TESTING = True
DEBUG = True
Expand Down
137 changes: 137 additions & 0 deletions d
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
commit f1b8f389f88a6dbc16decbb020789f6f2f3f7da9 (HEAD -> master)
Author: Zeft <zeft.huisen@gmail.com>
Date: Fri Feb 28 10:45:33 2020 +0800

flask-login without logout

commit add8edaa62ea71e87dbca470c4c5d4c8f43397af (origin/master, origin/HEAD)
Author: Liren <liren90@gmail.com>
Date: Thu Sep 26 16:45:36 2019 +0800

Non blocking footer

commit fbf16f6e84873f6408c404c1b913fc43cb0b2b85
Author: Joshua Teng <joshteng@me.com>
Date: Fri Feb 22 22:10:52 2019 +0800

set asset debug to false to prevent browser caching during development

commit 67189e67a3b9bbae73381ae46916d642e9103004
Author: Joshua Teng <joshteng@me.com>
Date: Fri Feb 22 22:09:09 2019 +0800

fix template namespace

commit a16f66a32af4b0254b087e877483c3feca2288b0
Author: Joshua Teng <joshteng@me.com>
Date: Thu Feb 21 03:25:07 2019 +0800

change to use teardown request

commit 55f705d55335e1166b34aec77f6e7e5cecc06f9a
Author: Joshua Teng <joshteng@me.com>
Date: Sun Feb 10 06:43:01 2019 +0800

update readme on first set up

commit fa3d5e87b812e75b826a6370db5109541497066d
Author: Joshua Teng <joshteng@me.com>
Date: Sun Feb 10 04:19:03 2019 +0800

make readme clearer

commit 68a14c93849a57ee92d9a86de9d7df1d931d579b
Author: Joshua Teng <joshteng@me.com>
Date: Sun Feb 10 04:18:28 2019 +0800

update readme on trouble starting flask

commit 4fd50e87f3657929122078809be2df0007901506
Author: Joshua Teng <joshteng@me.com>
Date: Sun Feb 10 04:03:25 2019 +0800

update dependency notes on bootstrap

commit 572f74b9337bb3ca72c93b1d501abd804e9ea39d
Author: Joshua Teng <joshteng@me.com>
Date: Sun Feb 10 03:53:53 2019 +0800

add python version for heroku

commit cf2efdae2f116f475042bd17dd4c4846498a670c
Author: Joshua Teng <joshteng@me.com>
Date: Sun Feb 10 03:42:41 2019 +0800

add bootstrap

commit 4a94d97185acd9be43b9be9429d8a2a32630f825
Author: Joshua Teng <joshteng@me.com>
Date: Sun Feb 10 03:41:25 2019 +0800

use dynamic database connfig

commit e12dcb1516a717ae38c10c1d5c78a378780c7dc8
Author: Joshua Teng <joshteng@me.com>
Date: Sun Feb 10 03:41:04 2019 +0800

set up static asset management

commit 7df68c886754795e80a4ef2e0cc1dac044841f50
Author: Joshua Teng <joshteng@me.com>
Date: Sat Feb 9 21:01:01 2019 +0800

manually parse database url - database.py

commit 1d64369caaa39c1e7f4b76122491dd3fe5c93814
Author: Joshua Teng <joshteng@me.com>
Date: Sat Feb 9 20:45:34 2019 +0800

simplified config loading between environments

commit e36c774e6be41bde85a9c3504a150d85c1106400
Author: Joshua Teng <joshteng@me.com>
Date: Sat Feb 9 20:42:08 2019 +0800

fix grammatical error

commit 6226825a94713712bf9fb531bb4746fe616e897b
Author: Joshua Teng <joshteng@me.com>
Date: Sat Feb 9 20:41:28 2019 +0800

improved readme clarity

commit 285f91a6beb62cb465693f4c7556b0fcbeff502b
Author: Joshua Teng <joshteng@me.com>
Date: Sat Feb 9 20:40:27 2019 +0800

improved readme

commit f165b33e138ee07691c2421f6f4ffded3373412d
Author: Joshua Teng <joshteng@me.com>
Date: Sat Feb 9 20:35:39 2019 +0800

update readme

commit 6a18218cb89b714247d52f17c7cb86f13b486cfc
Author: Joshua Teng <joshteng@me.com>
Date: Sat Feb 9 20:31:41 2019 +0800

add readme to generate random secret_key

commit 5708637fc6984f44c7ab161686cb6baed6b4dfec
Author: Joshua Teng <joshteng@me.com>
Date: Sat Feb 9 20:29:36 2019 +0800

generate a random secret_key in case anyone deploys this to production

commit 7b285b6cacb0c035ffbaddec879f37428d5a2d10
Author: Joshua Teng <joshteng@me.com>
Date: Sat Feb 9 02:11:25 2019 +0800

update readme

commit 238835d02abb2b68f284d1f7dfea846b0a7aec5d
Author: Joshua Teng <joshteng@me.com>
Date: Sat Feb 9 02:06:32 2019 +0800

initial commit
41 changes: 40 additions & 1 deletion instagram_web/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
from app import app
from flask import render_template
from flask import render_template, redirect, url_for
from instagram_web.blueprints.users.views import users_blueprint
from instagram_web.blueprints.sessions.views import sessions_blueprint
from instagram_web.blueprints.imgs.views import imgs_blueprint
from instagram_web.blueprints.donations.views import donations_blueprint
from instagram_web.blueprints.follows.views import follows_blueprint
from flask_assets import Environment, Bundle
from .util.assets import bundles
from flask_login import LoginManager, current_user
from models.user import User
from instagram_web.util.google_oauth import oauth

assets = Environment(app)
assets.register(bundles)

app.register_blueprint(users_blueprint, url_prefix="/users")
app.register_blueprint(sessions_blueprint, url_prefix="/sessions")
app.register_blueprint(imgs_blueprint, url_prefix="/imgs")
app.register_blueprint(donations_blueprint, url_prefix="/donations")
app.register_blueprint(follows_blueprint, url_prefix="/follows")
login_manager = LoginManager()
login_manager.init_app(app)

oauth.init_app(app)


@login_manager.user_loader
def load_user(user_id):
return User.get_by_id(user_id)


@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500


@app.errorhandler(404)
def internal_server_error(e):
return render_template('404.html'), 404


@app.errorhandler(401)
def internal_server_error(e):
return render_template('404.html'), 401


@app.errorhandler(400)
def internal_server_error(e):
return render_template('404.html'), 400


@app.route("/")
def home():
if current_user.is_authenticated:
return redirect(url_for('users.user_profile', id=current_user.id))

return render_template('home.html')
56 changes: 56 additions & 0 deletions instagram_web/blueprints/donations/templates/donations/new.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{%extends "_layout.html" %} {%block content%} {%block header%}

<script src="https://js.braintreegateway.com/web/dropin/1.22.1/js/dropin.min.js"></script>

{%endblock%}

<div class="container">
<div class="row justify-content-center">
<div class="col-6">
<img src="{{image.image_url}}" alt="" class="w-75 mx-auto d-block mb-4" />
</div>
</div>
</div>
<div class="mx-auto col-lg-4 mb-4">
<form
id="donation-form"
action="{{url_for('donations.create', photo_id=image.id)}}"
method="post"
>
<input type="hidden" name="payment_method_nonce" id="pmn-field" />
<input type="hidden" name="csrf_token" value="{{csrf_token()}}" />
<div class="form-group">
<label for="amount">Amount:</label>
<input type="text" name="amount" class="form-control" />
</div>
</form>
<div class="my-0" id="dropin-container"></div>
<div class="d-flex justify-content-center mx-auto my-3">
<button class="btn btn-outline-primary" id="submit-button">
Request payment method
</button>
</div>
</div>

<script>
const button = document.querySelector("#submit-button");

braintree.dropin.create(
{
authorization: "{{client_token}}",
container: "#dropin-container"
},
function(createErr, instance) {
button.addEventListener("click", function() {
instance.requestPaymentMethod(function(err, payload) {
// Submit payload.nonce to your server
console.log(payload);
$("#pmn-field").val(payload.nonce);
$("#donation-form").submit();
});
});
}
);
</script>

{%endblock%}
86 changes: 86 additions & 0 deletions instagram_web/blueprints/donations/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import peeweedbevolve
from flask import Blueprint, flash, Flask, render_template, request, flash, redirect, url_for, session
from models.user import User
from models.photos import Photos
from models.donation import Donation
from werkzeug.security import generate_password_hash
from werkzeug.utils import secure_filename
from flask_login import current_user, login_required
from instagram_web.util.s3_uploader import upload_file_to_s3
from instagram_web.util.braintree import gateway
from instagram_web.util.mailgun import send_simple_message


donations_blueprint = Blueprint('donations',
__name__,
template_folder='templates')


@donations_blueprint.route('/<photo_id>/new', methods=['GET'])
def new(photo_id):
image = Photos.get_or_none(Photos.id == photo_id)

if not image:
flash('No image found with id provided', 'warning')
return redirect(request.referrer)

client_token = gateway.client_token.generate()

if not client_token:
flash('Unable to obtain token', 'warning')
return redirect(request.referrer)

return render_template('donations/new.html', image=image, client_token=client_token)


@donations_blueprint.route('/<photo_id>', methods=['POST'])
def create(photo_id):
nonce = request.form.get('payment_method_nonce')

if not nonce:
flash(f"Error with payment method nonce", 'warning')
return redirect(url_for('users.show'))

image = Photos.get_or_none(Photos.id == photo_id)

if not image:
flash(f"Could not find image with provided ID", 'warning')
return redirect(url_for('users.show'))

amount = request.form.get("amount")

if not amount:
flash(f"No donation provided", 'warning')
return redirect(url_for('users.show'))

result = gateway.transaction.sale({
"amount": amount,
"payment_method_nonce": nonce,
"options": {
"submit_for_settlement": True
}



})

if not result.is_success:
flash('Unable to complete transaction', 'warning')

return redirect(request.referrer)

donation = Donation(amount=amount, photo_id=photo_id,
user_id=current_user.id)

if not donation.save():
flash('Dono successful but error recording', 'warning')
return redirect(url_for('users.show'))
# breakpoint()

receiving_name = donation.photo.user.name
name = User.get_or_none(User.id == current_user.id).name
flash(f'{name} successfully donated ${amount} to {receiving_name} ')

send_simple_message(
amount=amount, receiving_name=receiving_name, name=name)
return redirect(url_for('users.show'))
Loading