def get_outbox(self, owner_userid, glob, limit, offset): """ Gets a list of a user's sent messages. @param owner_username: User to get messages for. @type owner_username: String @param glob: Dictionary of options (count_only, order_by, order_dir) @type glob: Dictionary @param limit: Maximum number of messages to return @type limit: Integer @param offset: Position to start returning messages @type offset: Integer """ try: owner_userid = validation.cast_integer(owner_userid, 'owner_userid') if glob.has_key('order_by'): validation.oneof(glob['order_by'], ('message_id', 'to_username', 'subject', 'body', 'status', 'date_updated'), 'order_by') if glob.has_key('order_dir'): validation.oneof(glob['order_dir'], ('asc', 'desc'), 'order_dir') limit = validation.cast_integer(limit, 'limit') offset = validation.cast_integer(offset, 'offset') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def rotate(self, image_id, direction): """ Rotates an image. The newly rotated image will be stored in media_binaries. Also see: - L{crop} for cropping an image. - L{normalize} for auto-balancing the contrast of an image. - L{remove_filters} for removing filters from an image. @param owner_username: Username @type owner_username: String @param media_id: Media ID @type media_id: String @param direction: Direction to rotate image. Either ('cw' or 'ccw') for clock-wise or counter-clock-wise. @type direction: String @return: New Image ID @rtype: (Deferred) String """ try: image_id = validation.cast_integer(image_id, 'image_id') validation.oneof(direction, ('ccw', 'cw'), direction) except errors.ValidationError, ex: self.log.warning("Validation failure: %s" % str(ex)) raise errors.APIError, str(ex)
def set_account_album_permission(self, owner_userid, perm_type, flag, groups): """ Sets the permissions on a user's account for albums. @param owner_userid: userid of the Owner username @type owner_username: Int @param perm_type: Type of permission being set (view/comment) @type perm_type: String @param flag: Permission state - one of (0, 1, 2, 3) @type flag: Integer @param groups: New group access list, if appropriate @type groups: List/tuple """ try: owner_userid = validation.cast_integer(owner_userid, 'owner_userid') validation.oneof(perm_type, ('view', 'comment'), 'perm_type') flag = validation.cast_integer(flag, 'flag') groups = validation.sequence(groups, 'groups') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_complete_list_with_relations_owner(self, owner_userid, glob, limit, sort): """ Get *all* tags for owner_username's images and mark any that match the glob. @param owner_username: Owner of the account @type owner_username: String @param glob: A complex dictionary of limits for the user_images table see Glober.py for more details. @type glob: Dictionary """ valid_sorts = { 'title-asc': ("t1.tag_name", "asc"), 'title-desc': ("t1.tag_name", "desc"), 'date_asc': ("date_added", "asc"), 'date_desc': ("date_added", "desc"), 'count-asc': ("cnt_images", "asc"), 'count-desc': ("cnt_images", "desc"), 'recent': ("last_used", "desc") } try: owner_userid = validation.cast_integer(owner_userid, 'owner_userid') validation.required(glob, 'glob') limit = validation.cast_integer(limit, 'limit') validation.oneof(sort, valid_sorts.keys(), 'sort') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def set_image_permission(self, owner_userid, image_id, perm_type, flag, groups): """ Sets the flag and groups for a specific perm type (view, tag, etc) @param owner_userid: Userid of the user who owns the image @type owner_userid: Integer @param image_id: ID of the image @type media_id: Int @param perm_type: Permission being set @type perm_type: String @param flag: Permission value @type flag: Integer @param groups: (optional) groups to be added if flag = 2 @type groups: List """ try: owner_userid = validation.cast_integer(owner_userid, "owner_userid") image_id = validation.cast_integer(image_id, 'image_id') validation.oneof(perm_type, ('view', 'tag', 'comment', 'print', 'download', 'geotag', 'vote', 'blog'), 'perm_type') flag = validation.cast_integer(flag, 'flag') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_list(self, owner_userid, auth_userid, include_public_tags=0, order_by="tag_name", order_dir="asc"): """ Returns a flat list of tags. Each item in the list is a dictionary. Each dictionary having the following elements: - tag_name - cnt_images @param browse_userid: browse_userid (the user to find tags for) @type browse_userid: Int @param auth_userid: auth_userid (the logged in user making the request) @type browse_userid: String @param include_public_tags: Whether to show only tags used by this user, or everyone's tags on his photos @type include_public_tags: Boolean @param order_by: One of ('tag_name', 'cnt_images') @type order_by: String @param order_dir: One of ('asc', 'desc') @type order_dir: String @return: List of tags @rtype: List """ try: owner_userid = validation.cast_integer(owner_userid, 'owner_userid') if auth_userid: auth_userid = validation.cast_integer(auth_userid, 'auth_userid') validation.oneof(order_by, ('tag_name', 'cnt_images'), 'order_by') validation.oneof(order_dir, ('asc', 'desc'), 'order_dir') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_complete_list_with_relations_owner(self, owner_userid, glob, limit, sort): """ Get *all* tags for owner_username's images and mark any that match the glob. @param owner_username: Owner of the account @type owner_username: String @param glob: A complex dictionary of limits for the user_images table see Glober.py for more details. @type glob: Dictionary """ valid_sorts = { 'title-asc': ("t1.tag_name", "asc"), 'title-desc': ("t1.tag_name", "desc"), 'date_asc': ("date_added", "asc"), 'date_desc': ("date_added", "desc"), 'count-asc': ("cnt_images", "asc"), 'count-desc': ("cnt_images", "desc"), 'recent':("last_used", "desc") } try: owner_userid = validation.cast_integer(owner_userid, 'owner_userid') validation.required(glob, 'glob') limit = validation.cast_integer(limit, 'limit') validation.oneof(sort, valid_sorts.keys(), 'sort') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_featured_media(self, owner_userid, auth_userid, order_by="date_added", order_dir="desc", offset=0, limit=10): """ Get the list of featured media for a user, ordered by date added desc @param owner_username: the user to fetch the list for @type owner_username: String @param order_by: The database ordering. one of 'owner_username', 'media_id', 'date_added', or 'random' @type order_by: String @param order_dir: The database ordering direction. one of 'asc' or 'desc' @type order_dir: String @param offset: The starting row of the return set @type offset: Integer @param limit: The max number of rows to retreive @type limit: Integer @return: [ { media_id: MEDIA_ID, date_added: TIMESTAMP, owner_username: OWNER_USERNAME title: TITLE, description: DESCRIPTION }... ] @rtype: List of dictionaries """ try: owner_userid = validation.cast_integer(owner_userid, 'owner_userid') if auth_userid: auth_userid = validation.cast_integer(auth_userid, 'auth_userid') validation.oneof( order_by, ('owner_username', 'media_id', 'date_added', 'random'), 'order_by') if order_dir: validation.oneof(order_dir, ('asc', 'desc'), 'order_dir') offset = validation.cast_integer(offset, 'offset') limit = validation.cast_integer(limit, 'limit') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def check_feature(self, username, feature): """ See if a user account has a particular feature enabled @param username: The username we are checking @type username: String @param feature: one of the account constants from constants.py @type feature: String @return: 1 if the feature is enabled for the user, 0 if not @rtype: Integer """ self.log.debug("checking if user %s has %s enabled" % (username, feature)) # valid username? username = validation.username(username) # valid feature? validation.oneof(feature, self.valid_features, "feature") # immediately return true for reserved names if username in self.app.api.users._cfg_invalid_usernames.split(): return 1 # get their account info self.log.debug("getting account info for %s" % username) d = self.app.api.users.get_account_info(username) # check the feature def check(account_info, feature): if account_info.get("account_status", {}).has_key(feature): enabled = account_info.get("account_status", {}).get(feature, 0) else: enabled = account_info.get("account_type", {}).get(feature, 0) if enabled: # they have the feature self.log.debug("user %s has %s enabled" % (username, feature)) try: enabled = int(enabled) return enabled except: return 1 else: # they have the feature self.log.debug("user %s has %s disabled" % (username, feature)) return 0 d.addCallback(check, feature) return d
def get_tag_list(self, auth_userid, owner_userid, limit, offset, sort, count_flag): """ Returns a flat list of tags. Each item in the list is a dictionary. Each dictionary having the following elements: - tag_name - cnt_images @param browse_username: browse_username (the user to find tags for) @type browse_username: String @param auth_username: auth_username (the logged in user making the request) @type browse_username: String @param limit: Max number of tags to return @type limit: Integer @param sort: Tag sort order @type order_by: Integer @param offset: Offset of tags to start at @type offset: Integer @param count_flag: Flag to return only a count @type count_flag: Boolean @return: List of tags @rtype: List """ valid_sorts = { 'title-asc': ("t1.tag_name", "asc"), 'title-desc': ("t1.tag_name", "desc"), 'date_asc': ("date_added", "asc"), 'date_desc': ("date_added", "desc"), 'count-asc': ("cnt_images", "asc"), 'count-desc': ("cnt_images", "desc") } try: owner_userid = validation.cast_integer(owner_userid, 'owner_userid') if auth_userid: auth_userid = validation.cast_integer(auth_userid, 'auth_userid') limit = validation.cast_integer(limit, 'limit') offset = validation.cast_integer(offset, 'offset') validation.oneof(sort, valid_sorts.keys(), 'sort') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def create_archive(self, owner_userid, media_ids, image_size): """ Creates a .zip file the user can download. @param owner_userid: Owner of the images. @type owner_userid: Integer @param media_ids: List of images to be added to the archive. @type media_ids: List """ try: owner_userid = validation.cast_integer(owner_userid, 'owner_userid') if not isinstance(media_ids, (list, tuple)): media_ids = [media_ids] valid_sizes = ["ORIGINAL"] + display_sizes.keys() validation.oneof(image_size, valid_sizes, "image_size") except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def set_attr(self, owner_userid, set_id, key, value): """ Changes an attribute on a set. @param set_id: Set ID @type set_id: Integer @param key: Value to be changed @type key: String @param value: New value @type value: String """ try: set_id = validation.cast_integer(set_id, 'set_id') validation.oneof(key, self.valid_set_attrs, 'key') value = validation.string(value) except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_featured_media( self, owner_userid, auth_userid, order_by="date_added", order_dir="desc", offset=0, limit=10 ): """ Get the list of featured media for a user, ordered by date added desc @param owner_username: the user to fetch the list for @type owner_username: String @param order_by: The database ordering. one of 'owner_username', 'media_id', 'date_added', or 'random' @type order_by: String @param order_dir: The database ordering direction. one of 'asc' or 'desc' @type order_dir: String @param offset: The starting row of the return set @type offset: Integer @param limit: The max number of rows to retreive @type limit: Integer @return: [ { media_id: MEDIA_ID, date_added: TIMESTAMP, owner_username: OWNER_USERNAME title: TITLE, description: DESCRIPTION }... ] @rtype: List of dictionaries """ try: owner_userid = validation.cast_integer(owner_userid, "owner_userid") if auth_userid: auth_userid = validation.cast_integer(auth_userid, "auth_userid") validation.oneof(order_by, ("owner_username", "media_id", "date_added", "random"), "order_by") if order_dir: validation.oneof(order_dir, ("asc", "desc"), "order_dir") offset = validation.cast_integer(offset, "offset") limit = validation.cast_integer(limit, "limit") except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def update_status(self, to_userid, message_id, status): """ Updates the status of a received message (read/replied to). @param to_username: User who received the message @type to_username: String @param message_id: Message to be updated @type message_id: Integer @param status: New status for the message @type status: Integer """ try: to_userid = validation.cast_integer(to_userid, 'to_userid') message_id = validation.cast_integer(message_id, 'message_id') validation.oneof(status, (0, 1, 2), 'status') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def set_album_permission(self, owner_userid, album_id, perm_type, flag, groups): """ Sets the flag and groups for a specific perm type (view, tag, etc) @param owner_userid: User who owns the album @type owner_userid: Int @param album_id: ID of the album @type album_id: String @param perm_type: Permission being set @type perm_type: String @param flag: Permission value @type flag: Integer @param groups: (optional) groups to be added if flag = 2 @type groups: List """ owner_userid = validation.cast_integer(owner_userid, "owner_userid") album_id = validation.cast_integer(album_id, 'album_id') validation.oneof(perm_type, ('view', 'comment'), 'perm_type') flag = validation.cast_integer(flag, 'flag') group_list = [] group_val = "{}" if groups: for g in groups: validation.integer(g, 'group_id') group_list.append(validation.string(g)) group_val = "{%s}" % ', '.join(group_list) d = self.app.db.query( """ SELECT zoto_update_album_permission( %s, %s, %s, %s, %s) """, (owner_userid, album_id, perm_type, flag, group_val)) d.addCallback(lambda _: (0, "success")) d.addErrback(lambda _: (-1, _.getErrorMessage())) return d
def set_album_permission(self, owner_userid, album_id, perm_type, flag, groups): """ Sets the flag and groups for a specific perm type (view, tag, etc) @param owner_userid: User who owns the album @type owner_userid: Int @param album_id: ID of the album @type album_id: String @param perm_type: Permission being set @type perm_type: String @param flag: Permission value @type flag: Integer @param groups: (optional) groups to be added if flag = 2 @type groups: List """ owner_userid = validation.cast_integer(owner_userid, "owner_userid") album_id = validation.cast_integer(album_id, 'album_id') validation.oneof(perm_type, ('view', 'comment'), 'perm_type') flag = validation.cast_integer(flag, 'flag') group_list = [] group_val = "{}" if groups: for g in groups: validation.integer(g, 'group_id') group_list.append(validation.string(g)) group_val = "{%s}" % ', '.join(group_list) d = self.app.db.query(""" SELECT zoto_update_album_permission( %s, %s, %s, %s, %s) """, (owner_userid, album_id, perm_type, flag, group_val)) d.addCallback(lambda _: (0, "success")) d.addErrback(lambda _: (-1, _.getErrorMessage())) return d
def set_attr(self, album_id, owner_userid, key, value): """ Changes an attribute for an album. @param album_id: ID of the album being modified @type album_id: Integer @param owner_userid: User who owns the album @type owner_userid: Integer @param key: Name of the attribute being set @type key: String @param value: New attribute value @type value: String """ try: album_id = validation.cast_integer(album_id, 'album_id') owner_userid = validation.cast_integer(owner_userid, 'owner_userid') validation.oneof(key, self.valid_album_attrs, 'key') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def set_attr(self, owner_userid, image_id, key, value): """ Sets an attribute of an image. @param owner_username: Username @type owner_username: String @param media_id: Image ID @type media_id: String @param key: Field to set. One of ('title', 'description', 'date', 'camera_make', 'camera_model', 'fstop', 'exposure_time', 'focal_length', 'iso_speed', 'rotate_bit', 'flash_fired', 'lat', 'lng', 'alt') @type key: String @param value: Value to set field to. @type: String @return: Nothing @rtype: Nothing """ owner_userid = validation.cast_integer(owner_userid, 'owner_userid') image_id = validation.cast_integer(image_id, 'image_id') validation.required(key, 'key') validation.oneof(key, self.attr_fields.keys(), 'key') if key == 'date': validation.isodatetime(value, 'date') if key == 'lat' or key == 'lng': value = float(value) if key == 'title': value = utils.check_n_chop(value, 30) self.log.debug("setting image [%s] [%s]=>[%s]" % (image_id, key, value)) return self.app.db.runOperation(""" select zoto_image_set_attr(%s, %s, %s, %s) """, (owner_userid, image_id, key, utils.sql_escape(value)))
def get_complete_list_with_relations(self, auth_userid, owner_userid, glob, limit, sort): """ Get *all* tags on owner_username's images and mark any that match the glob @param owner_username: The user's tags we care about @type owner_username: String @param auth_username: The logged-in user that is trying to get the tag list @type auth_username: String @param glob: A complex dictionary of limits for the user_images table see Globber.py for more details @type glob: Dictionary @return: List of Dictionaries including tag_name, image_count, is_related (boolean) """ valid_sorts = { 'title-asc': ("t1.tag_name", "asc"), 'title-desc': ("t1.tag_name", "desc"), 'date_asc': ("date_added", "asc"), 'date_desc': ("date_added", "desc"), 'count-asc': ("cnt_images", "asc"), 'count-desc': ("cnt_images", "desc"), 'recent': ("last_used", "desc") } try: owner_userid = validation.cast_integer(owner_userid, 'owner_id') limit = validation.cast_integer(limit, 'limit') if auth_userid: auth_userid = validation.cast_integer(auth_userid, 'auth_userid') validation.required(glob, 'glob') validation.oneof(sort, valid_sorts.keys(), 'sort') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_image_tags(self, owner_userid, image_id, tag_type='owner'): """ Gets list of tags for image @param owner_username: Image onwer username @type owner_username: String @param media_id: image id @type media_id: String @param tag_type: oneof("owner", "public", "all") filters private tags @type tag_type: String @return: List of Tags @rtype: List """ try: owner_userid = validation.cast_integer(owner_userid, 'owner_userid') image_id = validation.cast_integer(image_id, 'image_id') validation.oneof(tag_type, ('owner', 'public', 'all'), 'tag_type') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_complete_list_with_relations(self, auth_userid, owner_userid, glob, limit, sort): """ Get *all* tags on owner_username's images and mark any that match the glob @param owner_username: The user's tags we care about @type owner_username: String @param auth_username: The logged-in user that is trying to get the tag list @type auth_username: String @param glob: A complex dictionary of limits for the user_images table see Globber.py for more details @type glob: Dictionary @return: List of Dictionaries including tag_name, image_count, is_related (boolean) """ valid_sorts = { 'title-asc': ("t1.tag_name", "asc"), 'title-desc': ("t1.tag_name", "desc"), 'date_asc': ("date_added", "asc"), 'date_desc': ("date_added", "desc"), 'count-asc': ("cnt_images", "asc"), 'count-desc': ("cnt_images", "desc"), 'recent':("last_used", "desc") } try: owner_userid = validation.cast_integer(owner_userid, 'owner_id') limit = validation.cast_integer(limit, 'limit') if auth_userid: auth_userid = validation.cast_integer(auth_userid, 'auth_userid') validation.required(glob, 'glob') validation.oneof(sort, valid_sorts.keys(), 'sort') except errors.ValidationError, ex: return utils.return_deferred_error(ex.value)
def get_methods(self, profile): """ @param profile: Either 'zapi' or 'papi' @type profile: String Returns a list of available ZAPI Methods for documentation purposes. This will be a list of dictionaries, organized like this:: 1. List - Dictionary - 'name': '<name of argument>' - 'methods': - List - Dictionary - 'name': '<name of method>', - 'summary': '<description of method>', - 'args': - List - Dictionary - 'name': '<High level name for arguments>', - 'doc': '<text describing arguemtn>', - 'default': '<default value, if any>', - 'is_last_arg': <true if this is the last argument in list>, - 'is_required': <true if this arguement is required> """ self.log.warning("here inside get_methods") validation.oneof(profile, ('zapi', 'papi'), 'profile') if not self.methods.has_key(profile): # # Assemble in dictionaries, then sort into lists module_dict = {} for api_name in dir(self.app.api): api = getattr(self.app.api, api_name) self.log.warning("processing api: %s" % api.__class__.__name__) self.log.warning("api summary: %s" % api.__class__.__doc__) if hasattr(api, 'enable_zapi') and api.enable_zapi: module_dict[api_name] = {'name': api_name, 'summary': api.__class__.__doc__, 'methods': {}} for attrname in dir(api): if attrname.startswith('xmlrpc_'): method = getattr(api, attrname) self.log.debug("method contents: %s" % method) self.log.debug("checking %s %s" % (api_name, attrname)) if hasattr(method, '_zapi_desc') and hasattr(method, '_zapi_args'): method_name = method.func_name.replace('xmlrpc_', '') temp_args = deepcopy(method._zapi_args) for arg in temp_args: self.log.debug("arg_type: %s" % pformat(arg['type'])) self.log.debug("API: %s" % api.__class__.__name__) self.log.debug("method: %s" % attrname) self.log.debug("arg: %s" % arg['name']) arg['type'] = self._clean_attr(arg['type']) info = {'summary': method._zapi_desc, 'name': method_name, 'args': temp_args, 'is_last_arg': 0 } if info['args']: info['args'][-1]['is_last_arg'] = 1 module_dict[api_name]['methods'][method_name] = info # # Now to sorted lists module_list = [] for module in module_dict.values(): if len(module['methods'].values()) == 0: continue methods = module['methods'].values() methods.sort(lambda a, b: cmp(a['name'], b['name'])) module['methods'] = methods module_list.append(module) module_list.sort(lambda a, b: cmp(a['name'], b['name'])) self.methods[profile] = module_list return self.methods[profile]
def get_methods(self, profile): """ @param profile: Either 'zapi' or 'papi' @type profile: String Returns a list of available ZAPI Methods for documentation purposes. This will be a list of dictionaries, organized like this:: 1. List - Dictionary - 'name': '<name of argument>' - 'methods': - List - Dictionary - 'name': '<name of method>', - 'summary': '<description of method>', - 'args': - List - Dictionary - 'name': '<High level name for arguments>', - 'doc': '<text describing arguemtn>', - 'default': '<default value, if any>', - 'is_last_arg': <true if this is the last argument in list>, - 'is_required': <true if this arguement is required> """ self.log.warning("here inside get_methods") validation.oneof(profile, ('zapi', 'papi'), 'profile') if not self.methods.has_key(profile): # # Assemble in dictionaries, then sort into lists module_dict = {} for api_name in dir(self.app.api): api = getattr(self.app.api, api_name) self.log.warning("processing api: %s" % api.__class__.__name__) self.log.warning("api summary: %s" % api.__class__.__doc__) if hasattr(api, 'enable_zapi') and api.enable_zapi: module_dict[api_name] = { 'name': api_name, 'summary': api.__class__.__doc__, 'methods': {} } for attrname in dir(api): if attrname.startswith('xmlrpc_'): method = getattr(api, attrname) self.log.debug("method contents: %s" % method) self.log.debug("checking %s %s" % (api_name, attrname)) if hasattr(method, '_zapi_desc') and hasattr( method, '_zapi_args'): method_name = method.func_name.replace( 'xmlrpc_', '') temp_args = deepcopy(method._zapi_args) for arg in temp_args: self.log.debug("arg_type: %s" % pformat(arg['type'])) self.log.debug("API: %s" % api.__class__.__name__) self.log.debug("method: %s" % attrname) self.log.debug("arg: %s" % arg['name']) arg['type'] = self._clean_attr(arg['type']) info = { 'summary': method._zapi_desc, 'name': method_name, 'args': temp_args, 'is_last_arg': 0 } if info['args']: info['args'][-1]['is_last_arg'] = 1 module_dict[api_name]['methods'][ method_name] = info # # Now to sorted lists module_list = [] for module in module_dict.values(): if len(module['methods'].values()) == 0: continue methods = module['methods'].values() methods.sort(lambda a, b: cmp(a['name'], b['name'])) module['methods'] = methods module_list.append(module) module_list.sort(lambda a, b: cmp(a['name'], b['name'])) self.methods[profile] = module_list return self.methods[profile]