def main(sample_size, show_progress, *args, **kwargs):

    # Set up progress bar.
    if show_progress:
        progress_bar = ProgressBar(maxval=len(TAGS), widgets=[
            'Progress: ', Percentage(),
            ' ', Bar(marker=RotatingMarker()),
            ' ', ETA(),
            ' Fetched posts for ', Counter(), ' / ' + str(len(TAGS)) + ' tags.'
        ])
        progress_bar.start()

    # Fetch statistics for posts related to each tag
    for tag_count, tag in enumerate(TAGS, start=1):

        # As it turns out, we can make the selection of random posts tractable if we
        # don't do a random ordering in SQL but instead do a random selection
        # in Python.  So we start by fetching all of the post IDs for a tag,
        # make the random choice locally, and then query for the posts in this subset.
        post_id_tuples = (
            PostTag.select()
            .join(Tag, on=(PostTag.tag_id == Tag.id))
            .where(Tag.tag_name == tag)
            .select(PostTag.post_id)
            .tuples()
        )

        # We convert this from a 2D Nx1 matrix to a 1D N-length matrix by taking its
        # transpose and then getting the "first row" of the transpose.
        post_ids = np.array(post_id_tuples).T[0]
        sample_post_ids = np.random.choice(post_ids, sample_size, replace=False).tolist()

        post_records = (
            Post.select(Post.title, Post.creation_date, Post.answer_count, Post.comment_count,
                        Post.favorite_count, Post.score, Post.view_count)
            .where(Post.id << sample_post_ids)
            .dicts()
        )

        # Store which tag this record is associated with
        for record in post_records:
            record['tag_name'] = tag

        yield post_records

        if show_progress:
            progress_bar.update(tag_count)

    if show_progress:
        progress_bar.finish()

    raise StopIteration
示例#2
0
def admin_tag_delete():
    status = {'ok': True}

    data = request.get_json()
    if 'id' not in data:
        abort(400)

    id_to_delete = data["id"]

    if id_to_delete:
        try:
            tag_to_delete = Tag.get(Tag.id == id_to_delete)
            posttags_to_delete = PostTag.select().where(
                PostTag.tag == tag_to_delete)
            for posttag in posttags_to_delete:
                posttag.delete_instance()
            tag_to_delete.delete_instance()

        except Tag.DoesNotExist:
            status['ok'] = False

    return json.dumps(status)
示例#3
0
def admin_post_delete():
    status = {'ok': True}

    data = request.get_json()
    if 'id' not in data:
        abort(400)

    id_to_delete = data["id"]

    if id_to_delete:
        try:
            post_to_delete = Post.get(Post.id == id_to_delete)
            posttags_to_delete = PostTag.select().where(
                PostTag.post == post_to_delete)
            for posttag_to_delete in posttags_to_delete:
                posttag_to_delete.delete_instance()
            postuser_to_delete = PostUser.select().where(
                PostUser.post == post_to_delete)[0]
            postuser_to_delete.delete_instance()
            post_to_delete.delete_instance()

            if request.form.get('was_edit', None) and request.form.get(
                    'was_edit', None) == 'true':
                flash('Deleted post ' + str(post_to_delete.id) + ' !',
                      "success")

        except Post.DoesNotExist:
            flash("Post does not exist, please look into the sql table",
                  "danger")
            status['ok'] = False

            #       except peewee.IntegrityError:
            #           flash("Peewee.IntegrityError there seems to be a foreign key constraint error", "danger")
            #           status['ok'] = False
    else:
        status['ok'] = False

    # return json.dumps({ "message" : "Deleted post.", "status" : "success"})
    return json.dumps(status)