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))
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})
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())
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)
def api_error(code, message): error = dict(request=request.path, message=message) return jsonify(error), code
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))
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))
def logout(): """Log user out of app.""" session.pop('email') session.pop('user_id') return jsonify({'message': 'logout successful'})
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")
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))
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))