def own_description_source(self, new_source): if not access_allowed(allow_authors, self): raise AccessError('Not an author') artwork_author = self._own_artwork_author post = Post(self.backend, artwork_author.description) new_post = post.edit(new_source) artwork_author.description = new_post._obj
def __get__(self, instance, owner): if instance: if not access_allowed(self.get_access, instance): raise AccessError('Cannot get %s' % self.column_name) return getattr(instance._obj, self.column_name) else: return self
def contacts(self): contacts = self._obj.contacts if not access_allowed(allow_self, self): # immutable contacts = dict(contacts) return NormalizedKeyDict( underlying_dict=contacts, normalizer=make_identifier)
def birthday(self): if not access_allowed(allow_self, self) and not self.show_birthday: return None dob = self._obj.date_of_birth if dob: return dob.month, dob.day else: return None
def avatar_request(self): if not access_allowed(allow_self, self): raise AccessError("Access denied") if self._obj.avatar_request: from fanart.backend.art import Artifact return Artifact(self.backend, self._obj.avatar_request) else: return None
def avatar(self): if not access_allowed(allow_self, self): raise AccessError("Access denied") if self._obj.avatar_request: self.avatar_request._schedule_removal() self._obj.avatar_request = None if self._obj.avatar: self.avatar._schedule_removal() self._obj.avatar = None
def authors(self): authors = self._obj.authors if access_allowed(allow_authors, self): authors = WrapList( authors, operator.attrgetter('_obj'), functools.partial(User, self.backend)) else: authors = tuple(User(self.backend, a) for a in authors) return authors
def contacts(self, new_value): if access_allowed(allow_self, self): existing = set(self._obj.contacts) new = set(new_value) for deleted_key in existing - new: del self._obj.contacts[deleted_key] self._obj.contacts.update(NormalizedKeyDict( underlying_dict=new_value, normalizer=make_identifier)) else: raise AccessError('Cannot set contacts')
def upload_avatar(self, input_file): if not access_allowed(allow_self, self): raise AccessError("Cannot upload another person's avatar") if self._obj.avatar_request: raise ValueError('Cannot upload another avatar request') from fanart.backend.art import Artifact, upload_artifact with upload_artifact(self.backend, input_file) as artifact: self._obj.avatar_request = artifact self.backend._db.flush() self.backend.schedule_task('apply_avatar', {'user_id': self.id}) return Artifact(self.backend, artifact)
def age(self): if not access_allowed(allow_self, self) and not self.show_age: return None now = datetime.now() dob = self._obj.date_of_birth if dob: age = now.year - dob.year if (now.month, now.day) < (dob.month, dob.day): age -= 1 return age else: return None
def remove(self, text, *, _flush=True): if not access_allowed(allow_owner, self): raise AccessError('Not the owner') identifier = make_identifier(text) try: kw = self._query.filter_by(text=text).one() except NoResultFound: raise LookupError(text) self.artwork._obj.artwork_user_keywords.remove(kw) self.backend._db.delete(kw) if _flush: self.backend._db.flush()
def add(self, text, *, _flush=True): if not access_allowed(allow_owner, self): raise AccessError('Not the owner') identifier = make_identifier(text) if text in self: raise ValueError('Duplicate value: {0!r}'.format(identifier)) keyword = tables.ArtworkUserKeyword( user=self.user._obj, artwork=self.artwork._obj, text=text, identifier=identifier) self.artwork._obj.artwork_user_keywords.append(keyword) if _flush: self.backend._db.flush()
def add(self, name=None): if not access_allowed(allow_logged_in, self): raise AccessError('Cannot add art') db = self.backend._db now = datetime.utcnow() item = self.item_table( name=name or '', created_at=now, added_at=now, ) db.add(item) db.flush() user = self.backend.logged_in_user if not user.is_virtual: item.authors.append(user._obj) return self.item_class(self.backend, item)
def add(self, source, *, recipient=None): if not access_allowed(allow_logged_in, self): raise AccessError('Cannot add news item') db = self.backend._db sender = self.backend.logged_in_user if sender.is_virtual: sender_obj = None else: sender_obj = sender._obj item = self.item_table( source=source, sender=sender_obj, recipient=recipient._obj if recipient else None, published_at=datetime.utcnow(), ) db.add(item) db.flush() return self.item_class(self.backend, item)
def add(self, heading, source): if not access_allowed(allow_logged_in, self): raise AccessError('Cannot add news item') db = self.backend._db reporter = self.backend.logged_in_user if reporter.is_virtual: reporter_obj = None else: reporter_obj = reporter._obj post = self.backend.posts.add(source) item = self.item_table( heading=heading, post=post._obj, reporter=reporter_obj, published_at=datetime.utcnow(), ) db.add(item) db.flush() return self.item_class(self.backend, item)
def add(self, source): if not access_allowed(allow_logged_in, self): raise AccessError('Cannot add post') poster = self.backend.logged_in_user db = self.backend._db if poster.is_virtual: poster_obj = None else: poster_obj = poster._obj time = datetime.utcnow() item = self.item_table( poster=poster_obj, posted_at=time, ) db.add(item) post = self.item_class(self.backend, item) post.edit(source, _time=time) return post
def bio(self, new_bio): if not access_allowed(allow_self, self): raise AccessError('Not allowed') new_post = self.bio_post.edit(new_bio) if not new_post.is_virtual: self._obj.bio_post = new_post._obj
def authors(self, new_value): if access_allowed(allow_authors, self): self.authors[:] = new_value else: raise AccessError('Cannot set authors')
def __set__(self, instance, value): if not access_allowed(self.set_access, instance): raise AccessError('Cannot set %s' % self.column_name) if self.check and not self.check(instance, value): raise ValueError('Cannot set %s to %s' % (self.column_name, value)) setattr(instance._obj, self.column_name, value)