Esempio n. 1
0
 def create_tags(self, tags, post_id):
     post_tags = ''
     if tags is str:
         tags = tags.split(',')
     tags = set(tags)
     for tag in tags:
         tag = tag.strip()
         if not tag:
             continue
         the_tag = self.get_tag_by_title(tag)
         if not the_tag:
             the_tag = Tag()
             the_tag.title = tag
             the_tag.slug = tag
             the_tag.post_count = 1
             db.session.add(the_tag)
             db.session.commit()
         else:
             the_tag.post_count += 1
         if not the_tag.post_ids:
             the_tag.post_ids = '%s' % post_id
         else:
             ids = the_tag.post_ids.split('|')
             ids.append(str(post_id))
             ids = list(set(ids))
             the_tag.post_ids = '|'.join(ids)
         db.session.add(the_tag)
         if post_tags == '':
             post_tags = '%s' % the_tag.id
         else:
             ids = post_tags.split('|')
             ids.append(str(the_tag.id))
             ids = list(set(ids))
             post_tags = '|'.join(ids)
     db.session.commit()
     return post_tags
Esempio n. 2
0
    def post(self):
        json = {}
        if not self.current_user:
            json = {
                'error': 1,
                'msg': self._('Access denied')
            }
            self.write(json)
            return

        title = self.get_argument('title', None)
        slug = self.get_argument('slug', None)
        # valid arguments
        if not title:
            json = {
                'error': 1,
                'msg': self._('Title field can not be empty')
            }
            self.write(json)
            return
        elif self.get_tag_by_title(title):
            json = {
                'error': 1,
                'msg': self._('Tag already exists')
            }
            self.write(json)
            return
        if not slug:
            json = {
                'error': 1,
                'msg': self._('Slug field can not be empty')
            }
            self.write(json)
            return
        elif self.get_tag_by_slug(slug):
            json = {
                'error': 1,
                'msg': self._('Slug already exists')
            }
            self.write(json)
            return
        # create tag
        tag = Tag()
        tag.title = title
        tag.slug = slug
        self.db.add(tag)
        self.db.commit()
        # delete cache
        keys = ['TagCloud', 'SystemStatus']
        self.cache.delete_multi(keys)

        json = {
            'error': 0,
            'msg': self._('Successfully created'),
            'tag': {
                'id': tag.id,
                'title': tag.title,
                'slug': tag.slug,
                'permalink': tag.permalink,
                'post_count': tag.post_count
            }
        }
        self.write(json)