def setUp(self): """ Contrived set of entries and tags in order to test most of the list functionality""" self.t1 = models.Tag(name='Tag 1', is_permanent=True) self.t1.save() self.t2 = models.Tag(name='Tag 2', is_permanent=True) self.t2.save() self.t3 = models.Tag(name='Tag 3', is_permanent=True) self.t3.save() self.t4 = models.Tag(name='Tag 4', is_permanent=True) self.t4.save() self.e1 = models.Entry(title="Entry 1") self.e1.save() self.e1.tags = [self.t1, self.t2, self.t3, self.t4] self.e2 = models.Entry(title="Entry 2") self.e2.save() self.e2.tags = [self.t2, self.t4] self.e3 = models.Entry(title="Entry 3") self.e3.save() self.e3.tags = [self.t3] self.e4 = models.Entry(title="Entry 4") self.e4.save() self.e4.tags = [self.t4] self.client = Client()
def test_create_post_with_tag_and_other_post(self): _user = self.users[2] tag_name = '_tag' tag = models.Tag() tag.user = _user tag.name = tag_name tag.save() data = { 'user': _user, 'title': '_title', 'text': '_text', 'is_public': True, 'description': '_description', 'keywords': '_post_keyword', } first_post = self.__create_and_save(data) data = { 'user': _user, 'title': '_title', 'text': '_text', 'tags': [tag.pk], 'relation_posts': [first_post.pk], 'is_public': True, 'description': '_description', 'keywords': '_post_keyword', } post = self.__create_and_save(data) self.assertEqual(self.model.objects.filter(user=_user).count(), 2) expected = {key: value for key, value in data.items()} expected['tags'] = 1 expected['relation_posts'] = 1 self.__chk_instance_data(post.pk, expected)
def post(self, request, *args, **kwargs): userinfo = request.session.get("userinfo") blog_id = userinfo["blog_id"] article_nid = request.POST.get("article_nid") article_title = request.POST.get("article_title") article_content = request.POST.get("article_content") article_type_id = request.POST.get("article_type_id") category_id = request.POST.get("category_id") tags = request.POST.get("tags") content_origin, content_text = xss.xss(article_content) article_info = { "title": article_title, "summary": content_text[:70], "blog_id": blog_id, "article_type_id": article_type_id, "category_id": category_id, } if article_nid: models.Article.objects.filter(nid=article_nid).update( **article_info) models.ArticleDetail.objects.filter(article_id=article_nid).update( content=content_origin) if (not article_nid ) and article_title and article_content: # 标题和内容必须存在 # 创建文章 art_obj = models.Article.objects.create(**article_info) # 创建文章详细内容 models.ArticleDetail.objects.create(content=content_origin, article=art_obj) if tags: tag_list = list(filter(lambda x: x, tags.split(","))) # 去除空字符 tag_models_list = [ item[0] for item in models.Tag.objects.filter( blog_id=blog_id).values_list("title") ] tag_create_set = set(tag_list).difference(tag_models_list) tag_obj_list = [] for item in tag_create_set: tag_obj_list.append(models.Tag(title=item, blog_id=blog_id)) models.Tag.objects.bulk_create(tag_obj_list) tag_objs = models.Tag.objects.filter(blog_id=blog_id, title__in=tag_list) # print(tag_objs) A2T_obj_list = [] for item in tag_objs: A2T_obj_list.append( models.Article2Tag(article=art_obj, tag=item)) x = models.Article2Tag.objects.bulk_create(A2T_obj_list) # print(x) return redirect("/backend/article.html")
def cms_add_tag(request): form = cf.CMSAddTagForm(request.POST,request) if not form.is_valid(): return xtjson.json_params_error(message=form.get_error()) else: name=form.cleaned_data.get('name') tag=bm.Tag(name=name) tag.save() return xtjson.json_result(message=u'标签添加成功!')
def create(self, request): name = request.POST.get('name') description = request.POST.get('description') if name and description: tag = models.Tag(name=name, description=description) tag.save() rc.CREATED else: rc.BAD_REQUEST
def set_up_reference_posts(self): tag1 = models.Tag(name="Winemaking") tag2 = models.Tag(name="Embedded") tag1.save() tag2.save() date1 = datetime.now(tz=timezone.utc) delta = timedelta(days=1) date2 = date1 - delta post1 = models.Post(title='Test Post about Wine', body="""I'm not an alcoholic""", pub_date=date1) post1.save() post1.tags.add(tag1.pk) post1.save() post2 = models.Post(title='Test Post about embedded control', body="""I sure do love reading manuals""", pub_date=date2) post2.save() post2.tags.add(tag2.pk) post2.save()
def saveTag(request): id = request.GET.get('id') name = request.GET.get('name') if name is None or name == '': return JsonResponse({'result': False, 'err': '调用方法缺少必要的参数!'}) if id is None or id == '': tag = models.Tag() tag.Name = name tag.save() else: tag = models.Tag.objects.get(Id=id) tag.Name = name tag.save() return JsonResponse({'result': True})
def post(self, request): new_tag = request.POST.get('new_tag') models.Tag(tag=new_tag).save() return Response('ok')