def create_tag(auth_token): try: post_data = request.get_json(force=True) except BadRequest as e: raise ApiError(e.description) new_tag = Tag() new_tag.update(post_data) db_session.add(new_tag) db_session.commit() return jsonify(new_tag.to_dict(max_depth=2))
def create_issue(auth_token): try: post_data = request.get_json(force=True) except BadRequest as e: raise ApiError(e.description) post_data['author'] = auth_token.owner.email new_issue = Issue() new_issue.update(post_data) db_session.add(new_issue) db_session.commit() return jsonify(new_issue.to_dict(max_depth=2))
def create_milestone(auth_token): try: post_data = request.get_json(force=True) except BadRequest as e: raise ApiError(e.description) if 'due_date' in post_data: post_data['due_date'] = from_unix_timestamp(post_data['due_date']) new_milestone = Milestone() new_milestone.update(post_data) db_session.add(new_milestone) db_session.commit() return jsonify(new_milestone.to_dict(max_depth=2))
def create_user(auth_token): try: post_data = request.get_json(force=True) except BadRequest as e: raise ApiError(e.description) if not post_data.get('password', False): raise ApiError('Missing or empty password.') post_data['password'] = md5(post_data['password'].encode()).hexdigest() new_user = User() new_tag.update(post_data) db_session.add(new_user) db_session.commit() return jsonify(new_user.to_dict(max_depth=2))
def __call__(self): # Create an application context. app = create_app(__name__, []) ctx = app.test_request_context() ctx.push() parser = argparse.ArgumentParser( prog=self.argv[0], description="Manage the user's account.") subparsers = parser.add_subparsers(dest='subcommand') subparsers.required = True sub = subparsers.add_parser('add', help='add a user') sub.add_argument('email', action='store', help="the email of the new user's account") sub.add_argument( '-n', '--name', dest='name', action='store', help='the full name of the user (default: email address)') sub.add_argument( '-p', '--password', dest='password', action='store', help='the full name of the user (will be asked if not provided)') sub = subparsers.add_parser('list', help='list users') args = parser.parse_args(self.argv[1:]) if args.subcommand == 'add': new_user = User() new_user.email = args.email new_user.name = args.name or args.email if args.password: password = args.password else: password = getpass('password: '******'confirm: ') != password: raise InvalidArgumentError('Password do not match.') new_user.password = md5(password.encode()).hexdigest() db_session.add(new_user) db_session.commit() elif args.subcommand == 'list': for user in db_session.query(User): print('name: {:>15}, email: {:>15}'.format(user.name, user.email)) ctx.pop()
def create_attachment(auth_token): # Check if the file format is valid (solely on its filename). file = request.files['file'] if not (file and check_file_ext(file.filename)): raise ApiError('Invalid file format.') # Create a file UID based on the file content, so we avoid storing # duplicates under different filenames. h = md5() while True: buf = file.read(128) if not buf: break h.update(buf) fuid = h.hexdigest() # Seek for an existing file reference on the upload. attachment = db_session.query(Attachment).filter(Attachment.uid == fuid).first() if attachment is None: # Create the attachment reference in the database. attachment = Attachment() attachment.uid = fuid attachment.name = file.filename attachment.filename = os.path.join(current_app.config['UPLOAD_FOLDER'], fuid) file_type, _ = mimetypes.guess_type(file.filename) if file_type is not None: attachment.mime_type = file_type # Save the upload. file.seek(0) file.save(attachment.filename) db_session.add(attachment) db_session.commit() return_status = 201 else: return_status = 200 return jsonify(attachment.to_dict(max_depth=2)), return_status
def create_token(): post_data = request.get_json(force=True) # get the credentials email = post_data.get('email') password = md5(post_data.get('password', '').encode()).hexdigest() # search for the user identified by email/password user = db_session.query(User).filter( User.email == email, User.password == password ).first() if user is None: abort(403) # generate a new token for the authenticated user token = make_auth_token(user) db_session.add(token) db_session.commit() return jsonify(token.to_dict()), 201