def add_tag(self, scope, guid, user_id, tag): cur = scope.get_handle() if execute_scalar( cur, "select count(object_guid) from object_tag where user_id=%s and object_guid=%s and lower(tag)=lower(%s)", user_id, guid, tag) == 0: cur.execute( "insert into object_tag (user_id, object_guid, tag) values (%s, %s, %s)", (user_id, guid, tag)) else: raise exception.ConflictException("Tag already exists.")
def vote(self, username, guid, up=True): with self.__create_db_connection__() as conn: with conn.enter_scope() as scope: self.__test_active_user__(scope, username) self.__test_writeable_object__(scope, guid) if not self.__object_db.user_can_vote(scope, guid, username): raise exception.ConflictException("User already rated.") user = self.__user_db.get_user(scope, username) self.__object_db.vote(scope, guid, user["id"], up) scope.complete()
def disable_user(self, username, disabled=True): with self.__create_db_connection__() as conn: with conn.enter_scope() as scope: self.__test_user_exists__(scope, username) details = self.__user_db.get_user(scope, username) if details["blocked"] and disabled: raise exception.ConflictException("User is already blocked.") elif not details["blocked"] and not disabled: raise exception.ConflictException("User is not blocked.") self.__user_db.block_user(scope, username, disabled) tpl = template.AccountDisabledMail(self.__get_language__(details), disabled) tpl.bind(username=username) subject, body = tpl.render() self.__mail_db.push_user_mail(scope, subject, body, details["id"]) mailer.ping(config.MAILER_HOST, config.MAILER_PORT) scope.complete()
def create_object(self, guid, source): # validate parameters: if not validate_guid(guid): raise exception.InvalidParameterException("guid") # create object: with self.__create_db_connection__() as conn: with conn.enter_scope() as scope: db = self.__object_db if db.object_exists(scope, guid): raise exception.ConflictException("Guid already exists.") db.create_object(scope, guid, source) scope.complete()
def favor(self, username, guid, favor=True): with self.__create_db_connection__() as conn: with conn.enter_scope() as scope: self.__test_active_user__(scope, username) self.__test_object_exists__(scope, guid) user = self.__user_db.get_user(scope, username) is_favorite = self.__user_db.is_favorite(scope, user["id"], guid) if favor and is_favorite: raise exception.ConflictException("Favorite already exists.") elif not favor and not is_favorite: raise exception.NotFoundException("Favorite not found.") self.__user_db.favor(scope, user["id"], guid, favor) scope.complete()
def request_account(self, username, email): # validate parameters: if not validate_username(username): raise exception.InvalidParameterException("username") if not validate_email(email): raise exception.InvalidParameterException("email") # store request in database: with self.__create_db_connection__() as conn: with conn.enter_scope() as scope: # test if user account or email already exist: if self.__user_db.username_or_email_assigned(scope, username, email): raise exception.ConflictException("Username or email already assigned.") # generate request id & code: id = util.generate_junk(config.REQUEST_ID_LENGTH, secure=True) while self.__user_db.user_request_id_exists(scope, id): id = util.generate_junk(config.REQUEST_ID_LENGTH, secure=True) code = util.generate_junk(config.REQUEST_CODE_LENGTH, secure=True) # save user request: self.__user_db.create_user_request(scope, id, code, username, email) # generate mail: url = util.build_url("/html/registration/%s?code=%s", config.WEBSITE_URL, id, code) tpl = template.AccountRequestMail(config.DEFAULT_LANGUAGE) tpl.bind(username=username, url=url) subject, body = tpl.render() self.__mail_db.push_mail(scope, subject, body, email) mailer.ping(config.MAILER_HOST, config.MAILER_PORT) scope.complete() return id, code
def follow(self, user1, user2, follow=True): with self.__create_db_connection__() as conn: with conn.enter_scope() as scope: db = self.__user_db if user1 == user2: raise InvalidParameterException("user2") self.__test_active_user__(scope, user1) self.__test_active_user__(scope, user2) is_following = db.is_following(scope, user1, user2) if follow and is_following: raise exception.ConflictException("User is already following the specified account.") elif not follow and not is_following: raise exception.NotFoundException("Couldn't find followed user account.") details1 = db.get_user(scope, user1) details2 = db.get_user(scope, user2) db.follow(scope, details1["id"], details2["id"], follow) scope.complete()
def update_user_details(self, username, email, firstname, lastname, gender, language, protected): # validate parameters: if not validate_email(email): raise exception.InvalidParameterException("email") if not validate_firstname(firstname): raise exception.InvalidParameterException("firstname") if not validate_lastname(lastname): raise exception.InvalidParameterException("lastname") if not validate_gender(gender): raise exception.InvalidParameterException("gender") if not validate_language(language): raise exception.InvalidParameterException("language") if not isinstance(protected, bool): if isinstance(protected, str) and (protected.lower() in ["true", "false"]): protected = util.to_bool(protected) else: raise exception.InvalidParameterException("protected") # update user details: with self.__create_db_connection__() as conn: with conn.enter_scope() as scope: self.__test_active_user__(scope, username) # test email if not self.__user_db.user_can_change_email(scope, username, email): raise exception.ConflictException("Email address is already assigned.") # update user details: self.__user_db.update_user_details(scope, username, email, firstname, lastname, gender, language, protected) scope.complete()