Skip to content

Commit

Permalink
clean up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Lichtenberger committed Jun 10, 2024
1 parent 25a751a commit 981ff1a
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 208 deletions.
Binary file modified __pycache__/app.cpython-39.pyc
Binary file not shown.
Binary file modified __pycache__/models.cpython-39.pyc
Binary file not shown.
40 changes: 29 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
CURR_USER_KEY = "curr_user"

app = Flask(__name__)
app.app_context().push()

app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'postgresql://localhost:5432/cocktailsdb')
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', "it's a secret")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = False

# app.jinja_env = Environment(autoescape=select_autoescape(['html', 'xhtml']), undefined='strict')


connect_db(app)

Expand All @@ -31,14 +30,12 @@ def inject_data():

def inject_funcs():

check_favorites = add_favorites_to_g()
return dict(check_favorites=check_favorites, check_author=check_author, len=len, math=math)

def check_favorites():
check_favorites = add_favorites_to_g()
pass
app.jinja_env.globals.update(check_favorites=check_favorites)

def check_author(post):

if g.user == post.user:
return True
else:
Expand All @@ -54,13 +51,25 @@ def add_user_to_g():
else:
g.user = None

@app.before_request
def add_favorites_to_g():

if g.user:
if 'curr_user' in session:
g.user = User.query.get(session['curr_user'])
g.favorites = [f.drink for f in g.user.favorites]
else:
else:
g.user = None
g.favorites = None

@app.before_request
def before_request():
add_favorites_to_g()

def check_favorites(drink_id):
if g.favorites:
return drink_id in g.favorites
else:
return False

app.jinja_env.globals.update(check_favorites=check_favorites)


def sess_login(user):
Expand All @@ -84,10 +93,17 @@ def get_random_drink():

return drink_list

def get_drinks_from_database():

from models import Drink
drinks = Drink.query.all()
return drinks

@app.route("/")
def show_home():
"""Show homepage - if logged in showcase random drink"""

drinks = get_drinks_from_database()
if g.user and g.favorites:
return render_template("home.html", drinks=drinks)
elif g.user and not g.favorites:
Expand Down Expand Up @@ -238,8 +254,10 @@ def show_category_drinks(category_id):

category = Category.query.get_or_404(category_id)
drinks = category.drinks
drink_count = len(drinks)
stop_at = 50

return render_template("drinks.html", drinks=drinks, title=f"{category.name}")
return render_template("drinks.html", drinks=drinks, title=f"{category.name}", drink_count=drink_count, stop_at=stop_at)

@app.route("/drinks/add", methods=["GET", "POST"])
def add_drink():
Expand Down
152 changes: 0 additions & 152 deletions generator/cocktail_db.py

This file was deleted.

26 changes: 6 additions & 20 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,11 @@ class User(db.Model):

__tablename__ = 'users'

id = db.Column(
db.Integer,
primary_key=True,
autoincrement=True,
)

username = db.Column(
db.String,
nullable=False,
unique=True,
)

password = db.Column(
db.String,
nullable=False,
)

drinkposts = db.relationship('DrinkPost', backref='creator', lazy=True)

id = db.Column(db.Integer, primary_key=True, autoincrement=True,)
username = db.Column(db.String, nullable=False, unique=True,)
password = db.Column(db.String,nullable=False,)
drinkposts = db.relationship('DrinkPost', back_populates='user', lazy=True)
# backref='creator'

def __repr__(self):
return f"<User #{self.id}: {self.username}, {self.email}>"
Expand Down Expand Up @@ -130,7 +116,7 @@ class DrinkPost(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'))
drink_id = db.Column(db.Integer, db.ForeignKey('drinks.id'))
user = db.relationship('User', backref='add_drink')
user = db.relationship('User', back_populates='drinkposts')
drink = db.relationship('Drink', backref='user')


Expand Down
8 changes: 0 additions & 8 deletions static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ $('body').on('click', '.page-link-num', function(evt) {
show_next_results(btn);
});

// Listen for when plus button is clicked on ingredients form and add
// another field

function add_ingredient_fields() {
let bottom = $('input.ing-form:visible').last();
Expand All @@ -42,7 +40,6 @@ function add_ingredient_fields() {
hide_if_no_more();
}

// Hide plus button once all fields are visible

function hide_if_no_more() {
if ($('.ing-form:hidden').length === 0) {
Expand All @@ -52,7 +49,6 @@ function hide_if_no_more() {

$('.bi-plus-lg').on('click', add_ingredient_fields);

// Button for removing optional ingredient fields after being added

function remove_ingredient_fields(btn) {
let next = $(btn.next());
Expand All @@ -69,8 +65,6 @@ $('#ingredients_form').on('click', '.bi-dash-circle', function(evt) {
remove_ingredient_fields(btn);
});

// Move search bar and logout, login and signup buttons to center
// when nav not collapsed
let clickable = true;
$('body').on('click', '.navbar-toggler-icon', function(evt) {
let btn = $(evt.target);
Expand All @@ -94,13 +88,11 @@ $('body').on('click', '.navbar-toggler-icon', function(evt) {

});

// If on mobile toggle smaller pagination element
if ($('body').width() < 1400) {
$('ul.pagination').toggleClass('pagination-lg');
}


// Trigger share menu when share button clicked
$('body').on('click', 'a.share', function(evt) {
if (navigator.share) {
navigator.share({
Expand Down
9 changes: 0 additions & 9 deletions static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ div.alert {
border-radius: .08cm;
}

/* Responsive Queries for mobile */

/* Extra small - default bootstrap breakpoint */

@media only screen and (max-width: 575px) {

h5.add-msg {
Expand All @@ -94,8 +90,6 @@ div.alert {
}
}

/* Applies to small breakpoint */

@media only screen and (min-width: 576px) and (max-width: 1399px) {
form#search_form {
margin-top: 15px;
Expand All @@ -113,9 +107,6 @@ div.alert {
}
}


/* Applies to large breakpoint */

@media only screen and (min-width: 1400px) {

form#search_form {
Expand Down
Loading

0 comments on commit 981ff1a

Please sign in to comment.