def note_create(self, data): from apps.notes.models import Note, Note2User try: note = Note() note.name = data['name'] note.status = data['status'] db_session.add(note) db_session.commit() note2user = Note2User() note2user.note = note note2user.user_id = self.id db_session.add(note2user) db_session.commit() if 'items' in data: for item in data['items']: note.item_add(item['name'], item['description']) db_session.commit() return note except Exception as NoteCreatingError: log(NoteCreatingError) db_session.rollback() return 0
def note_get_list(user): try: notes = user.note_list_get() return json_response(200, 'Notes list', notes) except: log() return json_response(400, "Can't get notes")
def logout(self): try: self._delete_session() return True except Exception as LogoutError: log(LogoutError) db_session.rollback() return False
def logout(user): try: if user.logout(): return json_response(200, 'Logout successful') else: return json_response(200, 'You are not login in') except Exception as LogoutError: log(LogoutError) return json_response(200, 'Logout failed')
def delete_me(user): try: if user.delete(): return json_response(200, 'User deleted') return json_response(200, 'Could not delete this user') except Exception as DeleteWhileDeleting: db_session.rollback() log(DeleteWhileDeleting) return json_response(400, 'Could not delete this user')
def note_delete(user, data): try: if user.note_delete(data['id']): return json_response(200, 'Note deleted successfully') return json_response(200, 'Note could not be deleted') except Exception as NoteCreatingError: log(NoteCreatingError) return jsonify(400)
def delete(self): try: self._delete_session() db_session.query(User).filter(User.id == self.id).delete() db_session.commit() return 1 except Exception as UserDeletingError: log(UserDeletingError) db_session.rollback() return 0
def note_create(user, data): try: note = user.note_create(data) note.changes_add(user, user.email + " created note '" + data['name'] + "'") if note: return json_response(200, 'Note created successfully', note.id) return json_response(200, "Can't create this note") except Exception as NoteCreatingError: log(NoteCreatingError) return jsonify(400)
def note_delete(self, note_id): try: note = self.note_get(note_id) if not note: return 0 db_session.delete(note) db_session.commit() return 1 except Exception as NoteDeletingError: log(NoteDeletingError) return 0
def login(cls, email, password): try: user_model = cls.authorize(email, password) if user_model: if not user_model.session or ('token' not in session) or (session['token'] is None): user_model._create_session() return user_model else: return 1 else: return 2 except Exception as LoginUserError: log(LoginUserError) db_session.rollback() return 0
def _create_session(self): try: self._delete_session() sess = Session() sess.user = self session['token'] = sess.token db_session.add(sess) db_session.commit() # self.session = sess # db_session.commit() except Exception as newSessionError: db_session.rollback() log(newSessionError) session['token'] = None
def json_request(self, url, data=None): rv = None try: data = json.dumps(data) rv = self.app.post(url, data=data, follow_redirects=False, content_type='application/json') return json.loads(rv.data) except Exception as JsonRequestError: error = '-------------------------\n' \ 'UNDECODED JSON RESPONSE:{}\n'\ 'URL:{}\n'\ 'ERROR:{}\n'\ '-------------------------'.format(rv, url, JsonRequestError) log(error)
def login(): from libs.tools import get_data try: data = get_data(request) if not data or not ('email' or 'password' in data): return json_response(400, 'Input data is incorrect') res = User.login(data['email'], data['password']) if res == 2: return json_response(400, 'Login or password is incorrect') elif res == 1: return json_response(400, "You're already login in") else: return json_response(200, 'Auth success') except Exception as LoginError: log(LoginError) return json_response(400, 'Auth failed')
def register(cls, email, passwd): try: user = db_session.query(User).filter(User.email == email).first() if user: return 0 password = str(md5(passwd).hexdigest()) user = User(email=email, password=password) user._create_session() db_session.add(user) db_session.commit() return user except Exception as RegisterUserError: log(ReferenceError) db_session.rollback() return 0
def register(): from libs.tools import get_data try: resp = get_data(request) if not resp or not ('email' or 'password' in resp) or (not resp['email'] or not resp['password']): return json_response(400, 'Input data is incorrect') user = User.register(resp['email'], resp['password']) if not user: return json_response(400, 'User with same email already exists') db_session.commit() return json_response(200, 'New user created successfully') except Exception as e: log(e) return json_response(400, 'Input data is incorrect')
def get_request(self, url, data=None): try: rv = self.app.get(url, follow_redirects=False) return json.loads(rv.data) except Exception as GetRequestError: log('get_request:' + str(GetRequestError))