def remove(self, lst, *, user): """See :http:delete:`/users/(id)/lists/(list-id)`. If *lst* is not in the collection, a :exc:`micro.error.ValueError` is raised. """ if user != getattr(self, 'user'): raise PermissionError() if lst.authors[0] == getattr(self, 'user'): raise micro.ValueError( 'user {} is owner of lst {}'.format(getattr(self, 'user').id, lst.id)) if self.app.r.zrem(self.ids.key, lst.id) == 0: raise micro.ValueError( 'No lst {} in lists of user {}'.format(lst.id, getattr(self, 'user').id))
def post(self, id): args = self.check_args({'item_id': str, 'to_id': (str, None)}) meeting = self.app.meetings[id] try: args['item'] = meeting.items[args.pop('item_id')] except KeyError: raise micro.ValueError('item_not_found') args['to'] = args.pop('to_id') if args['to'] is not None: try: args['to'] = meeting.items[args['to']] except KeyError: raise micro.ValueError('to_not_found') meeting.move_agenda_item(**args) self.write(json.dumps(None))
async def _create(self, title, *, text=None, resource=None, location=None): # pylint: disable=protected-access; List is a friend self.host[0]._check_permission(self.app.user, 'list-modify') attrs = await WithContent.process_attrs( { 'text': text, 'resource': resource }, app=self.app) if str_or_none(title) is None: raise micro.ValueError('title_empty') item = Item(id='Item:{}'.format(randstr()), app=self.app, authors=[self.app.user.id], trashed=False, text=attrs['text'], resource=attrs['resource'], list_id=self.host[0].id, title=title, location=location.json() if location else None, checked=False) self.app.r.oset(item.id, item) self.app.r.rpush(self.map_key, item.id) self.host[0].activity.publish( Event.create('list-create-item', self.host[0], {'item': item}, self.app)) return item
def do_edit(self, **attrs): self._check_permission(self.app.user, 'list-modify') if 'title' in attrs and str_or_none(attrs['title']) is None: raise micro.ValueError('title_empty') if ('features' in attrs and not set(attrs['features']) <= {'check', 'assign', 'vote', 'location', 'play'}): raise micro.ValueError('feature_unknown') if 'mode' in attrs and attrs['mode'] not in {'collaborate', 'view'}: raise micro.ValueError('Unknown mode') if 'title' in attrs: self.title = attrs['title'] if 'description' in attrs: self.description = str_or_none(attrs['description']) if 'features' in attrs: self.features = attrs['features'] if 'mode' in attrs: self.mode = attrs['mode']
def post(self, id): args = self.check_args({'list_id': str}) list_id = args.pop('list_id') try: args['lst'] = self.app.lists[list_id] except KeyError: raise micro.ValueError('No list {}'.format(list_id)) lists = self.get_collection(id) lists.add(**args, user=self.current_user) self.write({})
def post(self, id): args = self.check_args({'item_id': str}) meeting = self.app.meetings[id] try: args['item'] = meeting.trashed_items[args.pop('item_id')] except KeyError: raise micro.ValueError('item_not_found') meeting.restore_agenda_item(**args) self.write(json.dumps(None))
async def do_edit(self, **attrs): self._check_permission(self.app.user, 'item-modify') attrs = await WithContent.pre_edit(self, attrs) if 'title' in attrs and str_or_none(attrs['title']) is None: raise micro.ValueError('title_empty') WithContent.do_edit(self, **attrs) if 'title' in attrs: self.title = attrs['title'] if 'location' in attrs: self.location = attrs['location']
async def post(self, list_id, id): item = self.app.lists[list_id].items[id] args = self.check_args({ 'text': (str, None, 'opt'), 'resource': (str, None, 'opt'), 'title': (str, 'opt'), 'location': (dict, None, 'opt') }) if args.get('location') is not None: try: args['location'] = Location.parse(args['location']) except TypeError: raise micro.ValueError('bad_location_type') await item.edit(asynchronous=ON, **args) self.write(item.json(restricted=True, include=True))
async def _create_example(self, use_case): if use_case not in _EXAMPLE_DATA: raise micro.ValueError('use_case_unknown') data = _EXAMPLE_DATA[use_case] description = ( '{}\n\n*This example was created just for you, so please feel free to play around.*' .format(data[1])) lst = self.create(use_case, v=2) lst.edit(title=data[0], description=description) for item in data[2]: args = dict(item) checked = args.pop('checked', False) item = await lst.items.create(asynchronous=ON, **args) if checked: item.check() return lst
def create(self, use_case='simple', *, v=2): """See :http:post:`/api/lists`.""" # pylint: disable=unused-argument; former feature toggle # Compatibility for endpoint version (deprecated since 0.22.0) if not self.app.user: raise PermissionError() if use_case not in _USE_CASES: raise micro.ValueError('use_case_unknown') data = _USE_CASES[use_case] id = 'List:{}'.format(randstr()) lst = List( id=id, app=self.app, authors=[self.app.user.id], title=data['title'], description=None, features=data['features'], mode=data.get('mode', 'collaborate'), activity=Activity('{}.activity'.format(id), self.app, subscriber_ids=[])) self.app.r.oset(lst.id, lst) self.app.r.zadd('{}.users'.format(lst.id), {self.app.user.id.encode(): -time()}) self.app.r.rpush(self.map_key, lst.id) self.app.user.lists.add(lst, user=self.app.user) self.app.activity.publish( Event.create('create-list', None, {'lst': lst}, app=self.app)) return lst
def create(self, use_case=None, description=None, title=None, v=1): """See :http:post:`/api/lists`.""" if v == 1: # create(title, description=None) title = title or use_case if title is None: raise TypeError() lst = self.create('simple', v=2) lst.edit(title=title, description=description) return lst if v == 2: # create(use_case='simple') use_case = use_case or 'simple' else: raise NotImplementedError() if not self.app.user: raise PermissionError() if use_case not in _USE_CASES: raise micro.ValueError('use_case_unknown') data = _USE_CASES[use_case] id = 'List:{}'.format(randstr()) lst = List(id=id, app=self.app, authors=[self.app.user.id], title=data['title'], description=None, features=data['features'], mode='collaborate', activity=Activity('{}.activity'.format(id), self.app, subscriber_ids=[])) self.app.r.oset(lst.id, lst) self.app.r.rpush(self.map_key, lst.id) self.app.user.lists.add(lst, user=self.app.user) self.app.activity.publish( Event.create('create-list', None, {'lst': lst}, app=self.app)) return lst
async def create_example(self, use_case): """See :http:post:`/api/lists/create-example`.""" if use_case not in _EXAMPLE_DATA: raise micro.ValueError('use_case_unknown') data = _EXAMPLE_DATA[use_case] description = ( '{}\n\n*This example was created just for you, so please feel free to play around.*' .format(data[1])) lst = self.create(use_case, v=2) lst.edit(title=data[0], description=description) for item in data[2]: args = dict(item) checked = args.pop('checked', False) user_assigned = args.pop('user_assigned', False) user_voted = args.pop('user_voted', False) item = await lst.items.create(**args) if checked: item.check() if user_assigned: item.assignees.assign(self.app.user, user=self.app.user) if user_voted: item.votes.vote(user=self.app.user) return lst
def _check_feature(user, feature, item): if feature not in item.list.features: raise micro.ValueError('feature_disabled') if not user: raise PermissionError()