diff --git a/.gitignore b/.gitignore index f41e2f0..1aa5f67 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ *.pyc */__pycache__ db/*.sql + +credentials.database +.vscode/* diff --git a/Dockerfile b/Dockerfile index e2b6d95..b0b9adb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,14 @@ FROM python:3.6 + WORKDIR /home/app/api -ADD . $WORKDIR + +COPY requirements.txt $WORKDIR + RUN pip install -r requirements.txt + +COPY . $WORKDIR + +ENV PYTHONUNBUFFERED 1 + EXPOSE 5000 -CMD python3 -u main.py +CMD python main.py diff --git a/main.py b/main.py index 3bf4da3..9044c55 100644 --- a/main.py +++ b/main.py @@ -1,46 +1,45 @@ -from database_credentials import get_db_credentials -import flask -from flask import jsonify - -app = flask.Flask(__name__) -app.config["DEBUG"] = True -db_creds = get_db_credentials() - -hadiths = [ - {'id': 1, - 'collection_id': 1, - 'collection': 'Sahih al-Bukhari', - 'book_id': 1, - 'book': 'Revelation', - 'chapter_id': 1, - 'chapter': 'How the Divine Revelation started being revealed to Allah\'s Messenger', - 'text': 'I heard Allah\'s Messenger (ﷺ) saying, "The reward of deeds depends upon the intentions and every person will get the reward according to what he has intended. So whoever emigrated for worldly benefits or for a woman to marry, his emigration was for what he emigrated for."'} -] - -@app.route('/', methods=['GET']) -def home(): - return "

Welcome to sunnah.com API.

" - -@app.route('/v1/collections', methods=['GET']) -def api_collections(): - return jsonify(collections()) - -@app.route('/v1/collections/', methods=['GET']) -def api_collection(collection_id): - result = [collection for collection in collections() if collection['collection_id'] == collection_id ] - return jsonify(result) - -@app.route('/v1/collections//books//hadiths', methods=['GET']) -def api_hadiths(collection_id, book_id): - result = [hadith for hadith in hadiths if hadith['collection_id'] == collection_id and hadith['book_id'] == book_id] - return jsonify(result) - -def collections(): - return [collection(hadith) for hadith in hadiths] - -def collection(hadith): - return {'collection': hadith['collection'], 'collection_id': hadith['collection_id']} - -app.run( - host='0.0.0.0' -) +from database_credentials import get_db_credentials +from flask import Flask, jsonify +from models import db, HadithCollection + +app = Flask(__name__) +app.config['DEBUG'] = True +db_creds = get_db_credentials() +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}'.format(**db_creds) +db.init_app(app) + +hadiths = [ + {'id': 1, + 'collection_id': 1, + 'collection': 'Sahih al-Bukhari', + 'book_id': 1, + 'book': 'Revelation', + 'chapter_id': 1, + 'chapter': 'How the Divine Revelation started being revealed to Allah\'s Messenger', + 'text': 'I heard Allah\'s Messenger (ﷺ) saying, "The reward of deeds depends upon the intentions and every person will get the reward according to what he has intended. So whoever emigrated for worldly benefits or for a woman to marry, his emigration was for what he emigrated for."'} +] + +@app.route('/', methods=['GET']) +def home(): + return "

Welcome to sunnah.com API.

" + +@app.route('/v1/collections', methods=['GET']) +def api_collections(): + queryset = HadithCollection.query.all() + collections = [x.serialize() for x in queryset] + return jsonify(collections) + +@app.route('/v1/collections/', methods=['GET']) +def api_collection(collection_id): + collection = HadithCollection.query.get_or_404(collection_id) + return jsonify(collection.serialize()) + +@app.route('/v1/collections//books//hadiths', methods=['GET']) +def api_hadiths(collection_id, book_id): + result = [hadith for hadith in hadiths if hadith['collection_id'] == collection_id and hadith['book_id'] == book_id] + return jsonify(result) + + +if __name__ == '__main__': + app.run(host='0.0.0.0') diff --git a/models.py b/models.py new file mode 100644 index 0000000..d7b40f5 --- /dev/null +++ b/models.py @@ -0,0 +1,35 @@ +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() + +class HadithCollection(db.Model): + __tablename__ = 'Collections' + collectionID = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(100), nullable=False) + collectiontype = db.Column('type', db.String(30), nullable=False) + englishTitle = db.Column(db.String(200), nullable=False) + arabicTitle = db.Column(db.String(400), nullable=False) + hasvolumes = db.Column(db.String(3), nullable=False) + hasbooks = db.Column(db.String(3), nullable=False) + haschapters = db.Column(db.String(3), nullable=False, default='yes') + numhadith = db.Column(db.Integer, nullable=False) + totalhadith = db.Column(db.Integer) + englishgrade1 = db.Column(db.String(100)) + arabicgrade1 = db.Column(db.String(200), nullable=False) + annotation = db.Column(db.String(300)) + shortintro = db.Column(db.Text, nullable=False) + about = db.Column(db.Text, nullable=False) + status = db.Column(db.String(50), nullable=False) + numberinginfodesc = db.Column(db.Text, nullable=False) + + def serialize(self): + return { + 'id': self.collectionID, + 'name': self.name, + 'title_en': self.englishTitle, + 'title_ar': self.arabicTitle, + 'num_hadith': self.numhadith, + 'total_hadith': self.totalhadith, + 'short_intro': self.shortintro, + 'status': self.status + } diff --git a/requirements.txt b/requirements.txt index f52353a..f8edfc3 100644 Binary files a/requirements.txt and b/requirements.txt differ