예제 #1
0
    def create(self, validated_attrs):
        nutritioninfo = validated_attrs.pop('nutrition')
        taginfo = validated_attrs.pop('tags')
        # create ingredient
        ingre = Ingredient.objects.create(**validated_attrs)

        # create nutrition value
        for item in nutritioninfo:
            ingre.nutrition.add(NutritionValue.objects.create(**item))

        # create tags
        tag_list = taginfo.split(',')
        for tag in tag_list:
            if tag:
                try:
                    old_tag = Tag.objects.get(name=tag)
                    ingre.tags.add(old_tag)
                except Tag.DoesNotExist:
                    new_tag = Tag(name=tag)
                    new_tag.save()
                    ingre.tags.add(new_tag)

        ingre.save()

        return ingre
예제 #2
0
    def create(self, validated_attrs):
        nutritioninfo = validated_attrs.pop('nutrition')
        taginfo = validated_attrs.pop('tags')
        # create ingredient
        ingre = Ingredient.objects.create(**validated_attrs)

        # create nutrition value
        for item in nutritioninfo:
            ingre.nutrition.add(NutritionValue.objects.create(**item))

        # create tags
        tag_list = taginfo.split(',')
        for tag in tag_list:
            if tag:
                try:
                    old_tag = Tag.objects.get(name=tag)
                    ingre.tags.add(old_tag)
                except Tag.DoesNotExist:
                    new_tag = Tag(name=tag)
                    new_tag.save()
                    ingre.tags.add(new_tag)

        ingre.save()

        return ingre
예제 #3
0
def get_tag_id(tag):
    try:
        tag_id = Tag.objects.filter(tag=tag).get().id
    except:
        new_tag = Tag(tag=tag)
        new_tag.save()
        tag_id = Tag.objects.filter(tag=tag).get().id
    return tag_id
예제 #4
0
def tags_handle(new, old=''):
    '''
    处理文章标签
    :param new: 新提交的标签字符串
    :param old: 旧标签字符串,为文章更新提交之前的
    :return:
    '''
    objs = []
    new = set([tag for tag in new.split(',') if tag.strip()])  # 新提交的标签字符串
    old = set([tag for tag in old.split(',')
               if tag.strip()])  # 旧标签字符串,一般为更新之前的字符串
    els = list(old - new)  #获取差积删除

    # 创建Tag表数据对象
    for name in list(new):
        # 查询数据
        tag = Tag.query.filter(Tag.name == name).first()
        if tag is None: tag = Tag(name=name)
        objs.append(tag)

    # 删除无文章引用标签
    for name in els:
        tag = Tag.query.filter_by(name=name).first()
        if len(tag.posts) <= 1: db.session.delete(tag)

    return objs
 def _append_model(self, tag):
     log_of_current_tag = self._tag_log.get(tag.name, {'count': 0})
     log_of_current_tag['count'] += 1
     self._tag_log[tag.name] = log_of_current_tag
     model = self.vector_model(url=self.domain_url_model,
                               tag=Tag.find_by_tag_with_out_fail(tag.name),
                               tag_order=log_of_current_tag['count'],
                               depth=self.count_depth(tag))
     self.append_vector_model_set(model)
예제 #6
0
파일: post.py 프로젝트: mina998/zblog
def post_add():
    form = PostForm()
    form.cid.choices = [(v.id, v.title) for v in db.session.query(Classify.id, Classify.title).all()]
    form.sid.choices = [(v.id, v.name) for v in Style.query.all()]
    form.author.data = session.get('nikename')
    # 表单是否验证成功
    if form.validate_on_submit():
        form.tags.data = tags_handle(form.tags.data)
        form.content.data = content_file_handle(form.content.data)
        post = field_obj_set(Post(),form.data)
        db.session.add(post)
        db.session.commit()
        flash('添加成功!', category='ok')
        return redirect(url_for('admin.post_add'))
    title = '新建文章'
    data = dict(title=title, tags=Tag.tags(), form=form)
    return render_template('admin/post.form.html', **data)
예제 #7
0
 def _append_model(self, tag):
     log_of_current_tag = self._tag_log.get(tag.name, {'count': 0})
     log_of_current_tag['count'] += 1
     self._tag_log[tag.name] = log_of_current_tag
     word_list = tag.string.split()
     for index, word in enumerate(word_list):
         try:
             language = detect(word)
         except LangDetectException:
             language = 'en'
         model = self.vector_model(
             url=self.domain_url_model,
             tag=Tag.find_by_tag_with_out_fail(tag.name),
             tag_order=log_of_current_tag['count'],
             lang=LangMst.find_by_language_with_out_fail(language),
             word=Word.find_by_word_with_out_fail(word),
             word_order=index + 1,
             depth=self.count_depth(tag))
         self.append_vector_model_set(model)
예제 #8
0
파일: post.py 프로젝트: mina998/zblog
def post_edit(id):
    post = Post.query.get(id)
    #转成字符串
    tags =  ','.join([tag.name for tag in post.tags])
    #转成表单默认数据
    data = to_dict(PostForm, post)
    data['tags'] = tags
    form = PostForm(data=data)
    form.cid.choices = [(v.id, v.title) for v in db.session.query(Classify.id, Classify.title).all()]
    form.sid.choices = [(v.id, v.name) for v in Style.query.all()]
    # 表单是否验证成功
    if form.validate_on_submit():
        form.content.data =content_file_handle(form.content.data, old_content=post.content)
        form.tags.data = tags_handle(form.tags.data,old=tags)
        post = field_obj_set(post,form.data)
        db.session.add(post)
        db.session.commit()
        db.session.close()
        flash('更新成功!', category='ok')
        return redirect(url_for('admin.post'))
    title = '更新文章'

    data = dict(title=title, form=form, tags=Tag.tags(), id=id)
    return render_template('admin/post.form.html', **data)
예제 #9
0
 def create_bulk(self, k):
     Tag.objects.bulk_create([Tag(name=name) for name in self.names(k)])
예제 #10
0
파일: views.py 프로젝트: Lovzi/conan
 def get_context_data(self, **kwargs):
     p = Tag(name='d')
     p.save()
     print(p.id)
     return super().get_context_data()