Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
Enigma036 committed Apr 1, 2023
1 parent 07eb94d commit afc8621
Show file tree
Hide file tree
Showing 38 changed files with 5,458 additions and 110 deletions.
32 changes: 31 additions & 1 deletion website/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,48 @@ def create_app():
app = Flask(__name__)
app.config["SECRET_KEY"] = "TPYfvwikxqxYrtPNOXTTHyTO00E0Y05Y"
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{DB_NAME}"
app.config['JSON_SORT_KEYS'] = False
app.config['JSON_AS_ASCII'] = False
db.init_app(app)


from .views import views
from .messages import messages
from .programmer import programmer
from .auth import auth
from .import_export import import_export
from .api import api


app.register_blueprint(views, url_prefix="/")
app.register_blueprint(messages, url_prefix="/")
app.register_blueprint(programmer, url_prefix="/")
app.register_blueprint(auth, url_prefix='/')
app.register_blueprint(import_export, url_prefix='/')
app.register_blueprint(api, url_prefix='/')

from .models import User, Note

with app.app_context():
db.create_all()

login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)

@login_manager.user_loader
def load_user(id):
return User.query.get(int(id))


return app

def create_first_user():
from .models import User, Note
if User.query.count() == 0:
new_programmer = User(jmeno = "admin", email="[email protected]", username="admin", role="Administrátor",password=generate_password_hash("123456"))
db.session.add(new_programmer)
db.session.commit()




Expand Down
123 changes: 123 additions & 0 deletions website/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
from flask import Blueprint, render_template, redirect, url_for, jsonify, Response, request, abort
from flask_login import login_required, current_user
from datetime import datetime
from .models import Note, User
from website import create_first_user
from .sort_and_filtration import filtrateNotes, getFormData, getSortData, sort_items
from . import db
from sqlalchemy import desc
import json
import uuid


# Dodělat filtraci

api = Blueprint("api", __name__)

@api.route("/users/<string:user_id>/records/<string:record_id>", methods=['GET', 'PUT','DELETE'])
def record(user_id, record_id):

if request.method == "PUT":
note = Note.query.filter_by(id=record_id, user_id=user_id).first()
if note:
try:
data = request.get_json()

new_id = (data["id"])
new_date = datetime.strptime(data["date"], '%Y-%m-%d').date()
new_interval = int(data["time_spent"])
new_language = data["programming_language"]
new_stars = data["rating"]
new_data = data["description"]

if new_id != note.id:
abort(404)
elif new_date == None:
abort(404)
elif new_interval == None or new_interval=="" or int(new_interval) < 1 or int(new_interval) > 1440:
abort(404)
elif new_language == None or new_language.isspace() or len(new_language) < 1 or len(new_language) > 30:
abort(404)
elif new_stars == None or new_stars == "" or int(new_stars) > 5 or int(new_stars) < 0:
abort(404)
elif new_data == None or len(new_data) < 1 or len(new_data) > 300 or new_data.isspace():
abort(404)
else:
note.date = new_date
note.interval = new_interval
note.language = new_language
note.stars = new_stars
note.data = new_data
db.session.commit()
json_soubor = {'id': str(new_id), 'date': str(new_date),'time_spent': str(new_interval), 'programming_language': new_language, 'rating': new_stars, 'description': new_data}
return jsonify(json_soubor), 200
except:
abort(404)
else:
abort(404)



if request.method == "DELETE":
note = Note.query.filter_by(id=record_id, user_id=user_id).first()
if note:
db.session.delete(note)
db.session.commit()
return "", 200
else:
abort(404)


if request.method == "GET":
note = Note.query.filter_by(id=record_id, user_id=user_id).first()
if note:
json_soubor = {'id': str(note.id), 'date': note.date.strftime("%Y-%m-%d"),'time_spent': str(note.interval), 'programming_language': note.language, 'rating': note.stars, 'description': note.data}
return jsonify(json_soubor), 200
else:
abort(404)

@api.route("/users/<string:user_id>/records", methods=['GET', 'POST'])
def records(user_id):
if request.method == "GET":
notes = Note.query.filter_by(user_id=user_id).all()
json_soubor = []
try:
if notes:
for note in notes:
json_soubor.append({'id': str(note.id), 'date': note.date.strftime("%Y-%m-%d"),'time_spent': str(note.interval), 'programming_language': note.language, 'rating': note.stars, 'description': note.data})
return jsonify(json_soubor), 200
else:
abort(404)
except:
abort(404)

if request.method == "POST":
note = Note.query.filter_by(user_id=user_id).first()
try:
data = request.get_json()

new_date = datetime.strptime(data["date"], '%Y-%m-%d').date()
new_interval = int(data["time_spent"])
new_language = data["programming_language"]
new_stars = data["rating"]
new_data = data["description"]

if new_date == None:
abort(404)
elif new_interval == None or new_interval=="" or int(new_interval) < 1 or int(new_interval) > 1440:
abort(404)
elif new_language == None or new_language.isspace() or len(new_language) < 1 or len(new_language) > 30:
abort(404)
elif new_stars == None or new_stars == "" or int(new_stars) > 5 or int(new_stars) < 0:
abort(404)
elif new_data == None or len(new_data) < 1 or len(new_data) > 300 or new_data.isspace():
abort(404)
else:
user = Note(id=str(uuid.uuid4()), date=new_date, interval = new_interval, language = new_language, stars = new_stars, data = new_data, user_id = int(user_id))
db.session.add(user)
db.session.commit()
json_soubor = {'id': str(note.id), 'date': note.date.strftime("%Y-%m-%d"),'time_spent': str(note.interval), 'programming_language': note.language, 'rating': note.stars, 'description': note.data}
return jsonify(json_soubor), 201

except:
abort(404)
59 changes: 59 additions & 0 deletions website/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from flask import Blueprint, render_template, request, flash, redirect, url_for
from .models import User
from werkzeug.security import generate_password_hash, check_password_hash
from website import create_first_user
from . import db
from flask_login import login_user, login_required, logout_user, current_user


auth = Blueprint('auth', __name__)


@auth.route('/login', methods=['GET', 'POST'])
def login():
create_first_user()

er_succ_message = "Přihlášení"
trida = "none"

if request.method == 'POST':
username = request.form.get('user')
password = request.form.get('pass')
check = request.form.get('check')

remember = False

if check:
if check == "on":
remember = True
else:
remember = False


email = User.query.filter_by(email=username).first()
user = User.query.filter_by(jmeno=username).first()

if email:
user = email

if user:
if check_password_hash(user.password, password):
er_succ_message = "Úspěšné přihlášení"
trida = "success"
login_user(user, remember=remember)
return redirect(url_for('views.home'))
else:
er_succ_message = "Špatné heslo"
trida = "error"
else:
er_succ_message = "Váš účet nebyl nalezen"
trida = "error"

return render_template("login_form.html", message = er_succ_message, trida=trida)


@auth.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('auth.login'))
Loading

0 comments on commit afc8621

Please sign in to comment.