def __init__(self, user=None, user_id=None, media=None, media_id=None, publish_uri=None, revoke_task_id=None, send_email=False, _id=None, statistic=None, status=TaskModel.UNKNOWN): super(PublisherTask, self).__init__(_id, statistic, status) self.user = dict2object(User, user, inspect_constructor=True) if isinstance(user, dict) else user if user is None: # User attribute overrides user_id self.user_id = user_id del self.user self.media = dict2object(Media, media, inspect_constructor=True) if isinstance(media, dict) else media if media is None: # Media attribute overrides media_id self.media_id = media_id del self.media self.publish_uri = publish_uri self.revoke_task_id = revoke_task_id self.send_email = send_email
def get_user(self, spec, fields=None, secret=None): self.only_standalone() entity = self._db.users.find_one(spec, fields) if not entity: return None user = dict2object(User, entity, inspect_constructor=True) return user if secret is None or user.verify_secret(secret) else None
def list(self, head=False, **data): values = [] response_dict = self.api_client.do_request(get, self.get_url(extra=(u'HEAD' if head else None)), data=object2json(data, include_properties=False)) if self.cls is None: return response_dict for value_dict in response_dict: values.append(dict2object(self.cls, value_dict, inspect_constructor=True)) return values
def get_transform_profiles(self, spec=None, fields=None, skip=0, limit=0, sort=None): profiles, sort = [], sort or [("encoder_name", 1), ("title", 1)] # Sort by default, this is nicer like that ! for entity in list( self._db.transform_profiles.find( spec=spec, fields=fields, skip=int(skip), limit=int(limit), sort=sort, **self.db_find_options ) ): profiles.append(dict2object(TransformProfile, entity, inspect_constructor=True)) return profiles
def __init__(self, user=None, user_id=None, parent=None, parent_id=None, uri=None, public_uris=None, filename=None, metadata=None, status=PENDING, _id=None): super(Media, self).__init__(_id) self.user = dict2object(User, user, inspect_constructor=True) if isinstance(user, dict) else user if user is None: # User attribute overrides user_id self.user_id = user_id del self.user self.parent = dict2object(Media, parent, inspect_constructor=True) if isinstance(parent, dict) else parent if parent is None: # Parent attribute overrides parent_id self.parent_id = parent_id del self.parent self.uri = uri self.public_uris = public_uris or {} try: self.filename = unicode(filename).replace(u' ', u'_') except: self.filename = None self.metadata = metadata or {} self.status = status
def get_medias(self, spec=None, fields=None, skip=0, limit=0, sort=None, load_fields=False): medias, sort = [], sort or [('metadata.title', 1)] # Sort by default, this is nicer like that ! for entity in list(self._db.medias.find(spec=spec, fields=fields, skip=int(skip), limit=int(limit), sort=sort, **self.db_find_options)): media = dict2object(Media, entity, inspect_constructor=True) if load_fields: media.load_fields(self.get_user({u'_id': media.user_id}, {u'secret': 0}), self.get_media({u'_id': media.parent_id})) medias.append(media) return medias
def get_users(self, spec=None, fields=None, skip=0, limit=0, sort=None): self.only_standalone() if fields is not None: fields[u'secret'] = 0 # Disable access to users secret ! users, sort = [], sort or [(u'last_name', 1), (u'first_name', 1)] # Sort by default, this is nicer like that ! for entity in list(self._db.users.find(spec=spec, fields=fields, skip=int(skip), limit=int(limit), sort=sort, **self.db_find_options)): user = dict2object(User, entity, inspect_constructor=True) users.append(user) return users
def get_publisher_task(self, spec, fields=None, load_fields=False, append_result=True): entity = self._db.publisher_tasks.find_one(spec, fields) if not entity: return None task = dict2object(PublisherTask, entity, inspect_constructor=True) if load_fields: task.load_fields(self.get_user({u'_id': task.user_id}, {u'secret': 0}), self.get_media({u'_id': task.media_id})) if append_result: task.append_async_result() return task
def __init__(self, user=None, user_id=None, media_in=None, media_in_id=None, media_out=None, media_out_id=None, profile=None, profile_id=None, send_email=False, _id=None, statistic=None, status=TaskModel.UNKNOWN): super(TransformTask, self).__init__(_id, statistic, status) self.user = dict2object(User, user, inspect_constructor=True) if isinstance(user, dict) else user if user is None: # User attribute overrides user_id self.user_id = user_id del self.user self.media_in = dict2object(Media, media_in, True) if isinstance(media_in, dict) else media_in if media_in is None: # Media_in attribute overrides media_in_id self.media_in_id = media_in_id del self.media_in self.media_out = dict2object(Media, media_out, True) if isinstance(media_out, dict) else media_out if media_out is None: # Media_out attribute overrides media_out_id self.media_out_id = media_out_id del self.media_out self.profile = dict2object(TransformProfile, profile, True) if isinstance(profile, dict) else profile if profile is None: # Profile attribute overrides profile_id self.profile_id = profile_id del self.profile self.send_email = send_email
def add(self, *args, **kwargs): if not(bool(args) ^ bool(kwargs)): raise ValueError(to_bytes(u'You must set args OR kwargs.')) if args and len(args) != 1: raise ValueError(to_bytes(u'args should contain only 1 value.')) value = args[0] if args else kwargs response = self.api_client.do_request(post, self.get_url(), data=object2json(value, include_properties=False)) instance = dict2object(self.cls, response, inspect_constructor=True) if self.cls else response # Recover user's secret if isinstance(instance, User): instance.secret = value.secret if args else kwargs[u'secret'] return instance
def get_publisher_tasks(self, spec=None, fields=None, skip=0, limit=0, sort=None, load_fields=False, append_result=True): tasks, sort = [], sort or [('statistic.add_date', -1)] # Sort by default, this is nicer like that ! for entity in list(self._db.publisher_tasks.find(spec=spec, fields=fields, skip=int(skip), limit=int(limit), sort=sort, **self.db_find_options)): task = dict2object(PublisherTask, entity, inspect_constructor=True) if load_fields: task.load_fields(self.get_user({u'_id': task.user_id}, {u'secret': 0}), self.get_media({u'_id': task.media_id})) if append_result: task.append_async_result() tasks.append(task) return tasks
def get_media(self, spec, fields=None, load_fields=False): entity = self._db.medias.find_one(spec, fields) if not entity: return None media = dict2object(Media, entity, inspect_constructor=True) if load_fields: media.load_fields(self.get_user({u'_id': media.user_id}, {u'secret': 0}), self.get_media({u'_id': media.parent_id})) if not self.config.is_standalone: # Add read path to the media asset media.api_uri = self.config.storage_medias_path(media, generate=False) return media
def login(self, user_or_mail, secret=None, update_auth=True): if isinstance(user_or_mail, User): auth = user_or_mail.credentials elif secret is not None: auth = (user_or_mail, secret) else: raise ValueError(to_bytes(u'User_or_mail is neither a valid instance of User nor a mail with a secret ' 'following.')) user_dict = self.do_request(get, u'{0}/user/login'.format(self.api_url), auth) user = dict2object(User, user_dict, inspect_constructor=True) if update_auth: # Recover user's secret user.secret = auth[1] self.auth = user return user
def get_transform_task(self, spec, fields=None, load_fields=False, append_result=True): entity = self._db.transform_tasks.find_one(spec, fields) if not entity: return None task = dict2object(TransformTask, entity, inspect_constructor=True) if load_fields: task.load_fields( self.get_user({"_id": task.user_id}, {"secret": 0}), self.get_media({"_id": task.media_in_id}), self.get_media({"_id": task.media_out_id}), self.get_transform_profile({"_id": task.profile_id}), ) if append_result: task.append_async_result() return task
def get_transform_tasks( self, spec=None, fields=None, skip=0, limit=0, sort=None, load_fields=False, append_result=True ): tasks, sort = [], sort or [("statistic.add_date", -1)] # Sort by default, this is nicer like that ! for entity in list( self._db.transform_tasks.find( spec=spec, fields=fields, skip=int(skip), limit=int(limit), sort=sort, **self.db_find_options ) ): task = dict2object(TransformTask, entity, inspect_constructor=True) if load_fields: task.load_fields( self.get_user({"_id": task.user_id}, {"secret": 0}), self.get_media({"_id": task.media_in_id}), self.get_media({"_id": task.media_out_id}), self.get_transform_profile({"_id": task.profile_id}), ) if append_result: task.append_async_result() tasks.append(task) return tasks
def get_unit_local_config(self, service, number, cls=None, local_config=LOCAL_CONFIG_FILENAME): u"""Return an instance of ``cls`` with the content of the local configuration of an instance of a charm !""" config_dict = juju_do(u'ssh', environment=self.environment, options=[u'{0}/{1}'.format(service, number), u'sudo cat {0}'.format(get_unit_path(service, number, local_config))]) return dict2object(cls, config_dict, inspect_constructor=False) if cls else config_dict
def __getitem__(self, index): response_dict = self.api_client.do_request(get, self.get_url(index)) return response_dict if self.cls is None else dict2object(self.cls, response_dict, inspect_constructor=True)
def get_transform_profile(self, spec, fields=None): entity = self._db.transform_profiles.find_one(spec, fields) if not entity: return None return dict2object(TransformProfile, entity, inspect_constructor=True)