def sign_up(): if request.method == 'POST': req = request.get_json(force=True) name = req['name'] email = req['email'] password = req['password'] if (re.search('^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$', email)): if (User.query_by_email(email) is not None): output = response_generator(None, 304, 'ایمیل تکراری است') return jsonify(output) new_user = User(name, email, password) new_user.save() #User.initialize_motivations (user.id, user.motivation_count) t = Thread(target=User.initialize_motivations, args=(new_user.id, new_user.motivation_count)) t.daemon = True t.start() output = response_generator(new_user.serialize_one(), 200, 'ثبت نام با موفقیت انجام شد') return jsonify(output) output = response_generator(None, 304, 'لطفا ایمیل را به درستی وارد کنید') return jsonify(output) output = response_generator(None, 405, 'method is not POST') return jsonify(output)
def get_media(mediaType=None): if mediaType: if int(mediaType) == 1: media = Video.query.all() output = response_generator(Video.serialize_many(media), 200, 'OK') return jsonify(output) elif int(mediaType) == 2: media = Book.query.all() output = response_generator(Book.serialize_many(media), 200, 'OK') return jsonify(output) elif int(mediaType) == 3: media = Podcast.query.all() output = response_generator(Podcast.serialize_many(media), 200, 'OK') return jsonify(output) else: output = response_generator(None, 406, 'wrong media type') return jsonify(output) #show all multimedias else: out = { 'podcasts': Podcast.serialize_many(Podcast.query.all()), 'books': Book.serialize_many(Book.query.all()), 'videos': Video.serialize_many(Video.query.all()), } output = response_generator(out, 200, 'OK') return jsonify(output)
def login(): if request.method == 'POST': req = request.get_json(force=True) email = req['email'] password = req['password'] stored_user = User.query_by_email(email) if (stored_user is not None) and (stored_user.check_password(password)): login_user(stored_user) session['user_id'] = stored_user.id session['role'] = stored_user.role session.permanent = True output = response_generator(stored_user.serialize_one(), 200, 'ورود با موفقیت انجام شد') return jsonify(output) else: if stored_user is None: output = response_generator( None, 401, 'لطفا ایمیل و گذرواژه را به درستی وارد نمایید') return jsonify(output) elif not stored_user.check_password(req['password']): output = response_generator( None, 401, 'لطفا ایمیل و گذرواژه را به درستی وارد نمایید') return jsonify(output) output = response_generator(None, 405, 'method is not POST') return jsonify(output)
def unmark_motivation(motivationId): motivation = Motivation.query.get(motivationId) if motivation: user = User.query.get(session['user_id']) user.remove_bookmark(motivation) output = response_generator(None, 200, 'با موفقیت حذف شد') return jsonify(output) output = response_generator(None, 406, 'motivation id is wrong') return jsonify(output)
def get_category(categoryId=None): if categoryId: category = Category.query.get(int(categoryId)) if (category): output = response_generator(category.serialize_one(), 200, 'OK') return jsonify(output) output = response_generator(None, 406, 'wrong category id') return jsonify(output) categories = Category.query.all() output = response_generator(Category.serialize_many(categories), 200, 'OK') return jsonify(output)
def rename(): if request.method == 'POST': req = request.get_json(force=True) user = User.query.get(session['user_id']) new_name = req['name'] user.rename(new_name) output = response_generator(user.serialize_one(), 200, 'تغییر نام با موفقیت انجام شد') return jsonify(output) output = response_generator(None, 405, 'method is not POST') return jsonify(output)
def change_password(): if request.method == 'POST': req = request.get_json(force=True) user = User.query.get(session['user_id']) new_pw = req['password'] user.change_password(new_pw) output = response_generator(user.serialize_one(), 200, 'تغییر گذرواژه با موفقیت انجام شد') return jsonify(output) output = response_generator(None, 405, 'method is not POST') return jsonify(output)
def get_all_subcategories(categoryId=None): if categoryId: category = Category.query.get(int(categoryId)) if category: subcategories = category.subcategories output = response_generator( SubCategory.serialize_many(subcategories), 200, 'OK') return jsonify(output) output = {'categories': '', 'status': 'category id is wrong'} output = response_generator(None, 406, 'wrong subcategory id') return jsonify(output) subcategories = SubCategory.query.all() output = response_generator(SubCategory.serialize_many(subcategories), 200, 'OK') return jsonify(output)
def logout(): user_id = session.pop('user_id', None) session.pop('role', None) logout_user() user = User.query.get(user_id) output = response_generator(user.serialize_one(), 200, 'شما با موفقیت خارج شدید') return jsonify(output)
def edit_motivation_count(count): if (int(count) < 6) and (int(count) > 0): user = User.query.get(session['user_id']) user.edit_count(int(count)) user.clear_visited_motivations() t = Thread(target=User.reinitialize_motivations, args=(user.id, user.motivation_count)) t.daemon = True t.start() output = response_generator(user.serialize_one(), 200, 'تغییر با موفقیت اعمال شد') return jsonify(output) output = response_generator(None, 304, 'تعداد وارد شده بیش از سقف مجاز می باشد') return jsonify(output)
def get_motivations(subcategoryId): user = User.query.get(session['user_id']) print(user.id, "REQ FOR MOTIV") motivations = [] for i in range(7): motivation_ids = UserMotivation.query.with_entities\ (UserMotivation.motivation_id)\ .filter (UserMotivation.user_id == user.id,\ UserMotivation.subcategory_id == subcategoryId,\ UserMotivation.timestamp == datetime.now().date() + timedelta (days = i)) #add visited == False condition !? for motivation_id in motivation_ids: motivations.append(Motivation.query.get(motivation_id)) output = response_generator(Motivation.serialize_many(motivations), 200, 'OK') return jsonify(output)
def wrapper(*args, **kwargs): if (session['role'] == 'admin') or (session['role'] == 'owner'): return func(*args, **kwargs) else: output = response_generator(None, 403, 'access denied') return jsonify(output)
def get_bookmark(): user = User.query.get(session['user_id']) bookmark = user.get_bookmark() output = response_generator(Motivation.serialize_many(bookmark), 200, 'OK') return jsonify(output)