def update(): form = UserUpdateForm(request) if not form.validate(): return json({'message': 'Validation Failed', 'errors': form.errors}, 422) user = app.auth.user if user.name != form.name and User.nameExists(form.name): return json({'message': 'Validation Failed', 'errors': [ {'code': 'already_exists', 'field': 'name'}]}, 422) if user.email != form.email and User.emailExists(form.email): return json({'message': 'Validation Failed', 'errors': [ {'code': 'already_exists', 'field': 'email'}]}, 422) user.update_values(form.data) try: if form.avatar: user.saveAvatar(form.avatar) if user.update(): app.auth.user = user return json(user, 200) else: return json({'message': 'An error ocurred while creating the user'}, 400) except ValidationException, e: return json({'message': 'Validation Failed', 'errors': [ {'reason': e.reason, 'field': e.field_name}]}, 422)
def create(): form = UserCreateForm(request) if not form.validate(): return json({ 'message': 'Validation Failed', 'errors': form.errors }, 422) try: user = User.create(User(**form.data)) if user: if form.avatar: user.saveAvatar(form.avatar) return json(user, 201) else: return json( {'message': 'An error ocurred while creating the user'}, 400) except ValidationException, e: return json( { 'message': 'Validation Failed', 'errors': [{ 'reason': e.reason, 'field': e.field_name }] }, 422)
def testUpdate(self): user = User( id=2, name='paul', avatar_url= 'https://s3.amazonaws.com/boardhood_user_avatars/sheldoncooper.png' ) user.email = "*****@*****.**" user.password = '******' self.assertTrue(user.update())
def testCreate(self): obj = User.create(User(name=self.name, email=self.name + '@teste.com', password='******')) self.assertEqual(obj.name, self.name) self.assertTrue(is_integer(obj.id), 'ID was supposed to be int. Value: %s' % str(obj.id)) try: name = 'teste&%***(9 "" ' obj = User.create(User(name=name, email=name + '@teste.com', password='******')) self.fail("Create user should not succeed with invalid data") except ValidationException, e: self.assertTrue(True)
def update(): form = UserUpdateForm(request) if not form.validate(): return json({ 'message': 'Validation Failed', 'errors': form.errors }, 422) user = app.auth.user if user.name != form.name and User.nameExists(form.name): return json( { 'message': 'Validation Failed', 'errors': [{ 'code': 'already_exists', 'field': 'name' }] }, 422) if user.email != form.email and User.emailExists(form.email): return json( { 'message': 'Validation Failed', 'errors': [{ 'code': 'already_exists', 'field': 'email' }] }, 422) user.update_values(form.data) try: if form.avatar: user.saveAvatar(form.avatar) if user.update(): app.auth.user = user return json(user, 200) else: return json( {'message': 'An error ocurred while creating the user'}, 400) except ValidationException, e: return json( { 'message': 'Validation Failed', 'errors': [{ 'reason': e.reason, 'field': e.field_name }] }, 422)
def show(): user = User.findById(app.auth.user.id) if user is not None: return json(user.to_self_dict()) else: return json({'message': 'There is no authenticated user'}, 404)
def testCreate(self): obj = User.create( User(name=self.name, email=self.name + '@teste.com', password='******')) self.assertEqual(obj.name, self.name) self.assertTrue(is_integer(obj.id), 'ID was supposed to be int. Value: %s' % str(obj.id)) try: name = 'teste&%***(9 "" ' obj = User.create( User(name=name, email=name + '@teste.com', password='******')) self.fail("Create user should not succeed with invalid data") except ValidationException, e: self.assertTrue(True)
def followers(id, offset=0, limit=20): try: cursor = Interest.cursor() cursor.execute(FOLLOWERS, [str(id), limit, offset]) return User.parse_all(cursor.fetchall()) except Exception, e: Interest.report_error(e, cursor) return False
def show_user(name): form = UserShowForm(url={'name': name}) if not form.validate(): return json({'message': 'Validation Failed', 'errors': form.errors}, 422) data = User.findByName(form.name) if data is not False: return json(data) else: return json({'message': 'User with given name not found'}, 404)
def show_user(name): form = UserShowForm(url={'name': name}) if not form.validate(): return json({ 'message': 'Validation Failed', 'errors': form.errors }, 422) data = User.findByName(form.name) if data is not False: return json(data) else: return json({'message': 'User with given name not found'}, 404)
def create(): form = UserCreateForm(request) if not form.validate(): return json({'message': 'Validation Failed', 'errors': form.errors}, 422) try: user = User.create(User(**form.data)) if user: if form.avatar: user.saveAvatar(form.avatar) return json(user, 201) else: return json({'message': 'An error ocurred while creating the user'}, 400) except ValidationException, e: return json({'message': 'Validation Failed', 'errors': [{'reason': e.reason, 'field': e.field_name}]}, 422)
def show_user_conversations(name): form = UserNameListForm(request, url={'name': name}) if not form.validate(): return json({'message': 'Validation Failed', 'errors': form.errors}, 422) if not User.nameExists(form.name): return json({'message': 'User with given name not found'}, 404) total = Conversation.countAllByUserName(form.name) pg = paginate(form, total) if total > 0: if pg.page_valid: data = Conversation.findAllByUserName(form.name, pg.offset, pg.per_page) else: return json({'message': "This is the end of the list"}, 404) else: data = [] return json(dict(conversations=data, **pg.info))
def show_user_interests(name): form = UserNameListForm(request, url={'name': name}) if not form.validate(): return json({ 'message': 'Validation Failed', 'errors': form.errors }, 422) if not User.nameExists(form.name): return json({'message': 'User with given name not found'}, 404) total = Interest.countAllByUserName(form.name) pg = paginate(form, total) if total > 0: if pg.page_valid: data = Interest.findAllByUserName(form.name, pg.offset, pg.per_page) else: return json({'message': "This is the end of the list"}, 404) else: data = [] return json(dict(interests=data, **pg.info))
def parse(row, **kwargs): if not row: return None if len(kwargs) > 0: row.update(kwargs) obj = Conversation(**row) obj.author = User(id=row.get('user_id'), name=row.get('username'), avatar_url=row.get('avatar_url')) if row.get('lat') and row.get('lon'): obj.location = {'lat': row.get('lat'), 'lon': row.get('lon')} if row.get('interest_id') or row.get('interest_name'): obj.interest = Interest(id=row.get('interest_id'), name=row.get('interest_name')) if row.get('parent_id') or row.get('parent_name'): obj.parent = Conversation(id=row.get('parent_id'), name=row.get('parent_name')) return obj
def testAuthenticate(self): user = User(name='john', password='******') auth = User.authenticate(user) if auth is False: self.fail("User is not authenticated")
def testNameExists(self): self.assertTrue(User.nameExists('john')) self.assertFalse(User.nameExists('dasdasdjskow3'))
def emailExists(self): self.assertTrue(User.emailExists('*****@*****.**')) self.assertFalse(User.emailExists('*****@*****.**'))
from boardhood.helpers.response import json from boardhood.helpers.image import resize from boardhood.helpers.pagination import paginate from boardhood.helpers.auth import require_user_auth from boardhood.models.applications import Application, require_app_auth from boardhood.models.users import User from boardhood.models.interests import Interest from boardhood.models.conversations import Conversation from boardhood.models.base import ValidationException, UniqueViolationException from boardhood.forms import (UserCreateForm, UserUpdateForm, UserListForm, UserNameListForm, UserShowForm, InterestSearchForm, UserActivityForm) module = Blueprint('users', __name__) User.register_app(module, app) Interest.register_app(module, app) Conversation.register_app(module, app) # POST /users ----------------------------------------------------------------- @module.route('/users', methods=['POST']) @require_app_auth(app, Application.LEVEL_ALL) def create(): form = UserCreateForm(request) if not form.validate(): return json({'message': 'Validation Failed', 'errors': form.errors}, 422) try: user = User.create(User(**form.data)) if user:
def testUpdate(self): user = User(id=2, name='paul', avatar_url='https://s3.amazonaws.com/boardhood_user_avatars/sheldoncooper.png') user.email = "*****@*****.**" user.password = '******' self.assertTrue(user.update())
def testExists(self): self.assertTrue(User.exists(1))
def init_app(self, app): self.app = app self.store = app.cache User.register_app(app)
def check_auth(self, auth): user = User(name=auth.username, password=auth.password, ip=request.remote_addr, timestamp=datetime.utcnow()) self.user = User.authenticate(user) return self.user is not False
def testFindByName(self): name = 'john' obj = User.findByName(name) self.assertTrue(obj.name == name)
from boardhood.helpers.response import json from boardhood.helpers.image import resize from boardhood.helpers.pagination import paginate from boardhood.helpers.auth import require_user_auth from boardhood.models.applications import Application, require_app_auth from boardhood.models.users import User from boardhood.models.interests import Interest from boardhood.models.conversations import Conversation from boardhood.models.base import ValidationException, UniqueViolationException from boardhood.forms import (UserCreateForm, UserUpdateForm, UserListForm, UserNameListForm, UserShowForm, InterestSearchForm, UserActivityForm) module = Blueprint('users', __name__) User.register_app(module, app) Interest.register_app(module, app) Conversation.register_app(module, app) # POST /users ----------------------------------------------------------------- @module.route('/users', methods=['POST']) @require_app_auth(app, Application.LEVEL_ALL) def create(): form = UserCreateForm(request) if not form.validate(): return json({ 'message': 'Validation Failed', 'errors': form.errors }, 422)