Ejemplo n.º 1
0
def init_db():
    """Initializes the database."""
    call(['rm', 'post.db'])
    db.connect()
    db.create_tables([Post], safe=True)
    for x in xrange(1, 11):
        Post.create(title='Post-{}'.format(x), body='lorem ipsum lorem ipsum')
Ejemplo n.º 2
0
def save_post():
    post_data = json.loads(request.data)
    url = post_data["url"]
    json_data = make_readable(url)

    title = post_data["title"]
    slug = userutils.make_slug(title)
    author = User.objects(username=current_user.username).first()

    p = Post(title=title, slug=slug)
    p.saved_date = datetime.now()
    p.thumbnail = post_data["thumbnail"]
    p.url = url
    p.author = author
    p.content = json_data["content"]
    p.excerpt = json_data["excerpt"]
    p.domain = post_data["domain"]
    p.save()

    q.enqueue(insert_new_relation, p)

    author.posts.append(p)
    author.save()

    return "ok"
Ejemplo n.º 3
0
    def post(self, gid):
        body = request.get_json()
        user = request.authorization  # this gives dict
        uid = User.objects.get(username=user['username'])  # this gives user object
        user_id = str(uid.id)  # this gives the user id in string format
        group = Group.objects.get(id=gid)
        if user_id in group.role_dict:
            post = Post(**body, group_id=ObjectId(gid))
            post.date_created = datetime.now()
            post.save()
            # group id is a reference field and rf only takes object id.
            # convert object id to string with str when required
            post_id = post.id
            recipients = []
            # update last active status for user
            temp_dict = group.last_active_dict
            temp_dict[user_id] = datetime.now()
            group.update(set__last_active_dict=temp_dict)

            for x in group.role_dict:
                if group.role_dict[x] == A or group.role_dict[x] == MD:
                    u = User.objects.get(id=x)
                    if u:
                        recipients.append(u.email)

            if group.role_dict[user_id] == M:
                content = "{name} wants to put a post, please accept his request!".format(name=user.username)
                queue.enqueue(printhello())  # this is just a check that q works
                queue.enqueue(send_mail, recipients, content)

            return {'post_id': str(post_id)}, 200
        else:
            return "You ain't a member of this group", 500
Ejemplo n.º 4
0
def user(username):
    u = User()
    user = u.get_user(username=username)

    title = request.form.get('new-title')
    body = request.form.get('new-body')

    if title and body:
        p = Post(title=title, body=body, user_id=user.id)
        p.add_post()

    posts = u.get_posts_by_user(user.id)

    if not posts:
        posts = [
            Post(id=2,
                 title="There's nothing here yet!",
                 body="There's nothing here yet!",
                 user_id=-1)
        ]

    if user:
        return render_template('user_page.html',
                               username=user.username,
                               posts=posts)

    return "user not found"  # TODO change the custom 404
Ejemplo n.º 5
0
 def get(self, id=None):
     if not id:
         objects = Post.objects()
         return PostSchema().dump(objects, many=True)
     post = Post.objects(id=id).get()
     post.views += 1
     post.save()
     return PostSchema().dump(Post.objects(id=id).get())
Ejemplo n.º 6
0
    def post(self):
        csrf_value = self.request.get('csrf-token')

        if not memcache.get(csrf_value):
            return self.write('CSRF attack detected!!')

        title = cgi.escape(self.request.get('title'))
        content = cgi.escape(self.request.get('text'))
        user = users.get_current_user()
        email = user.email()
        new_post = Post(title=title, content=content, user_email=email)

        new_post.put()
        return self.redirect_to('post', post_id=new_post.key.id())
Ejemplo n.º 7
0
def delete_post():
    post_data = json.loads(request.data)
    post_id = post_data["postId"]

    post = Post.objects(id=post_id).first()
    author = User.objects(username=current_user.username).first()
    if post.author != author:
        return json.dumps({"status": "not-allowed"})

    Relation.objects(post1=post).delete()
    Relation.objects(post2=post).delete()
    Post.objects(id=post_id).delete()

    return json.dumps({"status": "success"})
Ejemplo n.º 8
0
    def post(self):
        subject = self.request.get("subject")
        content = self.request.get("content")

        if subject and content:
            post = Post(subject=subject, content=content)
            post.put()
            post_id = str(post.key().id())
            self.redirect('/post/%s' % post_id)
        else:
            error = "both subject and content are required"
            self.render("newpost.html",
                        subject=subject,
                        content=content,
                        error=error)
Ejemplo n.º 9
0
def newpost():
    # Init form
    form = NewpostForm()

    # If POST
    if request.method == 'POST':

        # Init user from poster
        session_user = User.query.filter_by(
            username=session['username']).first()

        # Init content from form request
        content = request.form['content']

        # Create in DB
        new_post = Post(author=session_user.uid,
                        content=content,
                        createdAt=datetime.now())
        Db.session.add(new_post)
        Db.session.commit()

        return redirect(url_for('index'))

    # If GET
    else:
        return render_template('newpost.html', title='Newpost', form=form)
Ejemplo n.º 10
0
def insert_new_relation(post1):
    """

    Arguments:
    - `post1`: newly added post
    """
    posts = Post.objects()
    if post1.post_type == "pdf":
        return None

    for post2 in posts:
        if post2.post_type != "pdf" and post2.url != post1.url:
            # text similarity
            text1 = post1.content.lower()
            text2 = post2.content.lower()
            vector1 = text_to_vector(text1)
            vector2 = text_to_vector(text2)
            content_cosine = get_cosine(vector1, vector2)
            # title similarity
            title1 = post1.title.lower()
            title2 = post2.title.lower()
            tvector1 = text_to_vector(title1)
            tvector2 = text_to_vector(title2)
            title_cosine = get_cosine(tvector1, tvector2)

            category_point = get_category_point(post1, post2)
            cosine = content_cosine + title_cosine + category_point

            if cosine > 0.1:
                relation = Relation(post1, post2, cosine)
                relation.save()

                relation = Relation(post2, post1, cosine)
                relation.save()
Ejemplo n.º 11
0
def index():
    if request.method == 'GET':
        if "user_name" in session:
            name = session["user_name"]
            posts = Post.query.order_by(Post.due, Post.title).all()
            all_user = User.query.order_by(User.user_name).all()
            title_name = 'index'

            return render_template("index.html",
                                   name=name,
                                   posts=posts,
                                   all_user=all_user,
                                   today=date.today(),
                                   title_name=title_name)
        else:
            return redirect(url_for("top", status="logout"))
    else:
        title = request.form.get('title')
        detail = request.form.get('detail')
        due = request.form.get('due')
        due = datetime.strptime(due, '%Y-%m-%d')
        user_name = session['user_name']
        new_post = Post(title=title,
                        detail=detail,
                        due=due,
                        user_name=user_name)
        db_session.add(new_post)
        db_session.commit()
        db_session.close()
        return redirect('/index')
Ejemplo n.º 12
0
def add_fake(numbers):
    if numbers is not None:
        try:
            for _ in range(int(numbers)):
                new_post = Post(title=fake.name(), content=fake.text())
                db.session.add(new_post)
                db.session.commit()

            response = {
                'success': True,
                'message': '%s Fakes created' % numbers
            }

        except ValueError:
            response = {
                'success': False,
                'message': 'Please submit a number',
            }

    else:
        response = {
            'success': False,
            'message': "Numbers is not define, please add has url parameter"
        }

    return jsonify(response)
Ejemplo n.º 13
0
def newpost():
    # Init form
    form = NewpostForm()

    # If POST
    if request.method == "POST":

        # Init user from poster
        session_user = User.query.filter_by(
            username=session["username"]).first()

        # Init content from form request
        content = request.form["content"]

        # Create in DB
        new_post = Post(author=session_user.uid,
                        content=content,
                        created_at=datetime.now())
        Db.session.add(new_post)
        Db.session.commit()

        flash("You successfully created a post")
        return redirect(url_for("index"))

    # If GET
    else:
        return render_template("newpost.html", title="Newpost", form=form)
Ejemplo n.º 14
0
def insert_new_relation(post1):
    """

    Arguments:
    - `post1`: newly added post
    """
    posts = Post.objects()
    if post1.post_type == "pdf":
        return None

    for post2 in posts:
        if post2.post_type != "pdf" and post2.url != post1.url:
            # text similarity
            text1 = post1.content.lower()
            text2 = post2.content.lower()
            vector1 = text_to_vector(text1)
            vector2 = text_to_vector(text2)
            content_cosine = get_cosine(vector1, vector2)
            # title similarity
            title1 = post1.title.lower()
            title2 = post2.title.lower()
            tvector1 = text_to_vector(title1)
            tvector2 = text_to_vector(title2)
            title_cosine = get_cosine(tvector1, tvector2)

            category_point = get_category_point(post1, post2)
            cosine = content_cosine + title_cosine + category_point

            if cosine > 0.1:
                relation = Relation(post1, post2, cosine)
                relation.save()

                relation = Relation(post2, post1, cosine)
                relation.save()
Ejemplo n.º 15
0
    def get(self):
        current_time = datetime.now()
        time_deleted_limit = current_time - timedelta(minutes=2)
        posts = Post.query(Post.deleted == True,
                           Post.time_updated < time_deleted_limit).fetch()

        for post in posts:
            post.key.delete()
Ejemplo n.º 16
0
    def test_edit_post(self):
        with test_database(test_db, (Post, )):
            post = self.create_testdata(1)[0]
            Post.objects.edit(post.id, 'changed', 'new body')

            changed = Post.get(Post.id == post.id)
        self.assertEqual(changed.title, 'changed')
        self.assertEqual(changed.body, 'new body')
Ejemplo n.º 17
0
def add_note_to_post():
    post_data = request.form
    post_note = post_data.get("note")
    post_id = post_data.get("id")
    post = Post.objects(id=post_id).first()
    post.note = post_note
    post.save()

    return redirect('/user/profile')
Ejemplo n.º 18
0
 def get(self, tag):
     if not tag:
         return jsonify(**{'error': 'Incorrect tag'})
     tags = Tag.objects().distinct('title')
     if tag not in tags:
         abort(404, message=f'Not found posts with tag: {tag}')
     tag = Tag.objects(title=tag).get()
     posts = Post.objects(tag=tag)
     return PostSchema().dump(posts, many=True)
Ejemplo n.º 19
0
 def get(self, first_name=None):
     if not first_name:
         return jsonify(**{'error': 'Incorrect Author'})
     authors = Author.objects().distinct('first_name')
     if first_name not in authors:
         abort(404, message=f'Not found posts with author: {first_name}')
     author = Author.objects(first_name=first_name).get()
     posts = Post.objects(author=author)
     return PostSchema().dump(posts, many=True)
Ejemplo n.º 20
0
def build_relation_db():
    """
    Build a relation collection that includes
    every similarity between posts.

    Only includes relation when similarity > 0.2

    This takes a lot of time, run this periodically.
    Eg. once a week or everynight.

    Use insert_new_relation() for new posts

    """
    posts = Post.objects()
    posts2 = Post.objects()
    Relation.drop_collection()
    counter = 0
    print counter
    for p1 in posts:
        for p2 in posts2:
            if p1.url != p2.url:
                if p1.post_type != "pdf" and p2.post_type != "pdf":
                    counter = counter + 1

                    # text similarity
                    text1 = p1.content.lower()
                    text2 = p2.content.lower()
                    vector1 = text_to_vector(text1)
                    vector2 = text_to_vector(text2)
                    content_cosine = get_cosine(vector1, vector2)
                    # title similarity
                    title1 = p1.title.lower()
                    title2 = p2.title.lower()
                    tvector1 = text_to_vector(title1)
                    tvector2 = text_to_vector(title2)
                    title_cosine = get_cosine(tvector1, tvector2)

                    category_point = get_category_point(p1, p2)
                    cosine = content_cosine + title_cosine + category_point

                    if cosine > 0.1:
                        relation = Relation(p1, p2, cosine)
                        relation.save()
    print counter
Ejemplo n.º 21
0
def build_relation_db():
    """
    Build a relation collection that includes
    every similarity between posts.

    Only includes relation when similarity > 0.2

    This takes a lot of time, run this periodically.
    Eg. once a week or everynight.

    Use insert_new_relation() for new posts

    """
    posts = Post.objects()
    posts2 = Post.objects()
    Relation.drop_collection()
    counter = 0
    print counter
    for p1 in posts:
        for p2 in posts2:
            if p1.url != p2.url:
                if p1.post_type != "pdf" and p2.post_type != "pdf":
                    counter = counter + 1

                    # text similarity
                    text1 = p1.content.lower()
                    text2 = p2.content.lower()
                    vector1 = text_to_vector(text1)
                    vector2 = text_to_vector(text2)
                    content_cosine = get_cosine(vector1, vector2)
                    # title similarity
                    title1 = p1.title.lower()
                    title2 = p2.title.lower()
                    tvector1 = text_to_vector(title1)
                    tvector2 = text_to_vector(title2)
                    title_cosine = get_cosine(tvector1, tvector2)

                    category_point = get_category_point(p1, p2)
                    cosine = content_cosine + title_cosine + category_point

                    if cosine > 0.1:
                        relation = Relation(p1, p2, cosine)
                        relation.save()
    print counter
Ejemplo n.º 22
0
 def get(tag=None):
     try:
         tag_id = Tag.objects(tag='#' + tag.lower())[0].id
         posts = Post.objects(tag=tag_id)
         for post in posts:
             post.views += 1
             post.save()
         return PostSchema(many=True).dump(posts)
     except IndexError:
         return []
Ejemplo n.º 23
0
def single_post(seq, slug):
    seq = int(seq)
    user = get_user()
    post = Post.objects(seq=seq).first()
    related_posts = Relation.objects(post1=post).order_by("-similarity")[:3]

    return render_template(
        'single.html', user=user, post=post,
        related_posts=related_posts
    )
Ejemplo n.º 24
0
def sync_post():
    post_data = request.json
    post_content = post_data.get("content")
    post_id = post_data.get("postid")

    p = Post.objects(id=post_id).first()
    p.content = post_content
    p.save()

    return json.dumps({"status": "success"})
Ejemplo n.º 25
0
 def get(nick_name=None):
     try:
         author_id = Author.objects(nick_name=nick_name)[0].id
         posts = Post.objects(author=author_id)
         for post in posts:
             post.views += 1
             post.save()
         return PostSchema(many=True).dump(posts)
     except IndexError:
         return []
Ejemplo n.º 26
0
    def post(self, post_id):
        value_csrf = self.request.get('csrf-token')

        if not memcache.get(value_csrf):
            return self.write('CSRF Attack Detected!')

        post = Post.get_by_id(int(post_id))
        content = cgi.escape(self.request.get('comment'))
        Comment.save_comment(post_id, content)

        return self.redirect_to('post', post_id=post.key.id())
Ejemplo n.º 27
0
    def post(self):
        if not self.user:
            self.redirect('/login')

        subject = self.request.get('subject')
        content = self.request.get('content')

        if subject and content:
            post = Post(parent=blog_key(),
                        subject=subject,
                        content=content,
                        author=self.user.key)
            post.put()
            self.redirect('/blog/%s' % str(post.key.id()))
        else:
            error = 'Both subject and content must be filled in!'
            self.render('newpost.html',
                        subject=subject,
                        conetent=content,
                        error=error)
Ejemplo n.º 28
0
def share_post():
    post_data = request.form
    post_id = post_data.get("id", "")
    recepients = post_data.get("recepients", "")  # this is still text
    username = current_user.username
    post = Post.objects(id=post_id).first()

    mmodule = mailutils.MailModule(username, recepients)
    mmodule.share_post(post)

    return redirect("/user/profile")
Ejemplo n.º 29
0
    def get(self, post_id):
        post = Post.get_by_id(int(post_id))
        user = users.get_current_user()
        email = ''

        if user:
            email = user.email()
        comments = Comment.query(Comment.user_email == email,
                                 Comment.postID == post.key.id()).fetch()
        params = {'post': post, 'comments': comments}
        return self.render_template('delete_comment.html', params=params)
Ejemplo n.º 30
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been created', 'success')
        return redirect(url_for('main.home'))
    return render_template('create_post.html', form=form)
Ejemplo n.º 31
0
Archivo: main.py Proyecto: sup/fill
def create_post(id=None):
    """Create a Post"""
    # Hacky redirect - refactor in the future
    username = request.cookies.get('username')
    cookie_user = User.get_user(username)
    if request.method == "GET":
        return redirect('timeline/' + str(cookie_user.key.id()))
    if not id:
        return redirect('timeline/' + str(cookie_user.key.id()))
    else:
        user = User.get_user_by_id(id)
    if user == cookie_user and request.method == "POST":
        title = request.form.get("title")
        body = request.form.get("body")
        # Create a Post
        post = Post(title=title, body=body, writer=user.key)
        post.put()
        return redirect('timeline/' + str(cookie_user.key.id()))
    else:
        return redirect('timeline/' + str(cookie_user.key.id()))
Ejemplo n.º 32
0
def share_tag(tag):
    post_data = request.form
    recepients = post_data.get("recepients", "")
    username = current_user.username
    user = get_user()

    posts = Post.objects(author=user, tags=tag).order_by("-saved_date")
    if posts:
        mmodule = mailutils.MailModule(username, recepients)
        mmodule.share_tag(tag, posts)

    return redirect("/user/profile")
Ejemplo n.º 33
0
    def get(self, gid):
        user = request.authorization  # this gives dict
        uid = User.objects.get(username=user['username'])  # this gives user object
        user_id = str(uid.id)  # this gives the user id in string format
        group = Group.objects.get(id=gid)

        if group.visibility == 'public':
            posts = Post.objects(group_id=gid).to_json()
            for post in posts:
                if post.approval:
                    return Response(post, mimetype="application/json", status=200)
                else:
                    return ""
        elif user_id in group.role_dict:
            posts = Post.objects(group_id=gid).to_json()
            for post in posts:
                if post.approval:
                    return Response(post, mimetype="application/json", status=200)
                else:
                    return ""
        else:
            return "You do not have the required access", 200
Ejemplo n.º 34
0
def profile():
    user = get_user()
    unsorted_freqs = User.objects(
        username=user.username
    ).item_frequencies(field="tags")

    freqs = sorted(
        unsorted_freqs.items(), key=operator.itemgetter(1),
        reverse=True
    )

    posts = Post.objects(author=user).order_by("-saved_date")
    posts = set_time_zones(posts)
    return render_template('profile.html', user=user, posts=posts, freqs=freqs)
Ejemplo n.º 35
0
def save_tag_post():
    post_data = request.form
    post_tag = post_data.get("tag")
    post_tag = userutils.make_slug(post_tag)
    post_id = post_data.get("id")
    user = get_user()
    post = Post.objects(id=post_id).first()

    if post_tag not in post.tags:
        post.tags.append(post_tag)
        post.save()
        user.tags.append(post_tag)
        user.save()

    q.enqueue(after_tagging_calculation, post)

    return redirect('/user/profile')
Ejemplo n.º 36
0
Archivo: main.py Proyecto: sup/fill
def timeline(id=None):
    """Return Profile Page"""
    # Hacky redirect - refactor in the future
    username = request.cookies.get('username')
    cookie_user = User.get_user(username)
    if not id:
        return redirect('timeline/' + str(cookie_user.key.id()))
    else:
        user = User.get_user_by_id(id)
    if user:
        is_owner = cookie_user == user
        joined_events = Event.get_events_by_volunteer(user.key)
        created_events = Event.get_events_by_admin(user.key)
        posts = Post.get_posts_by_writer(user.key)
        first_name = user.name.split(" ")[0]
        return render_template('timeline.html', is_owner=is_owner, user=user, first_name=first_name, joined_events=joined_events, created_events=created_events, posts=posts)
    else:
        return render_template('home.html', page_title="FILL")
Ejemplo n.º 37
0
Archivo: main.py Proyecto: sup/fill
def delete_post(id=None):
    # Check user
    username = request.cookies.get('username')
    user = User.get_user(username)
    if not user:
        return redirect(url_for('home'))
    # Check id 
    if id is None:
        return redirect('timeline/' + str(user.key.id()))
    # Check Post
    post = Post.get_post_by_id(id)
    if not post:
        return redirect('timeline/' + str(user.key.id()))
    if post.writer.id() != user.key.id():
        return redirect('timeline/' + str(user.key.id()))

    # Delete the event
    post.key.delete()
    return redirect('timeline/' + str(user.key.id()))
Ejemplo n.º 38
0
def save_post_pdf():
    post_data = request.form
    title = post_data.get("title")
    slug = userutils.make_slug(title)
    author = User.objects(username=current_user.username).first()

    print post_data.get("domain")

    if not title:
        return "please provide a title"

    p = Post(title=title, slug=slug)
    p.saved_date = datetime.now()
    p.thumbnail = post_data.get("thumbnail")
    p.url = post_data.get("url")
    p.author = author
    p.content = ""
    p.excerpt = ""
    p.post_type = "pdf"
    p.domain = post_data.get("domain")
    p.save()
    return redirect('/user/profile')
Ejemplo n.º 39
0
def index():
    user = get_user()
    recent = Post.objects(author=user).order_by("-saved_date")
    return render_template("index.html", user=user, recent=recent)
Ejemplo n.º 40
0
 def get(self):
     doc = Post.find_one_by(Post.slug == self.slug)
     raise tornado.gen.Return(doc)