forked from LukasJoswiak/Champion-Rotation-Predictor
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
Add SQLAlchemy, more structure, and Rotation model #2
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,9 @@ scrape/keys.* | |
connect/core/connect.php | ||
css/sass/.sass-cache/ | ||
*.swp | ||
|
||
# VSCode | ||
.vscode/ | ||
|
||
# macOS | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
# Define the WSGI application object | ||
app = Flask(__name__, instance_relative_config=True) | ||
|
||
# Configurations | ||
app.config.from_object('config') | ||
|
||
# Set the database connection string | ||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' | ||
|
||
# Define the database object which is imported | ||
# by modules and controllers | ||
db = SQLAlchemy(app) | ||
|
||
# Import models for DB schema creation | ||
from .models.rotation import Rotation | ||
from .models.champion import Champion | ||
|
||
# Build the database: | ||
# This will create the database file using SQLAlchemy, necessary for views | ||
db.create_all() | ||
|
||
from .views.champions import champions_api | ||
from .views.rotations import rotations_api | ||
|
||
# Register blueprint(s) | ||
app.register_blueprint(champions_api) | ||
app.register_blueprint(rotations_api) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from sqlalchemy import Column, Date, Integer, String | ||
from sqlalchemy.orm import relationship | ||
|
||
from app import db | ||
from .champion_rotation import champion_rotation | ||
|
||
CHAMPION_SQUARE_IMAGE_URL_PREFIX = 'https://ddragon.leagueoflegends.com/cdn/5.15.1/img/champion/' | ||
CHAMPION_TALL_IMAGE_URL_PREFIX = 'https://ddragon.leagueoflegends.com/cdn/img/champion/loading/' | ||
|
||
|
||
class Champion(db.Model): | ||
__tablename__ = 'champions' | ||
id = Column(Integer, primary_key=True) | ||
name = Column(String(50), unique=True) | ||
key = Column(String(50), unique=True) | ||
title = Column(String(50)) | ||
tags = Column(String(255)) | ||
date_released = Column(Date) | ||
rotations = relationship('Rotation', secondary=champion_rotation, back_populates='champions') | ||
|
||
def to_dict(self) -> dict: | ||
return { | ||
'id': self.id, | ||
'name': self.name, | ||
'key': self.key, | ||
'title': self.title, | ||
'tags': self.tags, | ||
'dateRelease': self.date_released.isoformat(), | ||
'squareImage': '{prefix}{key}.png'.format(prefix=CHAMPION_SQUARE_IMAGE_URL_PREFIX, key=self.key), | ||
'tallImage': '{prefix}{key}_0.jpg'.format(prefix=CHAMPION_TALL_IMAGE_URL_PREFIX, key=self.key) | ||
} | ||
|
||
def __repr__(self) -> str: | ||
return '<Champion %r>' % (self.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from sqlalchemy import Column, ForeignKey, Integer, Table | ||
|
||
from app import db | ||
|
||
|
||
champion_rotation = Table('champion_rotations', db.Model.metadata, | ||
Column('rotation_id', Integer, ForeignKey('rotations.week_number')), | ||
Column('champion_id', Integer, ForeignKey('champions.id')) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from sqlalchemy import Column, Date, Integer | ||
from sqlalchemy.orm import relationship | ||
|
||
from app import db | ||
from .champion_rotation import champion_rotation | ||
|
||
|
||
class Rotation(db.Model): | ||
__tablename__ = 'rotations' | ||
week_number = Column(Integer, primary_key=True) | ||
start_date = Column(Date) | ||
end_date = Column(Date) | ||
champions = relationship('Champion', secondary=champion_rotation, back_populates='rotations') | ||
|
||
def to_dict(self) -> dict: | ||
return { | ||
'weekNumber': self.week_number, | ||
'startDate': self.start_date.isoformat(), | ||
'endDate': self.end_date.isoformat() | ||
} | ||
|
||
def __repr__(self) -> str: | ||
return '<Rotation %d>' % (self.week_number) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from flask import Blueprint, jsonify | ||
|
||
from ..models.champion import Champion | ||
|
||
champions_api = Blueprint('champions', __name__, url_prefix='/api/champions') | ||
|
||
|
||
@champions_api.route('/', methods=['GET']) | ||
def get_champions(): | ||
""" | ||
API call that returns all champions. | ||
|
||
Returns: | ||
JSON representation of all champions | ||
""" | ||
champions = [champion.to_dict() for champion in Champion.query.all()] | ||
return jsonify(champions) | ||
|
||
|
||
@champions_api.route('/<int:id>/', methods=['GET']) | ||
def get_champion(id): | ||
""" | ||
API call that returns a single champion by its supplied ID | ||
|
||
Parameters: | ||
id (int): champion ID | ||
|
||
Returns: | ||
JSON representation of champion with corresponding ID | ||
""" | ||
champion = Champion.query.filter_by(id=id).first() | ||
if not champion: | ||
# If empty query, return 404 error | ||
return 'Could not find champion with supplied ID', 404 | ||
|
||
return champion.to_dict() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from flask import Blueprint, jsonify | ||
|
||
from ..models.rotation import Rotation | ||
|
||
rotations_api = Blueprint('rotations', __name__, url_prefix='/api/rotations') | ||
|
||
|
||
@rotations_api.route('/<int:week>', methods=['GET']) | ||
def get_rotation_week(week: int): | ||
""" | ||
Returns a JSON representation of all free champions for the given rotation week. | ||
""" | ||
rotations = Rotation.query.filter(Rotation.week_number == week) | ||
champions = [rotation.champions.to_dict() for rotation in rotations] | ||
return jsonify(champions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import os | ||
|
||
DEBUG = False | ||
SECRET_KEY = os.urandom(24) | ||
SQLALCHEMY_ECHO = False | ||
SQLALCHEMY_TRACK_MODIFICATIONS = False |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
Flask-SQLAlchemy | ||
flask |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# This file is for running the app in development, and should | ||
# be substituted by Gunicorn in production. | ||
from app import app | ||
|
||
app.run(host='0.0.0.0', port=5000, debug=True, threaded=True) | ||
print('champion-rotation-predictor started.') |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please update the links, thank you