def get(self, number): phone = Phone.query.filter(Phone.number == number).first() if phone is None: return fail_response({'user': '******'}, 404) return success_response(to_dict(phone.user))
def post(self, number, current_user): data = request.form.to_dict() message = data['message'] phone = Phone.query.filter(Phone.number == number).first() if phone is None: return fail_response({'message': 'Undefined number'}, 404) contact = Contact.query.filter(Contact.user_id == current_user.id). \ filter(Contact.recipient_id == phone.user_id).first() if contact is None: return fail_response({'message': 'Undefined contact'}, 404) if contact.recipient_id == current_user.id: return fail_response( {'message': 'You can\'t send message to yourself'}, 403) message = Message(recipient_id=contact.recipient_id, message=message, user_id=current_user.id) db.session.add(message) db.session.commit() return success_response(message.to_dict())
def delete(self, number, current_user): if not current_user.has_number(number): return fail_response({'message': 'Forbidden'}, 403) Phone.query.filter(Phone.user_id == current_user.id).delete() User.query.filter(User.id == current_user.id).delete() db.session.commit() return success_response()
def get(self, current_user): data = request.form.to_dict() last_id = data.get('last_id', 0) limit = data.get('limit', 10) contacts = Contact.query.filter(Contact.id > last_id).filter(Contact.user_id == current_user.id)\ .limit(limit).options(joinedload('user')).all() return success_response(to_dict(contacts))
def delete(self, number, current_user): if not current_user.has_contact(number): return fail_response({'message': 'Forbidden'}, 403) phone = Phone.query.filter(Phone.number == number).first() Contact.query.filter(Contact.user_id == current_user.id).\ filter(Contact.recipient_id == phone.user_id).delete() db.session.commit() return success_response()
def post(self, number, current_user): if not current_user.has_number(number): return fail_response({'message': 'Forbidden'}, 403) data = request.form.to_dict() form = UpdateProfileForm(request.form) if not form.validate(): return fail_response(dict(form.errors.items()), 500) user = User.query.filter(User.id == current_user.id).first() for item in data: setattr(user, item, data[item]) db.session.commit() return success_response(to_dict(user))
def post(self): data = request.form.to_dict() form = RegistrationForm(request.form) if not form.validate(): return fail_response(dict(form.errors.items()), 500) user = User(first_name=data['first_name'], last_name=data['last_name']) db.session.add(user) db.session.commit() phone = Phone(number=data['phone'], user_id=user.id) db.session.add(phone) db.session.commit() user = user.to_dict() user['phones'] = [phone.to_dict()] return success_response(user)
def post(self, current_user): data = request.form.to_dict() number = data['number'] phone = Phone.query.filter(Phone.is_active == True).filter( Phone.number == number).first() if phone is None: return fail_response({'message': 'Undefined number'}, 404) if phone.user_id == current_user.id: return fail_response({'message': 'You can\'t add yourself'}, 403) contact = Contact(recipient_id=phone.user_id, user_id=current_user.id) db.session.add(contact) db.session.commit() return success_response(contact.to_dict())
def validate_phone(current_user): data = request.form.to_dict() pin = '1234' try: phone = Phone.query.filter(Phone.number==data['number']).filter(Phone.is_active==False)\ .filter(Phone.user_id == current_user.id).first() if phone is not None and data['pin'] != pin: return fail_response({'message': 'Wrong pin'}, 401) phone.is_active = True db.session.commit() return success_response() except: pass return fail_response({'message': 'Number is wrong'}, 401)
def login(): auth = request.form.to_dict() if not auth or not auth['id'] or not auth['password']: return fail_response({'message': 'Could not verify'}, 401) user = User.query.filter_by(id=auth['id']).first() if not user: return fail_response({'message': 'Could not verify'}, 401) if user.verify(auth['password']): token = Token(token=sha256_crypt.hash(str(urandom(64))), user_id=user.id) db.session.add(token) db.session.commit() user = user.to_dict() user['token'] = token.token return success_response(user) return fail_response({'message': 'Could not verify'}, 401)
def get(self, number, current_user): data = request.form.to_dict() last_id = data.get('last_id', 0) limit = data.get('limit', 10) phone = Phone.query.filter(Phone.number == number).first() if phone is None: return fail_response({'message': 'Undefined number'}, 404) db_messages = Message.query.filter(Message.id > last_id)\ .filter(Message.recipient_id == phone.user_id)\ .filter(Message.user_id == current_user.id) \ .order_by(Message.created_at) \ .limit(limit)\ .all() messages = [] for message in db_messages: messages.append(message.to_dict()) return success_response(messages)
def get(self): users = User.query.filter(User.phones.any()).all() return success_response(to_dict(users))