def get_list(self, skip=0, limit=25, first_name=None, last_name=None, email=None, _id=None, **kwargs): """Provides list of users. Searches by `email`, `first_name` and `last_name`. """ _query = {} _fields = dict(User._default_fields_.items()) if first_name: _query['first_name'] = first_name if last_name: _query['last_name'] = last_name if email: _query['email'] = email if _id: _query['_id'] = _id _users = User.get_all(_query, fields=_fields, skip=skip, limit=limit, sort_by=[('_id', -1)]) if _users: return rjsonify([user.get_data() for user in _users]) return rjsonify([])
def get_id(self, _id, **kwargs): """ To get single record for a given _id. """ _fields = self._get_detail_fields() _contact = Contact.get({'_id': _id}, fields=_fields) if not _contact: return rjsonify({}) return rjsonify(_contact.get_data())
def get_id(self, _id, **kwargs): """ To get single record for a given _id. The user himself is allowed to see his various scores and contact information. """ if _id == kwargs['current_user']['_id']: _fields = self._get_detail_fields() else: _fields = User._default_fields_ _user = User.get({'_id': _id}, fields=_fields) if not _user: return rjsonify({}) return rjsonify(_user.get_data())
def create(self, first_name, email, last_name='', mobile='', **kwargs): """ Contact Create: first_name :string email :string """ data = { "first_name": first_name, "last_name": last_name, "email": email, "mobile": mobile, } _fields = self._get_detail_fields() _contact = Contact.get({'email': email}, fields=_fields) if _contact: raise CustomException('Sorry, it looks like {} belongs to an existing contact.'.format(email) + "Please try new email", Codes.CONFLICT) try: contact = Contact(data) contact_id = contact.insert() except Exception as e: raise CustomException(str(e), Codes.INVALID_PARAM) contact_data = Contact.get({'_id': contact_id}) return rjsonify(contact_data.get_data())
def delete_id(self, _id, **kwargs): """ Delete record """ contact = Contact.get({'_id': _id}, fields=['first_name', 'email']) if contact: contact.delete() try: contact.delete() except Exception as e: raise CustomException("Error while deleting Contact : {}".format(e)) else: raise CustomException("_id not found", Codes.NOT_FOUND) return rjsonify({'_id': _id, 'is_deleted': True})
def get_list(self, first_name='', last_name='', email='', skip=0, limit=25, **kwargs): """Provides list of contact. Searches by `email`, `first_name` and `last_name`. """ _query = {} if first_name: _query['first_name'] = first_name if last_name: _query['last_name'] = last_name if email: _query['email'] = email _fields = dict(Contact._default_fields_.items()) _contacts = Contact.get_all(_query, fields=_fields, skip=skip, limit=limit, sort_by=[('_id', -1)]) if _contacts: return rjsonify([contact.get_data() for contact in _contacts]) return rjsonify([])
def create(self, first_name, email, password, last_name='', **kwargs): """ Registers new user :param first_name: :param email: :param password: :param last_name: :param kwargs: :return: Required : email, name, password """ _fields = self._get_detail_fields() with suppress(Exception): email = email.lower() _user = User.get({'email': email}, fields=['_id']) password = self.__salt_hashed_password(email, password) if _user: _user_data = _user.get_data() _user_id = _user_data['_id'] _user_valid = User.get({ 'email': email, 'password': password }, fields=['_id']) if not _user_valid: raise CustomException( 'Sorry, it looks like {} belongs to an existing account.'. format(email) + "Please try logging in", Codes.CONFLICT) else: new_user = { 'first_name': first_name, 'last_name': last_name, 'email': email, 'password': password, } try: _user = User(new_user) _user_id = _user.insert() except Exception as e: raise CustomException(str(e), Codes.INVALID_PARAM) user_data = User.get({'_id': _user_id}, fields=_fields).get_data() return rjsonify(user_data)
def edit_id(self, _id, **kwargs): contact = Contact.get({'_id': _id}) if not contact: raise CustomException("Contact Not Found") contact_data = contact.get_data() _u = {} for key in ["first_name", "last_name", "email", "mobile"]: if kwargs.get(key): if kwargs[key] != contact_data.get(key): ignore_last_update = False if not _u.get('$set'): _u['$set'] = {} _u['$set'][key] = kwargs.get(key) try: contact.patch_update(_u, ignore_last_update=ignore_last_update) except Exception as e: raise CustomException("Error while updating Training Lesson : {}".format(e)) return rjsonify(Contact.get({'_id': _id}).get_data())