def addadmin(name, email, password): "Seed Admin data for testing" from server import SQLSession from senateBack.models.userModel import User import datetime import uuid session = SQLSession() admin = User( email=email, public_id=str(uuid.uuid4()), username=name, password=password, registered_on=datetime.datetime.utcnow(), last_updated_on=datetime.datetime.utcnow(), admin=True, varified=True, maintainer=True ) try: session.add(admin) session.commit() session.close() api_key = jwt.encode({'email': admin.email, 'exp':datetime.datetime.utcnow() + datetime.timedelta(days=400)}, os.environ.get('SECRET_KEY')).decode('UTF-8') print("API_KEY: ", api_key) except Exception as e: print("err in creating admin") print(str(e))
def setfixedmenu(): data = request.json from server import SQLSession session = SQLSession() m_ = session.query(FixedMenu).filter_by(day=data['day']).all() l = [(i.id, i.veg) for i in m_] if len(l) == 2: session.close() response_object = { 'status': 'fail', 'message': '{} menu already exist, apply for change'.format(data['day']), } return jsonify(response_object), 400 if len(l) > 0: if l[0][1] == data['veg']: if (data['veg'] == True): veg_ = "veg" else: veg_ = "non-veg" response_object = { 'status': 'fail', 'message': '{} menu already exist for {}, apply for change'.format( veg_, data['day']), } return jsonify(response_object), 400 new_men = FixedMenu(day=data['day'], breakfast=data['breakfast'], lunch=data['lunch'], dinner=data['dinner'], veg=data['veg']) try: session.add(new_men) session.commit() session.close() response_object = { 'status': 'success', 'message': 'saved the menu', } return jsonify(response_object), 200 except Exception as e: session.close() response_object = { 'status': 'fail', 'message': str(e), } return jsonify(response_object), 500
def addadmin(): "Seed Admin data for testing" from server import SQLSession from temp.models.userModel import User import datetime import uuid session = SQLSession() admin = User(email="*****@*****.**", public_id=str(uuid.uuid4()), username="******", password="******", registered_on=datetime.datetime.utcnow(), last_updated_on=datetime.datetime.utcnow(), admin=True, varified=True) session.add(admin) session.commit() session.close()
def save_new_user(data): from server import SQLSession session = SQLSession() print(data) user = session.query(User).filter_by(email=data['email']).first() if not user: new_user = User( public_id=str(uuid.uuid4()), email=data['email'], username=data['name'], veg=data['isVeg'], reg_id=data['reg_id'], sic_id=data['sic_id'], role=data['role'], password=data['password'], registered_on=datetime.datetime.utcnow(), last_updated_on=datetime.datetime.utcnow() ) try: session.add(new_user) session.commit() session.close() response_object = { 'status': 'Ok', 'message': 'User Created Successful', } return jsonify(response_object), 200 except Exception as e: session.close() response_object = { 'status': 'fail', 'message': 'Problem saving in db', 'error': str(e) } return jsonify(response_object), 500 else: session.close() response_object = { 'status': 'fail', 'message': 'User already exists. Please Log in. or try another email', } return jsonify(response_object), 400
def add_game(data): from server import SQLSession session = SQLSession() game = session.query(Games).filter_by(title=data['title']).first() if not game: new_game = Games(data['title'], data['plateform'], data['score'], data['genre'], data['editors_choice']) try: session.add(new_game) session.commit() print("OK") except: res = {'status': 'Fail', 'message': 'error in db'} print("NOT db") #return jsonify(res) finally: res = {'status': 'OK', 'message': 'successfully added'} #return jsonify(res) else: res = {'status': 'Fail', 'message': 'already_exist'}
def add_daily_menu_from_fixed_menu(date): from server import SQLSession session = SQLSession() str_date = date date = datetime.datetime.strptime(date, "%d %m %Y") date = date.astimezone(timezone('Asia/Kolkata')) print(date) day = date.strftime("%A") print(day) todays_menu = session.query(FixedMenu).filter_by(day=day).all() try: for i in todays_menu: d = { 'breakfast': i.breakfast, 'lunch': i.lunch, 'dinner': i.dinner } for j in d: if d[j] == 'breakfast': t = '7:30 AM - 9:30 AM' elif d[j] == 'lunch': t = '11:45 AM -2:00 PM' else: t = '8:30 PM - 9:30 PM' meal_ = Meal(date=date, type_of_meal=j, items=d[j], veg=i.veg, time_limit=t) session.add(meal_) session.commit() session.close() return True except Exception as e: session.close() print(str(e)) return False
def add_device(): from server import SQLSession session = SQLSession() data = request.json new_device = Device(name=data["name"], port=data["port"], alias=data["alias"], monitoring=data["monitoring"], measurement=data["measurement"], min_intensity=data["min_intensity"], max_intensity=data["max_intensity"], cur_intensity=data["cur_intensity"], location=data["location"], desc=data["desc"], created_on=datetime.datetime.utcnow(), updated_on=datetime.datetime.utcnow()) try: session.add(new_device) session.commit() session.close() return jsonify({"message": "success"}), 200 except Exception as e: return jsonify({"message": str(e)}), 500