Exemplo n.º 1
0
    def post(self, request):
        data = json.loads(request.body)
        blogpost = self._get_post(data.get('id'), request.user)
        blogpost.title = data.get('title', '')
        blogpost.body = data.get('body', '')
        blogpost.published = data.get('published')
        posted_to = data.get('posted_to')
        if posted_to == 'organization':
            content_object = Organization.get_single_by_user(request.user)
        else:
            posted_to_id = int(data.get('posted_to_id'))
            content_object = Team.objects.get(pk=posted_to_id) # TODO: validate team
        blogpost.content_object = content_object
        blogpost.save()

        blogpost.tags.clear()

        tags = data.get('tags', [])
        for _tag in tags:
            slug = slugify(_tag.get('text', ''))
            try:
                tag = Tag.objects.get(slug=slug)
            except Tag.DoesNotExist:
                tag = Tag()
                tag.text =_tag.get('text', '')
                tag.save()
            blogpost.tags.add(tag)

        return json_response(blogpost.to_dict())
Exemplo n.º 2
0
def creaetBlog():
    blog = Blog()
    blog.title = request.json['title']
    blog.content = request.json['content']
    tags_json = request.json['tags']
    if isinstance(tags_json, list):
        tags = []
        for tag_json in tags_json:
            tag_id = 0
            if not 'id' in tag_json:
                newTag = Tag()
                newTag.name = tag_json['name']
                db.session.add(newTag)
                db.session.commit()
                tag_id = newTag.id
            else:
                tag_id = tag_json['id']
            tag = Tag.query.get(tag_id)
            tags.append(tag)
        blog.tags = tags
    blog.author = g.user
    blog.createAt = datetime.now()
    db.session.add(blog)
    db.session.commit()
    return make_response(jsonify({'blog_id': blog.id}), 201)
Exemplo n.º 3
0
 def to_internal_value(self, data):
     try:
         return Tag.objects.get(name=data)
     except Tag.DoesNotExist:
         tag = Tag(name=data)
         tag.save()
         
         return tag
Exemplo n.º 4
0
def deploy():
    """Run deployment tasks."""
    from flask.ext.migrate import upgrade
    from app.models import User, Tag
    # migrate database to most recent revision
    upgrade()

    # add tag names
    Tag.insert_tags()
Exemplo n.º 5
0
def filter_tags(cleanedTags, idea):
    print "cleaned tags: " + cleanedTags
    Tag.objects.filter(idea = idea).delete()
    if cleanedTags:
        print "if cleaned tags"
        for tag in cleanedTags.split(','):
            if tag != '':
                tag = Tag(tag=tag.strip(), idea = idea)
                tag.save()
Exemplo n.º 6
0
    def add_post(title, content=None, tagnames=[]):
        post = Post(title=title, content=content)
        for tagname in tagnames:
            tag = db.session.query(Tag).filter(Tag.name==tagname).first()
            if not tag:
                tag = Tag(name=tagname)
                tag.save()
            post.tags.append(tag)

        post.save()
Exemplo n.º 7
0
def tags():
    form = TagForm()
    tag = Tag()
    if form.validate_on_submit():
        tag.name = form.name.data
        db.session.add(tag)
        flash('标签已添加')
    form.name.data = tag.name
    tags = db.session.query(Tag.name, func.count(Tag.name).label('post_count')).join(Tag.posts).group_by(
        Tag.name).all()
    return render_template('tags.html', tags=tags, form=form)
Exemplo n.º 8
0
def post_edit(post_id):
    if request.method == 'GET':
        try:
            post = Post.get(Post.post_id == post_id)  # todo: get not deleted
        except Post.DoesNotExist:
            abort(404)

        form = PostForm(obj=post)
        all_categories = Category.select()
        template = env.get_template('post/edit.html')
        return template.render(
            item=post,
            form=form,
            categories=all_categories,
        )
    elif request.method == 'POST':
        post = Post.get(Post.post_id == post_id)
        post.category = post_get('category-id')
        post.post_text = post_get('text')
        post.slug = post_get('slug')
        post.title = post_get('title')
        post.draft = bool(int(post_get('draft')))  # zero int is False
        post.language = post_get('language')
        post.show_on_index = bool(post_get('show-on-index'))
        post.date_updated = datetime.now()
        new_tags = post_get('tags')
        old_tags = Tag.select().join(Tag_to_Post)\
            .where(Tag_to_Post.post_id == post_id)
        remove_tags(old_tags, new_tags, post_id)
        add_new_tags(new_tags, post_id)
        post.save()
        app.flash('Article updated')
        redirect('/post/' + str(post_id))
Exemplo n.º 9
0
def all_tags():

    """
    Show all tags
    """

    tags = Tag.select().order_by(Tag.name)

    return render_template("tags.html", tags=tags)
Exemplo n.º 10
0
 def test_get_all_tags_by_post_id(self):
     file_object1 = werkzeug.datastructures.FileStorage(filename="fileupload1.JPG")
     file_object2 = werkzeug.datastructures.FileStorage(filename="fileupload2.JPG")
     p = Post.create(author_id=1, description="test", file_name=None, tag_list="food,fashion", choice_data=[("text_choice1", file_object1), ("text_choice2", file_object2)])
     tags = Tag.get_tags_by_post_id(p.post_id)
     tag_names = [str(tag.tag_name) for tag in tags]
     self.assertIn('food', tag_names)
     self.assertIn('fashion', tag_names)
     self.assertNotIn("apple", tag_names)
Exemplo n.º 11
0
def search_tag(page,keyword):
    if not keyword:
        keyword = request.form.get('keyword')


    if keyword == None or keyword == "":
        return redirect(url_for('list_tag'))
    else:
        tag = Tag.search_page(page,keyword)
        return render_template("admin/listtag.html",tag=tag,keyword=keyword)
Exemplo n.º 12
0
def add_new_tags(tags_string: str, post_id: int):
    """
    Add new tags or create connection to post with existed.
    """
    tags = tags_string.split(';')
    for tag in tags:
        tag = tag.strip()
        if not tag:
            continue
        try:
            old_tag = Tag.get(Tag.text == tag)
            try:
                tmp = Tag_to_Post.get(
                    Tag_to_Post.post_id == post_id,
                    Tag_to_Post.tag_id == old_tag.tag_id,
                )
            except Tag_to_Post.DoesNotExist:
                Tag_to_Post.create(post_id=post_id, tag_id=old_tag.tag_id)
        except DoesNotExist:
            new_tag = Tag.create(text=tag)
            Tag_to_Post.create(post_id=post_id, tag_id=new_tag.tag_id)
Exemplo n.º 13
0
def delete_tag(tag_id):

    """
    Delete Tag tag_id from DB
    """

    try:
        tag = Tag.get(id == tag_id)
    except Tag.DoesNotExist:
        abort(404)

    return render_template("tags.html", tags=tag)
Exemplo n.º 14
0
def edit_tag(tag_id):

    """
    Edit Tag tag_id in DB
    """

    try:
        tag = Tag.get(id == tag_id)
    except Tag.DoesNotExist:
        abort(404)

    return render_template("tags.html", tags=tag)
Exemplo n.º 15
0
def show_tag(tag_id):

    """
    Show titles associated with given tag tag_id
    """

    try:
        tags = Tag.select().where(Tag.id == tag_id).order_by(Tag.name)
    except Tag.DoesNotExist:
        abort(404)

    return render_template("tags.html", tags=tags)
Exemplo n.º 16
0
def submit(request):
    if request.method == 'POST':
        quote = Quote(
            text=request.POST['text'],
            submitter=request.user,
            )
        quote.save()
        tag_string = request.POST['tags']
        if len(tag_string) > 0:
            tag_texts = [Tag.make_valid_tag(text.strip()) for text in
                         tag_string.split(',')]
            for text in tag_texts:
                if len(text) > 0:
                    tag = Tag.objects.filter(text=text).first()
                    if tag is None:
                        tag = Tag(text=text)
                        tag.save()
                    quote.tags.add(tag)
        return redirect(reverse('app:detail', args=(quote.id,)))
    form = QuoteForm()
    context = {'form': form}
    return render(request, 'app/submit.html', context)
Exemplo n.º 17
0
def posts_for_tag(tag_id):
    try:
        tag = Tag.get(Tag.tag_id == tag_id)
    except DoesNotExist:
        # todo: enable logging for all these exceptions
        abort(404)
    posts = Post.get_posts().join(Tag_to_Post).\
        where(Tag_to_Post.tag_id == tag_id)
    how = posts.count()
    if how:
        info = 'Tagged with [%s] x%d' % (tag.text, how)
    else:
        info = 'No posts for [%s] tag' % tag.text
    template = env.get_template('post/list.html')
    return template.render(posts=posts, info=info)
Exemplo n.º 18
0
def same_tag(tag):
    """View blog posts with the same tag
    **Route:** ``/blog/tag/<tag>``

    **Methods:** ``GET``
    """

    tag = Tag.objects().get(tagname=tag)
    blog_posts = list(BlogPost.objects(post_tags=tag, published=True)
                      .order_by('-date_published')[:10])
    previous_index = None
    next_index = 1
    return render_template('blog/blog.html',
                           posts=blog_posts,
                           previous_index=previous_index,
                           next_index=next_index)
Exemplo n.º 19
0
def tag_add():
    form = TagForm()
    if form.validate_on_submit():
        data = form.data
        tag = Tag.query.filter_by(name=data["name"]).count()
        if tag == 1:
            flash("名称已经存在!", "err")
            return redirect(url_for('admin.tag_add'))
        tag = Tag(name=data["name"])
        db.session.add(tag)
        db.session.commit()
        flash("添加标签成功!", "ok")
        # 操作日志
        oplog = Oplog(admin_id=session["admin_id"],
                      ip=request.remote_addr,
                      reason="添加标签%s" % data["name"])
        db.session.add(oplog)
        db.session.commit()

        redirect(url_for('admin.tag_add'))
    return render_template("admin/tag_add.html", form=form)
Exemplo n.º 20
0
def title(title_id):

    """
    Render title (grid of thumbnails).
    Image 0 as blurred CSS background?
    """

    # Get title information
    try:
        volume = Volume.get(Volume.id == title_id)
    except Volume.DoesNotExist:
        abort(404)

    # Get list of images for this title
    thumbs = Image.select(Image, Volume).join(Volume).where(Volume.id == title_id).order_by(Image.page)

    # Get list of tags for this title
    tags = Tag.select(Tag, TagRelation, Volume).join(TagRelation).join(Volume).where(Volume.id == title_id).order_by(Tag.name)
    
    # pass list of thumbnails to template
    return render_template("title.html", title=volume.title,
                           id=str(title_id), thumbs=thumbs, tags=tags)
Exemplo n.º 21
0
def filter_tags(filter_string):
    """
    Display list of titles filtered by tags
    filter_string is dot-separated list of tag IDs

    Check that each tag id exists
    """
    filter_list = []
    filters = filter_string.split = (".")

    for x in filters:
        try:
            tag = Tag.get(id == x)
        except Tag.DoesNotExist:
            continue

        filter_list.append(x)

    # Filter titles on list of tag IDs

    t = Volume.select().join(Tag).where(Tag.id.in_(filter_list))
    return render_template("tags.html", tags=filter_list, title=t)
Exemplo n.º 22
0
def new():
    """Create a new blog post.

    **Route:** ``/admin/posts/new``

    **Methods:** ``POST``
    """
    form = CreateBlogPostForm(request.form)
    form.author.choices = [
        (str(u.id), u.name + " (You)" if u == g.user else u.name)
        for u in User.objects()
    ]
    form.author.data = str(g.user.id)
    upload_form = UploadImageForm()
    if form.validate_on_submit():
        author = User.objects().get(id=ObjectId(form.author.data))
        images = [Image.objects().get(filename=fn) for fn in form.images.data]
        tags = Tag.get_or_create_tags(form.tags.data)
        post = BlogPost(title=form.title.data,
                        slug=form.slug.data,
                        images=images,
                        markdown_content=form.body.data,
                        author=author,
                        posted_by=g.user, post_tags=tags)
        post.save()

        if form.published.data:
            post.publish()
        else:
            post.unpublish()

        if form.preview.data is True:
            return redirect(url_for('.preview', slug=post.slug))

        return redirect(url_for('.index'))
    images = Image.objects()
    return render_template('admin/posts/edit.html', user=g.user, form=form,
                           images=images, upload_form=upload_form)
Exemplo n.º 23
0
def get_tags(full_name, image_id):
    pages = get_pages_tags(full_name)
    image = Image.query.get(image_id)
    if pages > 0 and image:
        for page in xrange(1, pages + 1):
            tags = get_tags_page(full_name, page)
            if tags != None:
                for tag in tags:
                    tag_db = Tag.query.filter_by(image_id=image_id,
                                                 name=tag["name"]).first()
                    if not tag_db:
                        t = Tag(name=tag["name"], \
                                last_updated=tag["last_updated"], \
                                full_size= tag["full_size"], \
                                id_docker=tag["id"], \
                                image_id=image_id)
                        logging.info("Saving - image_id %s - %s", image_id,
                                     t.name)
                        db.session.add(t)
                        db.session.commit()
        image.tags_checked = datetime.utcnow().replace(tzinfo=UTC)
        db.session.commit()
        logging.info("Get tags finished: %s", image_id)
Exemplo n.º 24
0
def submission():
    form = PostForm()
    form.tag(placeholder='aaa')
    if form.validate_on_submit():
        save_to_db_objs = []
        post = Post()
        post.title = form.titile.data
        post.content = form.content.data
        post.category_id = form.category.data
        post.author_id = current_user.id
        if form.tag.data:
            tags = form.tag.data.split(',')
            for t in tags:
                tag_obj = Tag.query.filter_by(name=t).first()
                if tag_obj is None:
                    tag_obj = Tag(name=t)
                post.tags.append(tag_obj)
                save_to_db_objs.append(tag_obj)
        save_to_db_objs.append(post)
        db.session.add_all(save_to_db_objs)
        db.session.commit()
        return redirect(url_for('.post_index'))

    return render_template('frontend/submission.html', form=form)
Exemplo n.º 25
0
 def test_09_generate_tag(self):
     """测试批量新建标签"""
     Tag.generate_fake(20)
     self.assertTrue(Tag.query.count() == 20)
Exemplo n.º 26
0
def edit(post_id):
    """Edit an existing blog post.

    **Route:** ``/admin/posts/edit/<post_id>``

    **Methods:** ``GET, POST``

    :param str post_id: The ID of the post to edit.
    """
    try:
        object_id = ObjectId(post_id)
    except InvalidId:
        return abort(404)
    try:
        post = BlogPost.objects().with_id(object_id)
    except (DoesNotExist, ValidationError):
        flash('Cannot find blog post with id {}.'.format(post_id), ERROR_FLASH)
        return redirect(url_for('.index'))

    if request.method == 'POST':
        form = CreateBlogPostForm(request.form)
        form.author.choices = [
            (str(u.id), u.name + " (You)" if u == g.user else u.name)
            for u in User.objects()]
        form.author.default = str(g.user.id)

        if form.validate_on_submit():
            post.title = form.title.data
            post.author = User.objects.get(id=ObjectId(form.author.data))
            post.slug = form.slug.data
            post.markdown_content = form.body.data
            post.images = [
                Image.objects().get(filename=fn) for fn in form.images.data
            ]

            post.post_tags = Tag.get_or_create_tags(form.tags.data)
            if form.featured_image.data:
                post.featured_image = Image.objects().get(
                    filename=form.featured_image.data)
            else:
                post.featured_image = None
            post.save()

            if post.published != form.published.data:
                if form.published.data:
                    post.publish()
                    flash('Blogpost published', MESSAGE_FLASH)
                else:
                    post.unpublish()
                    flash('Blogpost unpublished', MESSAGE_FLASH)

            if form.preview.data is True:
                return redirect(url_for('.preview', slug=post.slug))

    upload_form = UploadImageForm()
    feat_img = post.featured_image.filename if post.featured_image else None
    form = CreateBlogPostForm(request.form,
                              title=post.title,
                              slug=post.slug,
                              published=post.published,
                              body=post.markdown_content,
                              images=[image.filename for image in post.images],
                              author=str(post.author.id),
                              featured_image=feat_img, tags=post.post_tags)
    form.author.choices = [
        (str(u.id), u.name + " (You)" if u == g.user else u.name)
        for u in User.objects()
    ]
    form.author.default = str(g.user.id)
    images = [image for image in Image.objects() if image not in post.images]

    return render_template('admin/posts/edit.html',
                           user=g.user,
                           form=form,
                           post=post,
                           images=images,
                           upload_form=upload_form)
Exemplo n.º 27
0
    def test_create_photo(self):
        # Start with photo
        photo = Photo()
        photo.instagram_id = '757677846642235453_1134805'
        photo.caption = '126. Who needs fireworks anyway'
        photo.created_time = datetime(2012, 02, 29)
        photo.featured_on = 'Josh Johnson; #JJ'
        photo.link = 'http://instagram.com/p/qDVDHkyxvS/'

        db.session.add(photo)
        db.session.commit()

        self.assertIn(photo, db.session)

        # Mock some images
        hi_img = Image()
        hi_img.height = 640
        hi_img.width = 640
        hi_img.url = 'http://scontent-b.cdninstagram.com/hphotos-xpa1/t51.2885-15/10522310_677385995673625_267718762_n.jpg'
        hi_img.type = 'standard_resolution'

        low_img = Image()
        low_img.height = 306
        low_img.width = 306
        low_img.url = 'http://scontent-b.cdninstagram.com/hphotos-xpa1/t51.2885-15/10522310_677385995673625_267718762_a.jpg'
        low_img.type = 'low_resolution'

        thumb_img = Image()
        thumb_img.height = 150
        thumb_img.width = 150
        thumb_img.url = 'http://scontent-b.cdninstagram.com/hphotos-xpa1/t51.2885-15/10522310_677385995673625_267718762_s.jpg'
        thumb_img.type = 'thumbnail'

        images = [hi_img, low_img, thumb_img]
        db.session.add_all(images)
        db.session.commit()

        self.assertTrue(all(i in db.session for i in images))

        # Connect images to photo
        photo.images.extend(images)
        db.session.commit()

        self.assertTrue(all(i in photo.images for i in images))
        self.assertTrue(all(photo is i.photo for i in images))

        # Mock location and tag
        loc = Location()
        loc.instagram_id = '1'
        loc.name = 'Dogpatch Labs'
        loc.latitude = 37.782
        loc.longitude = -122.387

        db.session.add(loc)
        db.session.commit()

        self.assertIn(loc, db.session)

        tag = Tag()
        tag.name = 'july4th'

        db.session.add(tag)
        db.session.commit()

        self.assertIn(tag, db.session)

        # Connect location and tag to photo
        photo.locations.append(loc)
        photo.tags.append(tag)

        db.session.commit()

        self.assertIn(loc, photo.locations)
        self.assertIn(tag, photo.tags)
        self.assertIn(photo, loc.photos)
        self.assertIn(photo, tag.photos)
Exemplo n.º 28
0
 def test_10_generate_post(self):
     """测试批量增加文章"""
     Tag.generate_fake(40)
     Category.generate_fake(30)
     Post.generate_fake()
     self.assertTrue(Post.query.count() == 30)
Exemplo n.º 29
0
def all_tags():
    """
    Fetch all tags.
    """
    cursor = Tag.find({}, sort=[('weight', pymongo.DESCENDING)])
    return [t for t in cursor]
Exemplo n.º 30
0
 def test_is_existed(self):
     self.assertFalse(Tag.is_existed('dog'))
     t = Tag(name='dog')
     db.session.add(t)
     db.session.commit()
     self.assertTrue(Tag.is_existed('dog'))
Exemplo n.º 31
0
def add_fake_data(number_fakes):
    """
    Adds fake data to the database.
    """
    User.generate_fake(count=number_fakes)
    Tag.generate_fake(count=number_fakes)
Exemplo n.º 32
0
 def testTagCount(self):
   tag_count = Event.all_for_tag("python").count()
   tag = Tag.get_by_name("python")
   self.assertEqual(tag_count, tag.count)
Exemplo n.º 33
0
def scan_archive_file(archives, filetype):
    # Loop over zips & rars, get tags from titles
    # Add volume to Volume DB
    # Add tags to Tag DB (many-to-many mapping)
    # Add pages to Image DB

    # initialise mimetypes
    mimetypes.init()

    # Iterate over input list of archive files
    for f in archives:

        # Attempt to scan ZIP file        
        if filetype == 'zip':

            try:
                myfile = zipfile.ZipFile(INPUT_PATH+f, 'r')
            except zipfile.BadZipfile as e:
                raise Exception('"{}" is not a valid ZIP file! Error: {}'.format(f, e))
            except:
                print('Unknown error: ZIP extraction failed: {}'.format(f))
                raise

        # Scan RAR file
        elif filetype == 'rar':

            try:
                myfile = rarfile.RarFile(INPUT_PATH+f, 'r')
            except (rarfile.BadRarFile, rarfile.NotRarFile) as e:
                raise Exception('"{}" is not a valid RAR file! Error: {}'.format(f, e))
            except:
                print('Unknown error: RAR extraction failed: {}'.format(f))
                raise

        else:

            raise ValueError('Unrecognised archive type value: {}'.format(filetype))

        # Get members from archive
        members = myfile.namelist()

        # Filter member list for images
        members = [x for x in members if is_image(x) is True]
        
        # Sort members
        members = natsorted(members)

        # Get number of images
        member_count = len(members)

        # If images found...
        if member_count > 0:

            # Generate MD5 checksum for archive file
            md5 = md5sum(INPUT_PATH+f)

            # Parse title, tags from filename
            title, tags = tag.split_title_tags(f)

            # Add volume to DB Volume table
            vol = Volume.create(title=title, filename=f, md5=md5, filetype=filetype, num=member_count, comments='')

            # Add tags to DB Tags table
            for t in tags:
                # check if tag already exists, insert if not
                try:
                    new_tag = Tag.get(Tag.name == t)
                except DoesNotExist:
                    new_tag = Tag.create(name=t, descr='')

                # insert tag and volume id into TagRelation table
                TagRelation.create(relVolume=vol.id, relTag=new_tag.id)

            # Reset page counter (assume cover is page 0)
            page = 0  

            # Generate display title
            disptitle=title[:20]
            if len(disptitle) < len(title):
                disptitle = disptitle+'...'
            disptitle.ljust(24)

            # initialise progress bar display
            widgets = [disptitle+': ', Counter(),'/'+str(member_count)+' ', Bar(marker='=', left='[', right=']'), ETA()]
            pbar = ProgressBar(widgets=widgets, maxval=member_count).start()
            
            # Attempt to create thumbnail directory if it doesn't already exist
            path = THUMB_PATH + str(vol.id)

            try:
                os.makedirs(path)
            except OSError:
                if not os.path.isdir(path):
                    raise

            # Iterate over images in zip file
            for m in members:

                # Guess mimetype for image from filename
                (mimetype, encoding) = mimetypes.guess_type(m)

                # Create record in Image table
                # >>> TODO: image record should include image height & width
                im = Image.create(volume=vol.id, page=page, mimetype=mimetype, filename=m)

                # Generate thumbnails
                # >>> TODO: May spawn greenlets to do this?

                # Read data from archive
                rawdata = myfile.read(m)
                
                # Generate Page object
                p = Page(rawdata)

                # Create thumbnail
                p.thumb(path+'/'+'{:03d}'.format(page)+'.jpg')
                
                # Update progress bar
                pbar.update(page)
                page += 1

            
            # end progress bar
            pbar.finish()

        # Close archive
        myfile.close()
Exemplo n.º 34
0
def find_or_create_tags(tag_names):
    return [Tag.get_or_create(tag_name) for tag_name in tag_names]