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

User signin #16

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
11 changes: 10 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import config
from flask import Flask
from flask import Flask, flash, redirect, url_for
from models.base_model import db
from flask_login import LoginManager, current_user, logout_user, login_required
from models.user import User

web_dir = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'instagram_web')
Expand All @@ -13,6 +15,13 @@
else:
app.config.from_object("config.DevelopmentConfig")

login_manager = LoginManager()
login_manager.init_app(app)

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


@app.before_request
def before_request():
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ class DevelopmentConfig(Config):
class TestingConfig(Config):
TESTING = True
DEBUG = True
ASSETS_DEBUG = True
ASSETS_DEBUG = True
44 changes: 37 additions & 7 deletions instagram_web/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
from app import app
from flask import render_template
import os
from flask import render_template, redirect, flash, url_for
from instagram_web.blueprints.sessions.views import sessions_blueprint
from instagram_web.blueprints.users.views import users_blueprint
from instagram_web.blueprints.comments.views import comments_blueprint
from instagram_web.blueprints.images.views import images_blueprint
from instagram_web.blueprints.payments.views import payments_blueprint
from instagram_web.blueprints.fan_idol.views import fan_idol_blueprint
from flask_assets import Environment, Bundle
from .util.assets import bundles
from flask_wtf.csrf import CSRFProtect
from flask_login import current_user
from instagram_web.util.google_oauth import oauth

csrf = CSRFProtect()
csrf.init_app(app)
oauth.init_app(app)

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(images_blueprint, url_prefix="/images")
app.register_blueprint(payments_blueprint, url_prefix="/payments")
app.register_blueprint(comments_blueprint, url_prefix="/comments")
app.register_blueprint(fan_idol_blueprint, url_prefix="/fan_idol")

@app.errorhandler(500)
def internal_server_error(e):
return render_template('500.html'), 500
# app.config['TRAP_HTTP_EXCEPTIONS'] = True

# @app.errorhandler(Exception)
# def handle_error(e):
# if e.code == 404:
# return render_template('404.html'), 404
# elif e.code == 500:
# return render_template('505.html'), 500
# else:
# return str(e.code) + ": Something went wrong"

@app.route("/")
def home():
return render_template('home.html')
@app.route('/')
def index():
if current_user.is_authenticated:
return redirect(url_for('images.index'))
else:
flash("You are not logged in", "info")
return redirect(url_for('sessions.new'))
36 changes: 36 additions & 0 deletions instagram_web/blueprints/comments/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import peewee
from flask import Blueprint, render_template, request, redirect, url_for, session, escape, flash
from flask_login import current_user
from models.comment import Comment
from instagram_web.util.helpers import allowed_file, upload_file_to_s3


comments_blueprint = Blueprint('comments',
__name__,
template_folder='templates')

@comments_blueprint.route('/', methods=['POST'])
def create():
text = request.form.get('text')
image = request.form.get('image')

comment= Comment(text=text, user_id=current_user.id, image_id=image)

if comment.save():
flash('Thank you for your comment', 'success')
else:
flash('Sorry, something went wrong. Try again.', 'danger')

return redirect(url_for('images.show', id=image))


@comments_blueprint.route('/<id>', methods=['POST'])
def delete(id):
image = request.form.get('image')
comment = Comment.get_by_id(id)
comment.delete_instance()

flash('Your comment has been removed', 'info')
return redirect(url_for('images.show', id=image))


45 changes: 45 additions & 0 deletions instagram_web/blueprints/fan_idol/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import peewee
from flask import Blueprint, render_template, request, redirect, url_for, session, escape, flash
from flask_login import current_user
from models.fan_idol import FanIdol
from models.user import User
from models.image import Image
from models.donation import Donation
from models.comment import Comment
from instagram_web.util.helpers import allowed_file, upload_file_to_s3


fan_idol_blueprint = Blueprint('fan_idol',
__name__,
template_folder='templates')

@fan_idol_blueprint.route('/follow/<id>', methods=['POST'])
def follow(id):
user = User.get_by_id(id)
if user.private:
new = FanIdol(idol_id=id, fan_id=current_user.id)
new.save()
flash("Waiting for user approval", "info")
else:
new = FanIdol(idol_id=id, fan_id=current_user.id, approved='true')
new.save()
flash("Thank you for following", "success")

return redirect(url_for('users.show', username=user.username))


@fan_idol_blueprint.route('/unfollow/<id>', methods=['POST'])
def unfollow(id):
user = User.get_by_id(id)
unfollow_user = FanIdol.delete().where((FanIdol.fan_id == current_user.id) & (FanIdol.idol_id == user.id))
unfollow_user.execute()


flash(f"You are no longer following {user.username}", "danger")
return redirect(url_for('users.show', username=user.username))

@fan_idol_blueprint.route('/approve/<id>', methods=['POST'])
def approve(id):
update = FanIdol.update(approved='true').where((FanIdol.fan_id==id) & (FanIdol.idol_id == current_user.id))
update.execute()
return redirect(url_for('users.show', username=current_user.username))
26 changes: 26 additions & 0 deletions instagram_web/blueprints/images/templates/images/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "_layout.html" %}
{% block content %}
<div class='container-fluid mt-3'>
<div class="card-columns mx-auto">
{% for image in images %}
<div class="card" style="overflow:hidden;border-color:#004f31;">
{% for user in users %}
{% if user.id == image.user_id %}
<a class="head-link" href="{{ url_for('users.show', username = user.username ) }}">
<div class="card-header p-0" style="background-color: #004f31;">
<img style="height:40px;background-color:white;" src="{{user.profile_picture_url}}"/>
{{user.username}}
</div>
</a>
{% endif %}
{% endfor %}
<a href="{{ url_for('images.show', id=image.id)}}">
<img src="{{image.image_url}}" class="card-img-bottom"></a>
</div>
{% endfor %}
</div>
</div>



{% endblock %}
17 changes: 17 additions & 0 deletions instagram_web/blueprints/images/templates/images/new.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "_layout.html" %}
{% block content %}

<div class="container col-lg-8 col-xl-6 text-center mt-3">
<h2>Upload photo for {{user.username}}</h2>
<form action="{{ url_for('images.create')}}" method="POST" enctype="multipart/form-data">

<input type="file" name="user_file" id="user_file" class="inputfile" />
<label for="user_file">Choose a file</label>

<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<button class="btn btn-outline-secondary w-25 mx-auto" type="submit">Upload</button>
<a class="btn btn-outline-secondary w-25 mx-auto" href="{{url_for('users.show', username=user.username)}}">Cancel</a>

</form>
</div>
{% endblock %}
43 changes: 43 additions & 0 deletions instagram_web/blueprints/images/templates/images/show.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% extends "_layout.html" %}
{% block content %}
<div class='container pb-5'>
<img class="col-12 col-md-6 d-block mx-auto my-3" src='{{ image.image_url }}' />
{{donations}}
<a class="btn btn-outline-secondary w-25 d-block mx-auto" href="{{ url_for('payments.new', id=image.id) }}">Donate</a>

{% if current_user.is_authenticated%}
<form class="my-3" action="{{ url_for('comments.create')}}" method="POST">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<input type="hidden" name="image" value={{image.id}} />
<div class="input-group col-12 col-md-8 mx-auto">
<textarea style="resize: none;" class="form-control" name="text" maxlength="200" placeholder="Enter a new comment here."></textarea>
<div class="input-group-append">
<button class="btn btn-outline-secondary"
id="button-addon2" type="submit">Send</button>
</div>
</div>
</form>
{% endif %}


{%for comment in image.comments %}
<form action="{{ url_for('comments.delete', id=comment.id) }}" method="POST" class="row pb-1 w-100 d-block">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
{% for user in users %}
{% if comment.user_id == user.id%}
<input type="hidden" name="image" value="{{ image.id }}">
<strong class="pr-2">{{user.username}}</strong>
{{comment.text}}
{% if user.username == current_user.username %}
<button class="btn btn-link text-danger float-right p-0">delete</button>
{% endif %}
{% endif %}
{% endfor%}
</form>
{% endfor %}

</div>



{% endblock %}
70 changes: 70 additions & 0 deletions instagram_web/blueprints/images/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import peewee
from flask import Blueprint, render_template, request, redirect, url_for, session, escape, flash
from flask_login import current_user
from models.user import User
from models.image import Image
from models.donation import Donation
from models.fan_idol import FanIdol
from instagram_web.util.helpers import allowed_file, upload_file_to_s3


images_blueprint = Blueprint('images',
__name__,
template_folder='templates')


@images_blueprint.route('/new', methods=['GET'])
def new():
return render_template('images/new.html', user=current_user)


@images_blueprint.route('/', methods=['POST'])
def create():
if 'user_file' not in request.files:
return "No user_file key in request.files"

file = request.files["user_file"]

if file.filename == "":
return "Please select a file"

if file and allowed_file(file.filename):
image = Image(path=file.filename, user_id=current_user.id)
image.save()
upload_file_to_s3(file)
return redirect(url_for('users.show', username = current_user.username))

else:
redirect(url_for('image.new', id=id))


@images_blueprint.route('/<id>', methods=["GET"])
def show(id):
image = Image.get_by_id(id)
users = User.select()
return render_template('images/show.html', image=image, users=users)


@images_blueprint.route('/', methods=["GET"])
def index():

images = Image.select().join(FanIdol, on=(Image.user_id == FanIdol.idol_id)).where(
(Image.user_id == current_user.id) |
((FanIdol.fan_id == current_user.id) & (FanIdol.approved == True))
).group_by(Image.id).order_by(Image.created_at.desc())

users = User.select()
return render_template('images/index.html', images=images, users=users)


@images_blueprint.route('/<id>/edit', methods=['GET'])
def edit(id):
pass


@images_blueprint.route('/<id>', methods=['POST'])
def update(id):
pass



46 changes: 46 additions & 0 deletions instagram_web/blueprints/payments/templates/payments/new.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{% extends "_layout.html" %}
{% block header %}
<meta charset="utf-8">
<script src="https://js.braintreegateway.com/web/dropin/1.19.0/js/dropin.min.js"></script>
{% endblock %}
{% block content %}
<div class='container text-center mt-3'>
<form id="payment" action="{{ url_for('payments.payment') }}" method="post">

<div class="col-md-6 mx-auto">
<label for="amount">Donation amount</label>
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text">$</div>
</div>
<input type="text" class="form-control" name="amount" id="amount" value="100">
</div>
</div>

<input id="paymentinput" type="hidden" name="payment_method_nonce">
<input id="image" type="hidden" name="image" value={{image.id}}>
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
</form>
<div id="dropin-container"></div>
<button class="btn btn-outline-success w-25 mb-5" id="submit-button">Donate</button>
<a class="btn btn-outline-danger w-25 mb-5" href="{{ url_for('images.show', id=image.id) }}">Cancel</a>
</div>
<script>
var button = document.querySelector('#submit-button');
const form = document.getElementById('payment')
const paymentNonce = document.getElementById('paymentinput')

braintree.dropin.create({
authorization: '{{token}}',
container: '#dropin-container'
}, function (createErr, instance) {
button.addEventListener('click', function () {
instance.requestPaymentMethod(function (err, payload) {
paymentNonce.value = payload.nonce;
form.submit();
});
});
});
</script>
{% endblock %}
</body>
Loading