Exemplo n.º 1
0
    def create_snippet(cls, snippet_id, snippet):
        """Create Snippet Table.

        Arguments:
            snippet_id {str} -- the id of snippet
            snippet {list} -- the list contains snippet information

        Returns:
            table -- Snippet table
        """
        if db_session.query(exists().where(Snippet.id == snippet_id)).scalar():
            tableSnippet = db_session.query(Snippet).filter(
                Snippet.id == snippet_id).first()
        else:
            if len(snippet) == 4 or len(snippet) == 5:
                topic = snippet[1]
                content = snippet[2]
                stance = snippet[3]
                # print("stance ", stance)
                tableSnippet = Snippet(id=snippet_id,
                                       content=content,
                                       topic=topic,
                                       stance=stance)
            else:
                print("invalid statement.")
                print("length ", len(snippet))
                print("statement ", snippet)
                return None
        return tableSnippet
Exemplo n.º 2
0
    def create_statement(cls, statement_id, statement):
        """Create Statement Table.

        Arguments:
            statement_id {str} -- the id of statement
            index_statement {int} -- the index of statement
            statements {list} -- the list contains statements

        Returns:
            table -- Statement table
        """
        if db_session.query(
                exists().where(Statement.id == statement_id)).scalar():
            tableStatement = db_session.query(Statement).filter(
                Statement.id == statement_id).first()
            # print("duplicated")
        else:
            # print("statement ", statement)
            if len(statement) == 4 or len(statement) == 5:
                topic = statement[1]
                content = statement[2]
                stance = statement[3]
                # print("stance ", stance)
                tableStatement = Statement(id=statement_id,
                                           content=content,
                                           target=topic,
                                           stance=stance)
            else:
                print("invalid statement.")
                print("length ", len(statement))
                print("statement ", statement)
                return None
        return tableStatement
Exemplo n.º 3
0
def show_entry():
    entry = db_session.query(Entry).\
            filter(Entry.id==request.args['id']).\
            one()
    comments = db_session.query(Comment).\
               filter(Comment.id2==request.args['id']).\
               all()
    return render_template('show_entry.html', entry=entry, comments=comments)
Exemplo n.º 4
0
    def create_event(cls, event_name):
        """Create Event table.

        Arguments:
            event_name {str} -- name of event

        Returns:
            table -- event table
        """
        if db_session.query(exists().where(Event.name == event_name)).scalar():
            tableEvent = db_session.query(Event).filter(
                Event.name == event_name).first()
        else:
            tableEvent = Event(name=event_name)
        return tableEvent
Exemplo n.º 5
0
def show_list():
    if 'oauth_token' in session:
        cur = db_session.query(Entry)
        List = [dict(id = entry.id, title=entry.title) for entry in cur]
        session['logged_in'] = True
        return render_template('show_list.html', List=List)
    return redirect(url_for('login'))
Exemplo n.º 6
0
def create():
    """Create a new post for the current user."""
    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        error = None

        if not title:
            error = 'Title is required.'

        if error is not None:
            flash(error)
        else:
            # for u, a in session.query(User, Address).
            # filter(User.id==Address.user_id).\
            #filter(Address.email_address=='*****@*****.**').\ all():
            u1 = g.user.id
            u = db_session.query(User).filter(User.id == u1).first()
            # p = Post( title, body, g.user['id'] )
            # Post(user_id=None , user = None ,created =None, body=None )
            # Post(title =title , body=body)
            # user = u ,
            p = Post(title=title, body=body)
            u.posts = [p]

            #db_session.add(p)
            db_session.commit()
            return redirect(url_for('blog.index'))

    return render_template('blog/create.html')
Exemplo n.º 7
0
def index():
    """Show all the posts, most recent first."""
    # et = session.query(DatasetRevision).order_by(desc(DatasetRevision.revision))
    posts = db_session.query(Post).all()
    #posts = db_session.query(Post).order_by(desc(Post.created)).all()

    return render_template('blog/index.html', posts=posts)
Exemplo n.º 8
0
    def create_cluster(cls, cluster):
        """Create Cluster table.

        Arguments:
            cluster {Path} -- the path of cluster

        Returns:
            table -- cluster table
        """
        print(cluster)
        if db_session.query(
                exists().where(Cluster.name == cluster.name)).scalar():
            clusterTable = db_session.query(Cluster).filter(
                Cluster.name == cluster.name).first()
        else:
            clusterTable = Cluster(name=cluster.name)
        return clusterTable
Exemplo n.º 9
0
def add_opinion(tweet_id, flag, opinion_value):
    """Add user's opinion to rumor."""
    print("tweet_id", tweet_id)
    print("opinion", opinion_value)
    print("user_id", current_user.id)
    rumor = Rumor.query.filter_by(tweet_id=tweet_id).first()
    rumor_id = rumor.id
    # check if the record existance
    existed = db_session.query(exists().where(and_(
        Opinion.flag == flag,
        Opinion.stance == opinion_value,
        Opinion.rumor_id == rumor_id,
        Opinion.user_name == current_user.username
    ))).scalar()
    if existed:
        # record existed
        print("record existed")
        print("Opinion", Opinion.query.all())
        r = make_response('existed', 200)
        return r
    else:
        # check if record updation
        print("checking update")
        q = db_session.query(Opinion).filter(and_(
            Opinion.flag == flag,
            Opinion.rumor_id == rumor_id,
            Opinion.user_name == current_user.username
        ))
        if q.all():
            # update record
            print("updating record...")
            q.update({"stance": opinion_value})
            db_session.commit()
            r = make_response('updated', 200)
            return r
    # insert new record
    print("insert record...")
    opinion = Opinion(flag=flag, stance=opinion_value)
    opinion.rumor = rumor
    current_user.rumors.append(opinion)
    db_session.add(current_user)
    db_session.commit()
    print("Opinion", Opinion.query.all())
    r = make_response('inserted', 200)
    return r
Exemplo n.º 10
0
def facebook_authorized(resp):
    if resp is None:
        return 'Access denied: reson=%s error=%s' % (request.args['error_reason'], request.args['error_description'])
    session['oauth_token'] = (resp['access_token'], '')
    me = facebook.get('/me')
    cur = db_session.query(FacebookLogin).filter(FacebookLogin.email==me.data['email']).all()
    if len(cur) != 0:
        flash('Logged in as email %s' % me.data['email']) 
    else:
        flash('please check your email address')
    return redirect(url_for('show_list'))
Exemplo n.º 11
0
def delete(id):
    """Delete a post.

    Ensures that the post exists and that the logged in user is the
    author of the post.
    """
    get_post(id)
    p = db_session.query(Post).filter(Post.id == id).all()
    db_session.delete(p)
    db_session.commit()

    return redirect(url_for('blog.index'))
Exemplo n.º 12
0
def del_comment():
    password = request.form.get("delCommentPassword")
    cur = db_session.query(Comment).\
          filter(Comment.id==request.args['id2'], Comment.id2==request.args['id']).\
          one()
    if password != cur.password:
        flash('Invalid password')
    else:
        db_session.delete(cur)
        db_session.commit()
        flash('Delete comment success')
    return redirect(url_for('show_entry', id=request.args['id']))
Exemplo n.º 13
0
    def create_rumor(cls, rumor, rumorID):
        """Create Rumor table.

        Arguments:
            rumor {list} -- the list contains rumor information

        Returns:
            table -- Rumor table
        """
        # print("rumor ", rumor)
        if db_session.query(
                exists().where(Rumor.tweet_id == rumorID)).scalar():
            tableRumor = db_session.query(Rumor).filter(
                Rumor.tweet_id == rumorID).first()
        else:
            tableRumor = Rumor(tweet_id=rumorID,
                               target=rumor[1],
                               tweet=rumor[2],
                               stance=rumor[3],
                               date=datetime.strptime(rumor[4],
                                                      '%Y-%m-%d %H:%M:%S'))
        return tableRumor
Exemplo n.º 14
0
def del_e():
    if not session.get('logged_in'):
        abort(401)
    cur = db_session.query(Entry).\
          filter(Entry.id==request.args['id']).\
          one()
    if request.form['entryPassword2'] == str(cur.password):
        db_session.delete(cur)
        db_session.commit()
        flash('Delete entry success')
    else:
        flash('Invalid password')
    return redirect(url_for('show_list'))
Exemplo n.º 15
0
def edit_entry():
    if not session.get('logged_in'):
        abort(401)
    cur = db_session.query(Entry).\
          filter(Entry.id==request.args['id']).\
          one()
    if request.form['entryPassword1'] == str(cur.password):
        cur.title = request.form['title']
        cur.text = request.form['text']
        db_session.commit()
        flash('Edit entry Success')
    else:
        flash('Invalid password')
    return redirect(url_for('show_entry', id=request.args['id']))
Exemplo n.º 16
0
def delete_project():
    """
    根据monitor info删除信息

    :return:
    """
    delete_id = request.args.get('monitor_project_info_id')
    print('delete id={}'.format(delete_id))
    m = db_session.query(MonitorProjectInfo).filter(
        MonitorProjectInfo.id == delete_id).first()
    if m:
        print('result = {}'.format(m))
        db_session.delete(m)
        db_session.commit()
        return jsonify({'success': 'true'})
    else:
        return jsonify({'success': 'false', 'info': '该条数据不存在'})
Exemplo n.º 17
0
def edit(id):
    u = db_session.query(User).filter_by(id=id).first()
    if request.method == 'GET':
        form = RegistrationForm(firstName=u.firstName,
                                lastName=u.lastName,
                                email=u.email)
        return render_template('edit.html', user=u, form=form)
    else:
        form = RegistrationForm(request.form)
        if form.validate():
            u.firstName = request.form['firstName']
            u.lastName = request.form['lastName']
            u.email = request.form['email']
            flash('User updated')
            db_session.commit()
            return redirect(url_for('index'))
        else:
            return render_template('edit.html', form=form)
Exemplo n.º 18
0
def query_project_list():
    # todo
    """
    查询项目列表
    :return:
    """
    all_project_list = db_session.query(MonitorProject).all()

    if all_project_list is None:
        return jsonify({
            'success':
            'flase',
            'error':
            'The database does not exist for this project info'
        })
    else:
        info_list = []
        for project in all_project_list:
            info_list.append(project.serialization_info())
        # return render_template('/crawler_base.html')
        return jsonify([{'xdays': '123', 'yvalue': '678'}])
Exemplo n.º 19
0
def get_post(id, check_author=True):
    """Get a post and its author by id.

    Checks that the id exists and optionally that the current user is
    the author.

    :param id: id of post to get
    :param check_author: require the current user to be the author
    :return: the post with author information
    :raise 404: if a post with the given id doesn't exist
    :raise 403: if the current user isn't the author
    """
    #u = User.query(User ).filter(User.posts.id == id ).first()
    post = db_session.query(Post).filter(Post.id == id).first()
    if post is None:
        abort(404, "Post id {0} doesn't exist.".format(id))

    if check_author and post['author_id'] != g.user['id']:
        abort(403)

    return post
Exemplo n.º 20
0
def update(id):
    """Update a post if the current user is the author."""
    post = get_post(id)

    if request.method == 'POST':
        title = request.form['title']
        body = request.form['body']
        error = None

        if not title:
            error = 'Title is required.'

        if error is not None:
            flash(error)
        else:
            p = db_session.query(Post).filter(Post.id == id).all()
            p.title = title
            p.body = body
            db_session.commit()
            return redirect(url_for('blog.index'))

    return render_template('blog/update.html', post=post)
Exemplo n.º 21
0
def insert_project():
    """
    插入一条数据
    {
        "project_name": "ad",
        "total_quantity" : 1,
        "crawler_quantity":1,
        "result_quantity":1,
        "crawler_start_time":1,
        "crawler_end_time":1,
        "author":"小武神",
        "partner":"锦鲤"
    }
    :return:
    """
    json_data = request.get_json()
    project = db_session.query(MonitorProject).filter(
        MonitorProject.project_name == json_data['project_name']).first()
    print("project={}".format(project))

    if not project:
        # 数据库不存在project id
        m = MonitorProject()
        m.project_name = json_data['project_name']
        db_session.add(m)
        db_session.flush()
        db_session.commit()

        project_info = MonitorProjectInfo()
        insert_project_info(m.id, json_data, project_info)
        print(json_data)
    else:
        # 已经存在,跟新其他信息
        project_info = MonitorProjectInfo()
        insert_project_info(project.id, json_data, project_info)
        print(project.id)

    return jsonify({'success': 'true'})
Exemplo n.º 22
0
def update_project():
    """
    更新项目信息
    :return:
    """
    update_id = request.args.get('monitor_project_info_id')
    total_quantity = request.args.get('total_quantity', type=int, default=None)
    crawler_quantity = request.args.get('crawler_quantity',
                                        type=int,
                                        default=None)
    result_quantity = request.args.get('result_quantity',
                                       type=int,
                                       default=None)
    crawler_start_time = request.args.get('crawler_start_time', default=None)
    crawler_end_time = request.args.get('crawler_end_time', default=None)
    author = request.args.get('author', type=str, default=None)
    partner = request.args.get('partner', type=str, default=None)
    # 这里查询条件可以为多个
    project_info = db_session.query(MonitorProjectInfo).filter(
        MonitorProjectInfo.id == update_id).first()
    if project_info is None:
        return jsonify({
            'success':
            'flase',
            'error':
            'The database does not exist for this project info'
        })
    else:
        project_info.total_quantity = total_quantity
        project_info.crawler_quantity = crawler_quantity
        project_info.result_quantity = result_quantity
        project_info.crawler_start_time = crawler_start_time
        project_info.crawler_end_time = crawler_end_time
        project_info.author = author
        project_info.partner = partner
        db_session.commit()
        return jsonify({'success': 'true', 'update_id': update_id})
Exemplo n.º 23
0
    def delete_data(cls, eventname):
        """Delete event data from database: Snippet, Rumor, Statement, Event_Cluster, Event"""
        event_clusters = db_session.query(Event_Cluster).filter(
            Event_Cluster.event_name == eventname).all()
        for event_cluster in event_clusters:
            statements = db_session.query(Statement).filter(
                Statement.event_cluster_id == event_cluster.id).all()
            for statement in statements:
                # delete snippets
                db_session.query(Snippet).filter(
                    Snippet.statement_id == statement.id).delete()
            # delete rumors
            db_session.query(Rumor).filter(
                Rumor.event_cluster_id == event_cluster.id).delete()
            # delete statements
            db_session.query(Statement).filter(
                Statement.event_cluster_id == event_cluster.id).delete()
        # delete event_clusters
        db_session.query(Event_Cluster).filter(
            Event_Cluster.event_name == eventname).delete()
        # delete events
        db_session.query(Event).filter(Event.name == eventname).delete()

        db_session.commit()