Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Fixed User tracked_items relationship. Got tracking and add_item pages working. Added remove button for each tracked item on trackings page.
  • Loading branch information
maryjng authored Feb 2, 2022
1 parent 9937e5d commit 1b93cf0
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 90 deletions.
98 changes: 69 additions & 29 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import requests

from sqlalchemy import join
from flask import Flask, render_template, flash, redirect, session, g, url_for
from flask_debugtoolbar import DebugToolbarExtension
from key import API_KEY, SECRET_KEY

from forms import UserAddForm, LoginForm, TrackItemForm
from models import db, connect_db, User, Item, Shops, Shops_Item, user_item
from models import db, connect_db, User, Item, Shops, Shops_Item, User_Item

BASE_URL = "https://api.originsro.org/api/v1/market/list"

Expand All @@ -27,7 +28,6 @@

connect_db(app)


#########################################################################

@app.before_request
Expand All @@ -46,6 +46,7 @@ def add_user_to_g():
def index():



return render_template("home.html")


Expand All @@ -56,39 +57,81 @@ def trackings():
item_ids = [item.id for item in user.tracked_items]
all_shops = []

for id in item_ids:
shops = (Shops_Item
.query
.filter_by(item_id=id)
.order_by(Shops_Item.price)
.limit(1)
.all())
#ORDER BY price ASC LIMIT 1
item = Item.query.get(id)

shop = {"item_name": item.name,
"item_id": shops.item_id,
"price": shops.price,
"timestamp": shops.timestamp}

all_shops.append(shop)

return render_template("trackings.html", shops=all_shops)
if len(item_ids) > 0:
for id in item_ids:
shops = (db.session.query(Shops.owner,
Shops.title,
Shops.map_location,
Shops.map_x,
Shops.map_y,
Shops_Item.item_id,
Shops_Item.price,
Shops_Item.timestamp)
.join(Shops_Item)
.filter(Shops_Item.item_id==id)
.limit(1)
.all())

item = Item.query.get(id)

for owner, title, map_location, map_x, map_y, item_id, price, timestamp in shops:
shop = {"name" : item.name,
"owner": owner,
"title": title,
"map_location": map_location,
"map_x": map_x,
"map_y": map_y,
"item_id": id,
"price": price,
"timestamp": timestamp}
#
# shop = {"item_name": item.name,
# "item_id": id,
# "price": shops.price,
# "timestamp": shops.timestamp,
# "map_location": shops.map_location,
# "map_x": shops.map_x,
# "map_y": shops.map_y}

all_shops.append(shop)

length = len(all_shops)

return render_template("trackings.html", shops=all_shops, length=length)

return redirect(url_for("login"))


@app.route("/tracking/<id>", methods=["GET"])
def track_item(id):
if not g.user:
flash("Access unauthorized.", "danger")
return redirect("/login")

user = g.user
prices = db.session.query(Shops_Item).filter_by(item_id=id).order_by(Shops_Item.price.asc()).all()
item = Item.query.get(id)
id=id

return render_template("item_tracking.html", prices=prices, name=item.name, id=id)


@app.route("/tracking/remove/<id>", methods=["POST"])
def remove_item(id):
if not g.user:
flash("Access unauthorized.", "danger")
return redirect("/login")

user = g.user
prices = db.session.query(Shops_Item).filter(item_id=id).order_by(asc(Shops_Item.price)).all()
item = Item.query.get(id)

tracked_items = user.tracked_items

return render_template("item_tracking.html", prices=prices)
if item in tracked_items:
user.tracked_items = [tracked for tracked in tracked_items if tracked != item]
db.session.commit()

return redirect(url_for("trackings"))


@app.route("/login", methods=["GET", "POST"])
Expand All @@ -114,7 +157,7 @@ def register():
try:
User.signup(
username = form.username.data,
email = form.username.data,
email = form.email.data,
password = form.password.data)

db.session.commit()
Expand Down Expand Up @@ -145,22 +188,19 @@ def add_item():
flash("Access unauthorized.", "danger")
return redirect("/")

user = g.user
# dropdown menu of items in database
items = [(i.id, i.name) for i in Item.query.all()]
form.item_name.choices = items

if form.validate_on_submit():
item_id = form.item_name.data
tracked_items = g.user.tracked_items

item = Item.query.get(item_id)

if item_id not in tracked_items:
tracked_items.append(item)
if item_id not in user.tracked_items:
user.tracked_items.append(item)
db.session.commit()

return render_template("trackings.html")

return render_template("add_item.html", form=form)

##########################################################
Expand Down
75 changes: 36 additions & 39 deletions func.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,42 @@

item_ids = [item.item_id for item in items]

def store_results(res):
shops = requests.get("https://api.originsro.org/api/v1/market/list", params={"api_key": API_KEY})
shops_res = shops.json()

shops_data = []
prices_data = []

#get Shops data
for shop in shops_res["shops"]:
for item in shop["items"]:
if item["item_id"] in item_ids:
location = shop["location"]
try:
db.session.add(Shops(owner=shop["owner"],
title=shop["title"],
map_location=location["map"],
map_x=location["x"],
map_y=location["y"])
)

except error as IntegrityError:
print(error)

try:
db.session.add(Shops_Item(item_id=item["item_id"],
price=item["price"],
owner=shop["owner"],
timestamp=shop["creation_date"])
)
except error as IntegrityError:
print(error)

break

db.session.commit()

# def ping_API():
# return requests.get("https://api.originsro.org/api/v1/ping")
#
Expand Down Expand Up @@ -45,46 +81,7 @@ def get_current_data():
return shops_res


def store_results(res):
shops = requests.get("https://api.originsro.org/api/v1/market/list", params={"api_key": API_KEY})
shops_res = shops.json()

shops_data = []
prices_data = []

#get Shops data
for shop in shops_res["shops"]:
for item in shop["items"]:
if item["item_id"] in item_ids:

location = shop["location"]
#
# try:
db.session.add(Shops(owner=shop["owner"],
title=shop["title"],
map_location=location["map"],
map_x=location["x"],
map_y=location["y"])
)

# except:
# print(f"Failed to add {shop_obj} to the Shops table.")

#get price history data
# for item in shops_res["items"]:

# try:
db.session.add(Shops_Item(item_id=item["item_id"],
price=item["price"],
owner=shop["owner"],
timestamp=shop["creation_date"])
)

break
# except:
# print(f"Failed to add {shop_item} to the Shops_Item table.")

db.session.commit()


def request_and_store_data():
Expand Down
34 changes: 25 additions & 9 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@
db = SQLAlchemy()

#table showing relationships between User and Item tables
user_item = db.Table('user_item',
db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
db.Column('item_id', db.Integer, db.ForeignKey('items.id'))
)
# user_item = db.Table('user_item',
# db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
# db.Column('item_id', db.Integer, db.ForeignKey('items.id'))
# )

class User_Item(db.Model):
__tablename__ = "user_item"

user_id = db.Column(
db.Integer,
db.ForeignKey('users.id', ondelete="cascade"),
primary_key=True
)

item_id = db.Column(
db.Integer,
db.ForeignKey('items.id', ondelete="cascade"),
primary_key=True
)


class User(db.Model):
__tablename__ = "users"
Expand All @@ -23,8 +39,8 @@ class User(db.Model):
tracked_items = db.relationship(
"Item",
secondary="user_item",
backref="following_users"
)
backref="users"
)

@classmethod
def signup(cls, username, email, password):
Expand Down Expand Up @@ -54,7 +70,6 @@ def authenticate(cls, username, password):

return False


class Item(db.Model):
""" All items in the game """

Expand All @@ -63,7 +78,8 @@ class Item(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text, nullable=False)

current_shops = db.relationship("Shops", secondary="shops_item", backref="item")
# tracking_users = db.relationship("User", backref="items", lazy=True)
current_shops = db.relationship("Shops", secondary="shops_item", backref="items")


class Shops(db.Model):
Expand All @@ -77,7 +93,7 @@ class Shops(db.Model):
map_x = db.Column(db.Integer, nullable=False)
map_y = db.Column(db.Integer, nullable=False)

items = db.relationship("Item", secondary="shops_item", backref="shops")
# items = db.relationship("Item", secondary="shops_item", backref="shops")


class Shops_Item(db.Model):
Expand Down
10 changes: 5 additions & 5 deletions templates/item_tracking.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

{% block content %}
{% if g.user %}
<h2> {{ prices.name }} | ID: {{ prices.item_id }}</h2>
<h2> {{ name }} | ID: {{ id }}</h2>
<ul>
{% for price in prices %}

<li>{{ prices.price}}</li>
<li>{{ prices.owner }}</li>
<li>{{ prices.timestamp }}</li>

<li> Zeny per: {{ price.price}}</li>
<li> Shop Owner: {{ price.owner }}</li>
<li> Store Opened: {{ price.timestamp }}</li>
<br>
{% endfor %}
</ul>
{% endif %}
Expand Down
30 changes: 22 additions & 8 deletions templates/trackings.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,32 @@

{% block content %}
{% if g.user %}
<ul>
<div>
<a href="/add">Add Item</a>
{% for shop in shops %}
</div>
<ul>

{% if length == 0 %}
<h3>No items on tracking list.</h3>
{% else %}
{% for shop in shops %}
<div>

<li>
<a href="tracking/{{ shop.item_id }}">{{ shop.item_name }}</a>
</li>
<li>{{ shop.price }}</li>
<li>{{ shop.item_id }}</li>
<li>
<a href="tracking/{{ shop.item_id }}">{{ shop.name }}</a>
| Item ID: {{ shop.item_id }}
<form method="POST" action="/tracking/remove/{{ shop.item_id }}" id="remove-item-form">
<button class="btn btn-sm">Remove</button>
</form>
</li>

{% endfor %}
<li>Price: {{ shop.price }}z</li>
<li>Location: {{ shop.map_location }} at ({{ shop.map_x }}, {{ shop.map_y }})</li>
<div>
<br>
{% endfor %}
</ul>
{% endif %}
{% endif %}

{% endblock %}

0 comments on commit 1b93cf0

Please sign in to comment.