def _get_collection(self): accessor = models.Accessor(self.request.user) id = self._get_collection_id() try: return accessor.query_collections().get(id=id) except ObjectDoesNotExist: raise NotFound('No such collection: %r' % id)
def update(self, pk): audit_msg(self.request) form = forms.CollectionForm(self.data) if not form.is_valid(): raise BadRequest('Validation failure: %r' % form.errors) with transaction.atomic(): accessor = models.Accessor(self.request.user) try: coll = accessor.query_collections().get(id=pk) except ObjectDoesNotExist: raise NotFound('Invalid collection id = %r' % pk) coll.blogged = form.cleaned_data['blogged'] if form.cleaned_data['blogged']: coll.ensure_blog_exists() coll.blog.slug = form.cleaned_data['blog_slug'] coll.blog.description = form.cleaned_data['blog_desc'] coll.blog.save() else: models.BlogCollection.objects.filter(collection=coll).delete() coll.name = form.cleaned_data['name'] coll.save() return self._prepare(coll)
def delete(self, pk): audit_msg(self.request) accessor = models.Accessor(self.request.user) try: accessor.query_items(owner=True).get(id=pk).delete() except ObjectDoesNotExist: raise NotFound('No id = %r' % pk)
def detail(self, pk): audit_msg(self.request) accessor = models.Accessor(self.request.user) try: return self._prepare(accessor.query_items().get(id=pk)) except ObjectDoesNotExist: raise NotFound('No id = %r' % pk)
def delete(self, pk): audit_msg(self.request) accessor = models.Accessor(self.request.user) try: collection = accessor.query_collections(owner=True).get(id=pk) collection.delete() except ObjectDoesNotExist: raise NotFound('Invalid collection id = %r' % pk)
def update(self, pk): audit_msg(self.request) form = forms.ItemForm(self.data) if not form.is_valid(): raise BadRequest('Validation failure: %r' % form.errors) accessor = models.Accessor(self.request.user) try: item = accessor.query_items().get(id=pk) except ObjectDoesNotExist: raise NotFound('No id = %r' % pk) if 'collection_id' in self.data: cid = form.cleaned_data['collection_id'] if item.collection.encrypted: raise BadRequest('Cannot move from an encrypted journal') try: collection = accessor.query_collections().get(id=cid) except ObjectDoesNotExist: raise NotFound('No collection id = %r' % cid) if collection.encrypted: raise BadRequest('Cannot move to an encrypted journal') item.collection = collection if 'title' in self.data: item.title = form.cleaned_data['title'] if 'content' in self.data: item.content = form.cleaned_data['content'] if 'notes' in self.data: item.notes = form.cleaned_data['notes'] if 'typ' in self.data: item.typ = form.cleaned_data['typ'] if 'created_at' in self.data: month, day, year = [ int(x) for x in form.cleaned_data['created_at'].split('/') ] d = item.created_at item.created_at = d.replace(year=year, month=month, day=day) if 'visibility' in self.data: item.visibility = form.cleaned_data['visibility'] slug = slugify(item.title) if len(slug) > 50: slug = slug[:50] item.slug = slug item.save() return self._prepare(item)
def list(self): audit_msg(self.request) accessor = models.Accessor(self.request.user) if self._get_q(): q = self._get_q() query_set = accessor.query_items().filter( collection__encrypted=False).filter( Q(title__icontains=q) | Q(notes__icontains=q)) else: collection_id = self._get_collection_id() query_set = accessor.query_items().filter( collection__id=collection_id).order_by(self._get_sort_by()) paginator = Paginator(query_set, 8) page = self._get_page() # Note serialize_list below! return { 'num_pages': paginator.num_pages, 'objects': [self._prepare(x) for x in paginator.page(page)] }
def list(self): audit_msg(self.request) accessor = models.Accessor(self.request.user) return [self._prepare(x) for x in accessor.query_collections()]