예제 #1
0
def delete_status(id):
    """Delete an existing status

    The status to be deleted should be posted as JSON using
    'application/json as the content type. The posted JSON needs to
    have 2 required fields:

    * user (the username)
    * api_key

    An example of the JSON::

        {
            "user": "******",
            "api_key": "qwertyuiopasdfghjklzxcvbnm1234567890"
        }

    """
    db = get_session(current_app)

    # The data we need
    user = request.json.get('user')

    if not (id and user):
        return jsonify(dict(error='Missing required fields.')), 400

    status = db.query(Status).filter_by(id=id)

    if not status.count():
        return jsonify(dict(error='Status does not exist.')), 400

    if not status[0].user.username == user:
        return jsonify(dict(error='You cannot delete this status.')), 403

    status.delete()
    db.commit()

    return jsonify(dict(id=id))
예제 #2
0
파일: views.py 프로젝트: tarasglek/standup
def delete_status(id):
    """Delete an existing status

    The status to be deleted should be posted as JSON using
    'application/json as the content type. The posted JSON needs to
    have 2 required fields:

    * user (the username)
    * api_key

    An example of the JSON::

        {
            "user": "******",
            "api_key": "qwertyuiopasdfghjklzxcvbnm1234567890"
        }

    """
    db = get_session(current_app)

    # The data we need
    user = request.json.get('user')

    if not (id and user):
        return jsonify(dict(error='Missing required fields.')), 400

    status = db.query(Status).filter_by(id=id)

    if not status.count():
        return jsonify(dict(error='Status does not exist.')), 400

    if not status[0].user.username == user:
        return jsonify(dict(error='You cannot delete this status.')), 403

    status.delete()
    db.commit()

    return jsonify(dict(id=id))
예제 #3
0
파일: views.py 프로젝트: Ms2ger/standup
def authenticate():
    """Authenticate user with Persona."""
    app = current_app
    db = get_session(app)

    data = browserid.verify(request.form['assertion'],
                            app.config.get('SITE_URL'))
    email = data['email']
    session['email'] = email

    # Create a user if one does not already exist for this email
    # address.
    user = db.query(User).filter_by(email=email).first()
    if user:
        session['user_id'] = user.id

    return jsonify({'email': email})
예제 #4
0
def authenticate():
    """Authenticate user with Persona."""
    app = current_app
    db = get_session(app)

    data = browserid.verify(request.form['assertion'],
                            app.config.get('SITE_URL'))
    email = data['email']
    session['email'] = email

    # Create a user if one does not already exist for this email
    # address.
    user = db.query(User).filter_by(email=email).first()
    if user:
        session['user_id'] = user.id

    return jsonify({'email': email})
예제 #5
0
파일: views.py 프로젝트: abhilashsn/standup
def create_team():
    """Creates a new team."""
    db = get_session(current_app)
    team = Team()

    team.slug = request.form.get('slug')

    if not team.slug:
        return api_error(400, 'No slug provided.')

    team.name = request.form.get('name', team.slug)

    db.add(team)

    try:
        db.commit()
    except IntegrityError:
        return api_error(400, 'Slug is already in use.')

    return jsonify(team.dictify())
예제 #6
0
파일: views.py 프로젝트: Ms2ger/standup
def create_team():
    """Creates a new team."""
    db = get_session(current_app)
    team = Team()

    team.slug = request.form.get('slug')

    if not team.slug:
        return api_error(400, 'No slug provided.')

    team.name = request.form.get('name', team.slug)

    db.add(team)

    try:
        db.commit()
    except IntegrityError:
        return api_error(400, 'Slug is already in use.')

    return jsonify(team.dictify())
예제 #7
0
def get_statuses():
    """Get all status updates.

    Returns id, user (the name), project name and timestamp of statuses.

    The amount of items to return is determined by the limit argument
    (defaults to 20)::

        /api/v1/feed/?limit=20

    An example of the JSON::

        {
            "1": {
                "user": "******",
                "content": "working on bug 123456",
                "project": "sumodev",
                "timestamp": "2013-01-11T21:13:30.806236"
            }
        }

    """
    db = get_session(current_app)

    limit = request.args.get('limit', 20)

    statuses = db.query(Status).filter_by(reply_to=None)\
        .order_by(desc(Status.created)).limit(limit)

    data = OrderedDict()
    for row in statuses:
        id = row.id
        created = row.created.isoformat()
        if row.project is not None:
            project_name = row.project.name
        else:
            project_name = None
        data[id] = (dict(author=row.user.name, content=row.content,
                         timestamp=created, project=project_name))

    return jsonify(data)
예제 #8
0
파일: views.py 프로젝트: tarasglek/standup
def get_statuses():
    """Get all status updates.

    Returns id, user (the name), project name and timestamp of statuses.

    The amount of items to return is determined by the limit argument
    (defaults to 20)::

        /api/v1/feed/?limit=20

    An example of the JSON::

        {
            "1": {
                "user": "******",
                "content": "working on bug 123456",
                "project": "sumodev",
                "timestamp": "2013-01-11T21:13:30.806236"
            }
        }

    """
    db = get_session(current_app)

    limit = request.args.get('limit', 20)

    statuses = db.query(Status).filter_by(reply_to=None)\
        .order_by(desc(Status.created)).limit(limit)

    data = OrderedDict()
    for row in statuses:
        id = row.id
        created = row.created.isoformat()
        if row.project is not None:
            project_name = row.project.name
        else:
            project_name = None
        data[id] = (dict(author=row.user.name, content=row.content,
                         timestamp=created, project=project_name))

    return jsonify(data)
예제 #9
0
파일: errors.py 프로젝트: robhudson/standup
def api_error(code, message):
    error = dict(request=request.path, message=message)
    return jsonify(error), code
예제 #10
0
파일: views.py 프로젝트: tarasglek/standup
def create_status():
    """Post a new status.

    The status should be posted as JSON using 'application/json' as
    the content type. The posted JSON needs to have 3 required fields:

    * user (the username)
    * content
    * api_key

    An example of the JSON::

        {
            "user": "******",
            "project": "sumodev",
            "content": "working on bug 123456",
            "api_key": "qwertyuiopasdfghjklzxcvbnm1234567890"
        }

    """
    db = get_session(current_app)

    # The data we need
    username = request.json.get('user')
    project_slug = request.json.get('project')
    content = request.json.get('content')
    reply_to = request.json.get('reply_to')

    # Validate we have the required fields.
    if not (username and content):
        return jsonify(dict(error='Missing required fields.')), 400

    # If this is a reply make sure that the status being replied to
    # exists and is not itself a reply
    if reply_to:
        replied = db.query(Status).filter_by(id=reply_to).first()
        if not replied:
            return jsonify(dict(error='Status does not exist.')), 400
        elif replied.reply_to:
            return jsonify(dict(error='Cannot reply to a reply.')), 400
    else:
        replied = None

    # Get the user
    user = db.query(User).filter_by(username=username).first()
    if not user:
        #autocreate users for testing
        user = user_test_thingy(username=username, name=username, email=username+"@mozilla.com", slug=username, save=True)

    # Get or create the project (but not if this is a reply)
    if project_slug and not replied:
        # This forces the slug to be slug-like.
        project_slug = slugify(project_slug)
        project = db.query(Project).filter_by(slug=project_slug).first()
        if not project:
            project = Project(slug=project_slug, name=project_slug)
            db.add(project)
            db.commit()

    # Create the status
    status = Status(user_id=user.id, content=content, content_html=content)
    if project_slug and project:
        status.project_id = project.id
    if replied:
        status.reply_to_id = replied.id
    db.add(status)
    db.commit()

    return jsonify(dict(id=status.id, content=content))
예제 #11
0
파일: views.py 프로젝트: tarasglek/standup
def update_user(username):
    """Update settings for an existing user.

    The settings to be deleted should be posted as JSON using
    'application/json as the content type. The posted JSON needs to
    have 2 required fields:

    * user (the username of the IRC user)
    * api_key

    You may optionally supply the following settings to overwrite
    their values:

    * name
    * email
    * github_handle

    An example of the JSON::

        {
            "user": "******",
            "email": "*****@*****.**"
            "api_key": "qwertyuiopasdfghjklzxcvbnm1234567890"
        }

    """
    db = get_session(current_app)

    # The data we need
    authorname = request.json.get('user')

    # Optional data
    name = request.json.get('name')
    email = request.json.get('email')
    github_handle = request.json.get('github_handle')

    if not (username and authorname and (name or email or github_handle)):
        return jsonify(dict(error='Missing required fields')), 400

    author = db.query(User).filter_by(username=authorname).first()

    user = db.query(User).filter_by(username=username).first()

    if not user or not author:
        return jsonify(dict(error='User does not exist.')), 400

    if author.username != user.username and not author.is_admin:
        return jsonify(dict(error='You cannot modify this user.')), 403

    if name:
        user.name = name

    if email:
        user.email = email

    if github_handle:
        user.github_handle = github_handle

    db.commit()

    return jsonify(dict(id=user.id))
예제 #12
0
파일: views.py 프로젝트: Ms2ger/standup
def logout():
    """Log user out of app."""
    session.pop('email')
    session.pop('user_id')
    return jsonify({'message': 'logout successful'})
예제 #13
0
파일: views.py 프로젝트: robhudson/standup
    try:
        params = _get_params(request)

        statuses = db.query(Status)
        statuses = _handle_since(statuses, params)
        statuses = _handle_max(statuses, params)
        statuses = _handle_include_replies(statuses, params)
        statuses = _handle_count(statuses, MAX, params)
    except ApiError, e:
        return api_error(400, str(e))

    data = []
    for status in statuses:
        data.append(status.dictify(trim_user=params["trim_user"], trim_project=params["trim_project"]))

    return jsonify(data)


@blueprint.route("/statuses/user_timeline.json", methods=["GET"])
def user_timeline():
    """Get a collection of the user's recent status updates."""
    app = current_app
    db = get_session(app)
    MAX = app.config.get("API2_TIMELINE_MAX_RESULTS", TIMELINE_MAX_RESULTS)

    try:
        params = _get_params(request)
    except ApiError, e:
        return api_error(400, str(e))

    user_id = request.args.get("user_id")
예제 #14
0
def create_status():
    """Post a new status.

    The status should be posted as JSON using 'application/json' as
    the content type. The posted JSON needs to have 3 required fields:

    * user (the username)
    * content
    * api_key

    An example of the JSON::

        {
            "user": "******",
            "project": "sumodev",
            "content": "working on bug 123456",
            "api_key": "qwertyuiopasdfghjklzxcvbnm1234567890"
        }

    """
    db = get_session(current_app)

    # The data we need
    username = request.json.get('user')
    project_slug = request.json.get('project')
    content = request.json.get('content')
    reply_to = request.json.get('reply_to')

    # Validate we have the required fields.
    if not (username and content):
        return jsonify(dict(error='Missing required fields.')), 400

    # If this is a reply make sure that the status being replied to
    # exists and is not itself a reply
    if reply_to:
        replied = db.query(Status).filter_by(id=reply_to).first()
        if not replied:
            return jsonify(dict(error='Status does not exist.')), 400
        elif replied.reply_to:
            return jsonify(dict(error='Cannot reply to a reply.')), 400
    else:
        replied = None

    # Get the user
    user = db.query(User).filter_by(username=username).first()
    if not user:
        return jsonify(dict(error='User does not exist.')), 400

    # Get or create the project (but not if this is a reply)
    if project_slug and not replied:
        # This forces the slug to be slug-like.
        project_slug = slugify(project_slug)
        project = db.query(Project).filter_by(slug=project_slug).first()
        if not project:
            project = Project(slug=project_slug, name=project_slug)
            db.add(project)
            db.commit()

    # Create the status
    status = Status(user_id=user.id, content=content, content_html=content)
    if project_slug and project:
        status.project_id = project.id
    if replied:
        status.reply_to_id = replied.id
    db.add(status)
    db.commit()

    return jsonify(dict(id=status.id, content=content))
예제 #15
0
def logout():
    """Log user out of app."""
    session.pop('email')
    session.pop('user_id')
    return jsonify({'message': 'logout successful'})
예제 #16
0
파일: views.py 프로젝트: Ms2ger/standup
                                 TIMELINE_MAX_RESULTS)

    try:
        params = _get_timeline_params()

        statuses = db.query(Status)
        statuses = _handle_weekly(statuses, params)
        statuses = _handle_since(statuses, params)
        statuses = _handle_max(statuses, params)
        statuses = _handle_include_replies(statuses, params)
        statuses = _handle_count(statuses, MAX, params)
    except ApiError, e:
        return api_error(e.code, str(e))

    data = _get_data(statuses, params)
    return jsonify(data)


@blueprint.route('/statuses/user_timeline.json', methods=['GET'])
@crossdomain(origin='*')
def user_timeline():
    """Get a collection of the user's recent status updates."""
    db = get_session(current_app)
    MAX = current_app.config.get('API2_TIMELINE_MAX_RESULTS',
                                 TIMELINE_MAX_RESULTS)

    try:
        params = _get_timeline_params()
    except ApiError, e:
        return api_error(e.code, str(e))
예제 #17
0
파일: views.py 프로젝트: abhilashsn/standup
                                 TIMELINE_MAX_RESULTS)

    try:
        params = _get_timeline_params()

        statuses = db.query(Status)
        statuses = _handle_weekly(statuses, params)
        statuses = _handle_since(statuses, params)
        statuses = _handle_max(statuses, params)
        statuses = _handle_include_replies(statuses, params)
        statuses = _handle_count(statuses, MAX, params)
    except ApiError, e:
        return api_error(e.code, str(e))

    data = _get_data(statuses, params)
    return jsonify(data)


@blueprint.route('/statuses/user_timeline.json', methods=['GET'])
@crossdomain(origin='*')
def user_timeline():
    """Get a collection of the user's recent status updates."""
    db = get_session(current_app)
    MAX = current_app.config.get('API2_TIMELINE_MAX_RESULTS',
                                 TIMELINE_MAX_RESULTS)

    try:
        params = _get_timeline_params()
    except ApiError, e:
        return api_error(e.code, str(e))
예제 #18
0
def update_user(username):
    """Update settings for an existing user.

    The settings to be deleted should be posted as JSON using
    'application/json as the content type. The posted JSON needs to
    have 2 required fields:

    * user (the username of the IRC user)
    * api_key

    You may optionally supply the following settings to overwrite
    their values:

    * name
    * email
    * github_handle

    An example of the JSON::

        {
            "user": "******",
            "email": "*****@*****.**"
            "api_key": "qwertyuiopasdfghjklzxcvbnm1234567890"
        }

    """
    db = get_session(current_app)

    # The data we need
    authorname = request.json.get('user')

    # Optional data
    name = request.json.get('name')
    email = request.json.get('email')
    github_handle = request.json.get('github_handle')

    if not (username and authorname and (name or email or github_handle)):
        return jsonify(dict(error='Missing required fields')), 400

    author = db.query(User).filter_by(username=authorname).first()

    user = db.query(User).filter_by(username=username).first()

    if not user or not author:
        return jsonify(dict(error='User does not exist.')), 400

    if author.username != user.username and not author.is_admin:
        return jsonify(dict(error='You cannot modify this user.')), 403

    if name:
        user.name = name

    if email:
        user.email = email

    if github_handle:
        user.github_handle = github_handle

    db.commit()

    return jsonify(dict(id=user.id))
예제 #19
0
def api_error(code, message):
    error = dict(request=request.path, message=message)
    return jsonify(error), code