botEdukadu/ramen/api.py
2022-10-24 15:05:26 +02:00

194 lines
7.2 KiB
Python

#Szefowe Zadanka Restful API
#Created by Kacper Kostka from CubeSoftware
#-------------------------------
#Copyright (c) 2021-2022 CubeSoftware
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the #Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the #Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import datetime
import os
import uuid
import configparser
from functools import wraps
import jwt # PYJWT
from flask import Flask, request, jsonify, make_response
from flask_marshmallow import Marshmallow
from flask_sqlalchemy import SQLAlchemy
global rankid
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SECRET_KEY'] = 'This is secret'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
secret_key = app.config['SECRET_KEY']
db = SQLAlchemy(app)
ma = Marshmallow(app)
class QuizAnswer(db.Model):
id = db.Column(db.Integer, primary_key=True)
public_id = db.Column(db.String(50), unique=True)
name = db.Column(db.String(50))
author = db.Column(db.String(50))
subject = db.Column(db.String(50))
question = db.Column(db.String(500))
week = db.Column(db.Integer())
answer = db.Column(db.String(3000))
def __init__(self, public_id, name, author, subject, question, week, answer):
self.public_id = public_id
self.name = name
self.author = author
self.subject = subject
self.question = question
self.week = week
self.answer = answer
class QuizAnswerSchema(ma.Schema):
class Meta:
fields = ('public_id', 'name', 'author', 'subject', 'question', 'week', 'answer')
class ExcersiseAnswer(db.Model):
id = db.Column(db.Integer, primary_key=True)
public_id = db.Column(db.String(50), unique=True)
name = db.Column(db.String(50))
author = db.Column(db.String(50))
subject = db.Column(db.String(50))
week = db.Column(db.Integer())
answer = db.Column(db.String(3000))
def __init__(self, public_id, name, author, subject, question, week, answer):
self.public_id = public_id
self.name = name
self.author = author
self.subject = subject
self.week = week
self.answer = answer
class Test(db.Model):
id = db.Column(db.Integer, primary_key=True)
public_id = db.Column(db.String(50), unique=True)
name = db.Column(db.String(50))
subject = db.Column(db.String(50))
date = db.Column(db.String(50))
topic = db.Column(db.String(50))
def __init__(self, public_id, name, subject, date, topic):
self.public_id = public_id
self.name = name
self.subject = subject
self.date = date
self.topic = topic
class TestSchema(ma.Schema):
class Meta:
fields = ('public_id', 'name', 'subject', 'date', 'topic')
# Schema
quiz_answer_schema = QuizAnswerSchema()
quiz_answers_schema = QuizAnswerSchema(many=True)
test_schema = TestSchema()
tests_schema = TestSchema(many=True)
excersise_answer_schema = QuizAnswerSchema()
excersise_answers_schema = QuizAnswerSchema(many=True)
# make a request for creating a new test
@app.route('/newtest', methods=['POST'])
def create_test():
public_id = str(uuid.uuid4())
name = request.json['name']
subject = request.json['subject']
date = request.json['date']
topic = request.json['topic']
new_test = Test(public_id, name, subject, date, topic)
db.session.add(new_test)
db.session.commit()
return test_schema.jsonify(new_test)
# make a request for creating a new quiz answer
@app.route('/newquizanswer', methods=['POST'])
def create_quiz_answer():
public_id = str(uuid.uuid4())
name = request.json['name']
author = request.json['author']
subject = request.json['subject']
question = request.json['question']
week = request.json['week']
answer = request.json['answer']
new_quiz_answer = QuizAnswer(public_id, name, author, subject, question, week, answer)
db.session.add(new_quiz_answer)
db.session.commit()
return quiz_answer_schema.jsonify(new_quiz_answer)
# make a request for creating a new excersise answer
@app.route('/newexcersiseanswer', methods=['POST'])
def create_excersise_answer():
public_id = str(uuid.uuid4())
name = request.json['name']
author = request.json['author']
subject = request.json['subject']
week = request.json['week']
answer = request.json['answer']
new_excersise_answer = ExcersiseAnswer(public_id, name, author, subject, week, answer)
db.session.add(new_excersise_answer)
db.session.commit()
return excersise_answer_schema.jsonify(new_excersise_answer)
# make a request for getting last 6 tests
@app.route('/last6tests', methods=['GET'])
def get_last_6_tests():
last_6_tests = Test.query.order_by(Test.id.desc()).limit(6).all()
result = tests_schema.dump(last_6_tests)
return jsonify(result)
# make a request for getting all tests
@app.route('/alltests', methods=['GET'])
def get_all_tests():
all_tests = Test.query.all()
result = tests_schema.dump(all_tests)
return jsonify(result)
# make a request for getting all quiz answers
@app.route('/allquizanswers', methods=['GET'])
def get_all_quiz_answers():
all_quiz_answers = QuizAnswer.query.all()
result = quiz_answers_schema.dump(all_quiz_answers)
return jsonify(result)
# make a request for getting all quiz answers with specific week
@app.route('/allquizanswers/<week>', methods=['GET'])
def get_all_quiz_answers_with_week(week):
all_quiz_answers = QuizAnswer.query.filter_by(week=week).all()
result = quiz_answers_schema.dump(all_quiz_answers)
return jsonify(result)
# make a request for getting all excersise answers
@app.route('/allexcersiseanswers', methods=['GET'])
def get_all_excersise_answers():
all_excersise_answers = ExcersiseAnswer.query.all()
result = excersise_answers_schema.dump(all_excersise_answers)
return jsonify(result)
# make a request for getting all excersise answers with specific week and subject
@app.route('/allexcersiseanswers/<week>/<subject>', methods=['GET'])
def get_all_excersise_answers_with_week_and_subject(week, subject):
all_excersise_answers = ExcersiseAnswer.query.filter_by(week=week, subject=subject).all()
result = excersise_answers_schema.dump(all_excersise_answers)
return jsonify(result)
# Run the app
if __name__ == '__main__':
app.run(debug=True)