def post(self): data = request.get_json(force=True) form = RegistrationForm.from_json(data) if not form.validate(): return {'errors': form.errors}, 400 user = Account(**data) user.save_to_db() @after_this_request def set_jwt_cookies(response): user_tokens = create_tokens(user) set_cookies(user_tokens, response) return response return {'username': user.username, 'userID': user.id, 'email': user.email}, 201
def post(self): params = PARSER.parse_args() if not params: abort(415) password = params.get('password', False) adjectiveList = ["Happy", "Silly", "Tiny", "Super", "Musical", "Funny"] colorList = ['Yellow', 'Pink', 'Green', 'Blue', 'Orange', 'Red'] animalList = ['Elephant', 'Unicorn', 'Giraffe', 'Dinosaur', 'Kangaroo'] adjective = random.choice(adjectiveList) color = random.choice(colorList) animal = random.choice(animalList) number = str(random.randint(1, 100)) username = adjective + color + animal + number hash = hash_password(password) account = False with Database(auto_commit=True) as db: account = db.query(Account).filter_by(name=username).first() # if account: # abort(403) new = Account(username, hash) db.add(new) return jsonify(Signup=True, Username=username, Password=password)
def validate(self): rv = Form.validate(self) if not rv: return False email_in_use = Account.find_by_email(self.email.data) is not None username_in_use = Account.find_by_email(self.username.data) is not None if email_in_use: self.email.errors.append("Email is already in use.") return False elif username_in_use: self.username.errors.append("Username is already in use.") return False return True
def get(self, paste_uuid): paste = Paste.find_by_uuid(paste_uuid) identity = get_jwt_identity() current_user_id = Account.find_by_username(identity).id if paste is None: return {'error': 'Paste not found'}, 404 if paste.owner_id != current_user_id: return {'error': 'You can not delete pastes you do not own.'}, 401 paste.delete() return {'result': 'Paste deleted.'}, 204
def post(self): data = request.get_json(force=True) form = SubmitPasteForm.from_json(data) if not form.validate(): return {'errors': form.errors}, 401 identity = get_jwt_identity() data['owner_id'] = Account.find_by_username(identity).id this_paste = Paste(**data) this_paste.save_to_db() return {'paste_uuid': this_paste.paste_uuid}, 200
def getaccount(cls, accountid): ''' Get/Create an Account based on the given Account ID ''' account = session.query(Account).get(accountid) if account: return account else: print 'Creating New Account %s...' % accountid account = Account(id=accountid) session.add(account) session.commit() return account
def get(self, paste_uuid): paste = Paste.find_by_uuid(paste_uuid) if paste is None: return {'error': 'Paste not found'}, 404 identity = get_jwt_identity() current_user_id = Account.find_by_username(identity).id if paste.owner_id != current_user_id and not paste.open_edit: return {'error': 'You are not the owner of this paste, and open edit is not enabled for it.'}, 401 paste_information = paste.paste_dict() # Strip out unneeded information and set expiration to 0 for client for key in ['deletion_inbound', 'expiration_date']: paste_information.pop(key) paste_information['expiration'] = 0 return {'paste': paste_information}, 200
def post(self): data = request.get_json(force=True) form = LoginForm.from_json(data) if not form.validate(): return {'errors': form.errors}, 401 user = Account.find_by_username(data.get('username')) @after_this_request def set_jwt_cookies(response): user_tokens = create_tokens(user) set_cookies(user_tokens, response) return response return {'username': user.username, 'userID': user.id, 'email': user.email}, 200
def get(self, *args): name = self.get_argument('name') acc = session.query(Account).filter(Account.name == name).one_or_none() if not acc: acc = Account(name=name) logger.info(session.add(acc)) logger.info(session.commit()) a = acc data = { "id": a.id, 'value': '{}-{}-{}-{}'.format(a.name, a.balance, a.ccy, a.limit)} logger.info(data) for c in sockets: c.write_message(data) self.finish()
def validate(self): rv = Form.validate(self) if not rv: return False user = Account.find_by_username(self.username.data) if not user: self.username.errors.append("Username not found.") return False if not user.password_correct(self.password.data): self.password.errors.append("Password is incorrect.") return False return True
def create_fort_user(argv): """ 创建堡垒主机用户表 :param argv: :return: """ filename = argv[1] get_info = parser_file(filename) for user, info in get_info.items(): username = user if type(info) != dict: raise CommandError("\033[31;1mdata format error\033[0m") password = info.get('password') if not (user and password): # 插入的数据必须有这两个字段 raise CommandError("\033[31;1mdata format error\033[0m") insert_user = Account(username=username, password=password) session.add(insert_user)
def save_account(cls, account, password, category, owner, **kwargs): acc = Account() acc.account = account acc.password = password acc.category = category acc.owner = owner for k, v in kwargs.items(): if hasattr(acc, k): setattr(acc, k, v) db_session.add(acc) db_session.commit() return acc
def post(self, paste_uuid): # Just in case someone tries to get dirty with post requests, verify things here too. paste = Paste.find_by_uuid(paste_uuid) if paste is None: return {'error': 'Paste not found'}, 404 data = request.get_json(force=True) form = SubmitPasteForm.from_json(data) if not form.validate(): return {'errors': form.errors}, 401 identity = get_jwt_identity() current_user_id = Account.find_by_username(identity).id if paste.owner_id != current_user_id and not paste.open_edit: return {'error': 'You are not the owner of this paste, and open edit is not enabled for it.'}, 401 if paste.owner_id != current_user_id and paste.open_edit: # Restrict changes to the password, expiration date, and open edit settings if they are not # the paste owner. data['password'] = None data['open_edit'] = None data['expiration'] = None paste.update_paste(**data) return {'paste_uuid': paste.paste_uuid}, 200
def get(self, page): def strf_date(date): return date.strftime("%Y-%m-%d %H:%M:%S") if date is not None else None identity = get_jwt_identity() current_user = Account.find_by_username(identity) paste_pagination = current_user.pastes.paginate(int(page), 10, False) pastes = [] for paste in paste_pagination.items: pastes.append({ 'uuid': paste.paste_uuid, 'title': paste.title, 'language': paste.language, 'submission_date': strf_date(paste.submission_date), 'expiration_date': strf_date(paste.expiration_date), 'edit_date': strf_date(paste.edit_date), 'open_edit': paste.open_edit, 'password_protected': paste.password is not None }) return {'pastes': { 'current_page': paste_pagination.page, 'last_page': paste_pagination.pages, 'next_page_url': ('/api/paste/list/%i' % paste_pagination.next_num) if paste_pagination.has_next else None, 'prev_page_url': ('/api/paste/list/%i' % paste_pagination.prev_num) if paste_pagination.has_prev else None, 'data': pastes }}
def sync_account(account: Account): account.is_sync = True
def update_account_auth(oauth_token, oauth_token_secret, account: Account): account.oauth_token = oauth_token account.oauth_token_secret = oauth_token_secret
def transfer_between_accounts(from_account: Account, to_account: Account, amount): from_account.balance = from_account.balance - amount to_account.balance = to_account.balance + amount
def insert(uname, pw): user = Account(username=uname, password=pw) user.save()
def get(self): current_username = get_jwt_identity() user = Account.find_by_username(current_username) return {'username': current_username, 'userID': user.id, 'email': user.email}