abetusk пре 5 година
родитељ
комит
0c30924daa
3 измењених фајлова са 267 додато и 1 уклоњено
  1. 205 0
      main/app/__init__.py
  2. 61 0
      main/app/routes.py
  3. 1 1
      main/run

+ 205 - 0
main/app/__init__.py

@@ -1,7 +1,212 @@
 #!/usr/bin/env python3
 
 from flask import Flask
+from flask_restful import Resource, Api
+
+from bson import json_util
+import json
+
+import pymysql
+
 
 app = Flask(__name__)
+api = Api(app)
+
+class Database:
+  def __init__(self):
+    host = "127.0.0.1"
+    user = "bus"
+    password = ""
+    db = "busdb"
+
+    self.con = pymysql.connect(host=host, user=user, db=db, cursorclass=pymysql.cursors.DictCursor)
+    self.cur = self.con.cursor()
+
+  def ok(self):
+    self.cur.execute("select rulename from rule_class where id = %s", 1)
+    res = self.cur.fetchall()
+    return res
+
+
+
+#from app import routes
+
+class Card(Resource):
+  def get(self, cardid = None):
+
+    db = Database()
+    db.cur.execute("select logical_card_id, " \
+                   "mag_token, rfid_token, comment, " \
+                   "userid, issuetype, "\
+                   "issued, lastused, firstused, " \
+                   "active, deactivated, group_id " \
+                   "from user_card " \
+                   "where logical_card_id = %s", cardid)
+    res  = db.cur.fetchall()
+    print(res)
+    if len(res)>0:
+      s = json.dumps(res[0], default=json_util.default)
+      return json.loads(s)
+    return {"msg":"no card found", "param":cardid}
+
+  def delete(self):
+    return {"msg":"delete card not implemented"}
+  def put(self):
+    return {"msg":"put/update card not implemented"}
+  def post(self):
+    return {"msg":"post/insert card not implemented"}
+
+class Pass(Resource):
+  def get(self, passid = None):
+
+    db = Database()
+    db.cur.execute("select " \
+                   "user_pass_id, logical_card_id, " \
+                   "issued, activated, deactivated, " \
+                   "firstused, lastused, " \
+                   "nrides_orig, nrides_remain, " \
+                   "nday_orig, nday_expiration, " \
+                   " active, expired, " \
+                   "rule, queue_order, " \
+                   "comment, paytype " \
+                   "from user_pass where user_pass_id = %s",
+                   passid)
+    res  = db.cur.fetchall()
+    print(res)
+    if len(res)>0:
+      s = json.dumps(res[0], default=json_util.default)
+      return json.loads(s)
+    return {"msg":"pass not found", "param":passid}
+
+  def delete(self):
+    return {"msg":"delete pass not implemented"}
+  def put(self):
+    return {"msg":"put/update pass not implemented"}
+  def post(self):
+    return {"msg":"post/insert pass not implemented"}
+
+class User(Resource):
+  def get(self, userid = None):
+
+    db = Database()
+    db.cur.execute("select " \
+                   "username, userid, comment, " \
+                   "first_name, last_name, phone, email, " \
+                   "address, city, state, zip, created, active, " \
+                   "passwordhash, " \
+                   "shipping_address, shipping_city, shipping_state, shipping_zip, " \
+                   "shipping_country_code, shipping_country_name, " \
+                   "reset_attempts " \
+                   "from users where userid = %s",
+                   userid)
+    res  = db.cur.fetchall()
+    print(res)
+    if len(res)>0:
+      s = json.dumps(res[0], default=json_util.default)
+      return json.loads(s)
+    return {"msg":"user not found", "param":passid}
+
+
+  def delete(self):
+    return {"msg":"delete user not implemented"}
+  def put(self):
+    return {"msg":"put/update user not implemented"}
+  def post(self):
+    return {"msg":"post/insert user not implemented"}
+
+class Group(Resource):
+  def get(self):
+    return {"msg":"get group not implemented"}
+  def delete(self):
+    return {"msg":"delete group not implemented"}
+  def put(self):
+    return {"msg":"put/update group not implemented"}
+  def post(self):
+    return {"msg":"post/insert group not implemented"}
+
+class PassOption(Resource):
+  def get(self):
+    return {"msg":"get passoption not implemented"}
+  def delete(self):
+    return {"msg":"delete passoption not implemented"}
+  def put(self):
+    return {"msg":"put/update passoption not implemented"}
+  def post(self):
+    return {"msg":"post/insert passoption not implemented"}
+
+class Rule(Resource):
+  def get(self):
+    return {"msg":"get rule not implemented"}
+  def delete(self):
+    return {"msg":"delete rule not implemented"}
+  def put(self):
+    return {"msg":"put/update rule not implemented"}
+  def post(self):
+    return {"msg":"post/insert rule not implemented"}
+
+
+## Admin related
+##
+
+class AdminUser(Resource):
+  def get(self):
+    return {"msg":"get adminuser not implemented"}
+  def delete(self):
+    return {"msg":"delete adminuser not implemented"}
+  def put(self):
+    return {"msg":"put/update adminuser not implemented"}
+  def post(self):
+    return {"msg":"post/insert adminuser not implemented"}
+
+class AdminGroup(Resource):
+  def get(self):
+    return {"msg":"get admingroup not implemented"}
+  def delete(self):
+    return {"msg":"delete admingroup not implemented"}
+  def put(self):
+    return {"msg":"put/update admingroup not implemented"}
+  def post(self):
+    return {"msg":"post/insert admingroup not implemented"}
+
+class Driver(Resource):
+  def get(self):
+    return {"msg":"get driver not implemented"}
+  def delete(self):
+    return {"msg":"delete driver not implemented"}
+  def put(self):
+    return {"msg":"put/update driver not implemented"}
+  def post(self):
+    return {"msg":"post/insert driver not implemented"}
+
+class Paddle(Resource):
+  def get(self):
+    return {"msg":"get paddle not implemented"}
+  def delete(self):
+    return {"msg":"delete paddle not implemented"}
+  def put(self):
+    return {"msg":"put/update paddle not implemented"}
+  def post(self):
+    return {"msg":"post/insert paddle not implemented"}
+
+class Bus(Resource):
+  def get(self):
+    return {"msg":"get bus not implemented"}
+  def delete(self):
+    return {"msg":"delete bus not implemented"}
+  def put(self):
+    return {"msg":"put/update bus not implemented"}
+  def post(self):
+    return {"msg":"post/insert bus not implemented"}
+
 
 from app import routes
+
+api.add_resource(Card, "/api/v1/card/", "/api/v1/card/<int:cardid>")
+api.add_resource(Pass, "/api/v1/pass/", "/api/v1/pass/<int:passid>")
+api.add_resource(User, "/api/v1/user/", "/api/v1/user/<int:userid>")
+api.add_resource(Group, "/api/v1/group/", "/api/v1/group/<int:groupid>")
+api.add_resource(PassOption, "/api/v1/passoption/", "/api/v1/passoption/<int:passoptionid>")
+api.add_resource(Rule, "/api/v1/rule/", "/api/v1/rule/<int:ruleid>")
+
+if __name__ == '__main__':
+  app.run(debug=True)

+ 61 - 0
main/app/routes.py

@@ -2,6 +2,11 @@
 
 from flask import render_template, url_for
 from app import app
+from flask import request, jsonify
+from flask_restful import Resource, Api
+
+import pymysql
+
 
 @app.route("/")
 @app.route("/index")
@@ -49,4 +54,60 @@ def help():
   usr = { "username":"clementine" }
   return render_template( 'help.html', title='home', user=usr)
 
+###
+
+class Database:
+  def __init__(self):
+    host = "127.0.0.1"
+    user = "bus"
+    password = ""
+    db = "busdb"
+
+    self.con = pymysql.connect(host=host, user=user, db=db, cursorclass=pymysql.cursors.DictCursor)
+    self.cur = self.con.cursor()
+
+  def ok(self):
+    self.cur.execute("select rulename from rule_class where id = %s", 1)
+    res = self.cur.fetchall()
+    return res
+
+
+
+#@app.route("/api/v1/card")
+#def api_card():
+#
+#  print(request.args)
+#  param = request.args
+#
+#  cardid = param.get("id")
+#  mag_token = param.get("mag_token")
+#  rfid_token = param.get("rfid_token")
+#
+#  db = Database()
+#  ok = db.ok()
+#
+#  print(ok)
+#
+#  if cardid:
+#    print("cardid:", cardid)
+#  elif mag_token:
+#    print("mag:", mag_token)
+#  elif rfid_token:
+#    print("rfid:", rfid_token)
+#
+#
+#  return jsonify({ "message": "card not implemented"})
+#
+#@app.route("/api/v1/user")
+#def api_user():
+#  return jsonify({ "message": "user not implemented"})
+#
+#@app.route("/api/v1/pass")
+#def api_pass():
+#  return jsonify({ "message": "pass not implemented"})
+#
+#@app.route("/api/v1/group")
+#def api_group():
+#  return jsonify({ "message": "group not implemented"})
+
 

+ 1 - 1
main/run

@@ -1,4 +1,4 @@
 #!/bin/bash
 
 export FLASK_APP=main.py
-flask run
+flask run --host 0.0.0.0