def setUp(self) -> None: super().setUp() self.tag = Tag(name='new tag') self.tag.save() self.post = Post(title='admin', body='*****@*****.**', author=self.current_user) self.post.save() self.post.tags.add(self.tag) self.post.save()
def setUpClass(cls): super().setUpClass() session = cls.Session() cls.username = '******' cls.password = '******' user = User(username=cls.username, password=str( bcrypt.hashpw(cls.password.encode('utf-8'), bcrypt.gensalt()), 'utf-8'), avatar='Katarina.png', introduction='L') article = Article( user=user, title='L', introduction='L', markdown_content='L', compiled_content='L', ) tag = Tag(content='hello') article.tag.append(tag) user.tag.append(tag) session.add(user) session.add(article) fish_record = Record( title='hello', content=['hello'], image_num=1, source='hello', ) session.add(fish_record) session.commit() session.close()
def handle(): randomize.out("\ntags:") for i in range(50): Tag(name=randomize.string(20)).save() randomize.out('.') print "\nusers:", for i in range(5): user, created = User.objects.get_or_create( username='******' % i, email='*****@*****.**' % str(i), password='******', is_blog_author=True, first_name=u'Иван%s' % str(i), second_name=u'Иванович%s' % str(i), last_name=u'Иванов%s' % str(i), ) if created: user.save() print "\nposts" for j in range(50): post = Post( author=user, title='Post%s' % str(i), text='Post text %s' % str(i), published=True, enable_comments=True, ) post.save() if randomize.boolean(): post.tags = Tag.objects.order_by('?')[:randomize.integer(1, 6)] randomize.out('.') randomize.out('.')
class PostAPITests(LoggedUserAPITestCase): def setUp(self) -> None: super().setUp() self.tag = Tag(name='new tag') self.tag.save() self.post = Post(title='admin', body='*****@*****.**', author=self.current_user) self.post.save() self.post.tags.add(self.tag) self.post.save() def tearDown(self) -> None: super().tearDown() self.tag.delete() self.post.delete() def get_posts(self, **kwargs): url = reverse_lazy('post-list') return self.client.get(url, kwargs, format='json') def get_post(self, slug, **kwargs): url = reverse_lazy('post-detail', kwargs={'slug': slug}) return self.client.get(url, kwargs, format='json') def post_new_post(self, title, body, tags): url = reverse_lazy('post-list') data = {"title": title, "body": body, "tags": tags} response = self.client.post(url, data, format='json') return response def test_post_new_post_with_valid_data(self): response = self.post_new_post('post title', 'post body', [self.tag.pk]) assert response.status_code == status.HTTP_201_CREATED def test_get_posts(self): response = self.get_posts() assert response.status_code == status.HTTP_200_OK def test_get_post_with_valid_slug(self): response = self.get_post(self.post.slug) assert response.status_code == status.HTTP_200_OK def test_get_post_with_fake_slug(self): response = self.get_post('fake-slug') # fake slug assert response.status_code == status.HTTP_404_NOT_FOUND
class TagAPITests(LoggedUserAPITestCase): def setUp(self) -> None: super(TagAPITests, self).setUp() self.tag = Tag(name='new tag') self.tag.save() def tearDown(self) -> None: super(TagAPITests, self).tearDown() self.tag.delete() def get_tags(self, **kwargs): url = reverse_lazy('tag-list') return self.client.get(url, kwargs, format='json') def get_tag(self, pk, **kwargs): url = reverse_lazy('tag-detail', kwargs={'pk': pk}) return self.client.get(url, kwargs, format='json') def post_tag(self, name): url = reverse_lazy('tag-list') data = {"name": name} response = self.client.post(url, data, format='json') return response def test_post_user_with_valid_data(self): response = self.post_tag('new tag name') assert response.status_code == status.HTTP_201_CREATED def test_post_user_with_duplicated_data(self): response = self.post_tag('new tag') assert response.status_code == status.HTTP_400_BAD_REQUEST def test_get_users(self): response = self.get_tags() assert response.status_code == status.HTTP_200_OK def test_get_user_with_valid_pk(self): response = self.get_tag(self.tag.pk) assert response.status_code == status.HTTP_200_OK def test_get_user_with_fake_pk(self): response = self.get_tag(111) # fake pk assert response.status_code == status.HTTP_404_NOT_FOUND
def mutate_and_get_payload(cls, input, info): try: tag = Tag() tag.name = input.get('name') tag.full_clean() tag.save() return CreateTag(tag=tag) except ValidationError as e: return CreateTag(tag=None, errors=getErrors(e))
def setUp(self) -> None: super(TagAPITests, self).setUp() self.tag = Tag(name='new tag') self.tag.save()
def entry_create(request, slug=None): if slug is not None: entry = Entry.all().filter('slug =', slug).get() action_url = reverse('entry_edit', args=[slug]) initial = {'title': entry.title, 'content': entry.content, 'tags': "\n".join(tag.name() for tag in entry.tags), 'status': entry.status} else: entry = None action_url = reverse('entry_create') initial = None if request.method == 'POST': form = EntryForm(request.POST, initial) if form.is_valid(): if not initial: tags = [] for tagtitle in form.cleaned_data['tags']: tag = Tag.inc_or_insert(tagtitle) tags.append(tag.key()) form.cleaned_data['tags'] = tags entry = Entry(**form.cleaned_data) entry.put() messages.success(request, 'Post created successfully') else: initial_tags = [tag.name() for tag in entry.tags] new_tags = form.cleaned_data['tags'] new = set(new_tags) - set(initial_tags) existing = set(new_tags) & set(initial_tags) deleted = set(initial_tags) - set(new_tags) tags = [] for tagtitle in deleted: Tag.try_dec(tagtitle) for tagtitle in new_tags: if tagtitle in new: tag = Tag.inc_or_insert(tagtitle) tags.append(tag.key()) elif tagtitle in existing: tag = Tag.get_by_key_name(tagtitle) tags.append(tag.key()) else: raise Exception("what?") entry.tags = tags entry.title = form.cleaned_data['title'] entry.status = form.cleaned_data['status'] entry.content = form.cleaned_data['content'] entry.put() messages.success(request, 'Post modified successfully') if entry.status == 'draft': msg = ('<strong>Warning</strong> The post was saved as a ' 'draft and will not be visisble from the homepage. ' 'Click <a href="%s">here</a> to view all your posts.' % reverse('all_index')) messages.warning(request, mark_safe(msg)) return redirect('entry_view', entry.slug) else: form = EntryForm(initial) return render_to_response('blog/create.html', { 'form': form, 'action_url': action_url }, context_instance=RequestContext(request))
def get_query(self): tag = Tag.get_by_key_name(self.kwargs['tag']) return Entry.all().filter('status =', 'published') \ .filter('tags =', tag.key()) \ .order('-published_at')
def get_context_data(self, **kwargs): context = super(EntriesView, self).get_context_data(**kwargs) entries = self.get_page(self.get_query()) context['entries'] = entries context['cloud'] = Tag.cloud() return context