def viewable_by(self, user): if (self.privacy == Privacy.PUBLIC or self.privacy == Privacy.URL_ONLY): return True viewer = Author.from_user(user) if viewer is None: return False if self.author == viewer: return True elif self.privacy == Privacy.FRIENDS: return self.author.friends_with(viewer) elif self.privacy == Privacy.FOAF: #TODO return False return False
def posts(request): if request.method == "POST": visibility_table = { "PRIVATE": Privacy.PRIVATE, #"PUBLIC": Privacy.URL_ONLY, "FRIENDS": Privacy.FRIENDS, "FOAF": Privacy.FOAF, "PUBLIC": Privacy.PUBLIC, } author = Author.from_user(request.user) if author is None: return JsonResponse({}, status=401) try: data = json.loads(request.body) title = data["title"] content = data["content"] content_type = data["contentType"] privacy = data["visibility"] if "visibility" in data else "PUBLIC" privacy = visibility_table[privacy] if "unlisted" in data and data["unlisted"]: privacy = Privacy.URL_ONLY except (json.decoder.JSONDecodeError, KeyError): return JsonResponse({}, status=400) post = Post.objects.create(date=datetime.date.today(), title=title, content=content, author=author, content_type=content_type, image=None, #TODO privacy=privacy) return redirect("api_post", post.pk) else: return render_posts( request, blog.models.Post.public(), api_reverse(request, "api_posts") )
def post(request, post_id): post = get_object_or_404(Post, pk=post_id) if request.method == "PUT": visibility_table = { "PRIVATE": Privacy.PRIVATE, #"PUBLIC": Privacy.URL_ONLY, "FRIENDS": Privacy.FRIENDS, "FOAF": Privacy.FOAF, "PUBLIC": Privacy.PUBLIC, } author = Author.from_user(request.user) if author is None or author != post.author: return JsonResponse({}, status=401) try: data = json.loads(request.body) except json.decoder.JSONDecodeError: return JsonResponse({}, status=400) if "title" in data: post.title = data["title"] if "content" in data: post.content = data["content"] if "contentType" in data: post.content_type = data["contentType"] if "visibility" in data: try: post.privacy = visibility_table[data["visibility"]] except KeyError: return JsonResponse({}, status=400) if "unlisted" in data and data["unlisted"]: post.privacy = Privacy.URL_ONLY post.save() return JsonResponse(serialize_post(request, post))
def test_from_user(self): self.assertEqual(self.author_A, Author.from_user(self.author_A.user)) user = User.objects.create_user(username="******", password="******") self.assertIs(None, Author.from_user(user))