def delete(self, user_id): args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": user = User.query.filter_by(id=user_id).first() if user: try: del_user = user.delete() return { "status": "deleted", "message": "User was deleted from database.", "user": del_user.to_json(), } except IntegrityError as e: print("deleting error", e.__class__) return { "status": "error", "message": "You can't delete this user. There may be an account associated with it!", } else: return { "status": "error", "message": "User id incorrect", } else: return status
def post(self): args = self.post_args.parse_args() status = check_token(args) if status["status"] == "success": user = User.query.filter_by(username=args["username"]).first() if not user: try: new_user = User( username=args["username"], email=args["email"], password=args["password"], ) new_user.save() return { "status": "created", "message": "User was created", "data": new_user.to_json(), } except Exception: return { "status": "error", "message": "Internal server error" } else: return {"status": "error", "message": "Username already exist"} else: return status
def get(self): log(log.INFO, "GET ad_login and password start") args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": log(log.DEBUG, "Token valid") password = secrets.token_urlsafe(6) while str.isalpha(password): password = secrets.token_urlsafe(6) isUnique = False while not isUnique: rand_int = secrets.randbelow(17575999) ad_login = account.ecc_encode(rand_int) acc = Account.query.filter_by(ad_login=ad_login).first() if not acc: isUnique = True result = {"ad_login": ad_login, "password": password} resp = { "status": "success", "message": "Account was deleted", "data": result, } return resp else: log(log.ERROR, "Token error: %s", status.message) return status
def get(self, account_id): log(log.INFO, "GET account by ID start") args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": log(log.DEBUG, "Token valid") account = Account.query.get(account_id) if account: log(log.DEBUG, "Account found") resp = { "status": "success", "message": "Account was found", "data": account.to_json(), } return resp else: log(log.ERROR, "Account not found") resp = { "status": "error", "message": "Account not found", } return resp else: log(log.ERROR, "Token error: %s", status.message) return status
def get(self): args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": return status else: return status
def delete(self, reseller_id): args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": reseller = Reseller.query.filter_by(id=reseller_id).first() if reseller: try: reseller.delete() return { "status": "success", "message": "Reseller was deleted from database.", "data": [r.to_json() for r in Reseller.query.all()], } except IntegrityError as e: print("deleting error", e.__class__) return { "status": "error", "message": "You can't delete this reseller. There may be an account associated with it!", } return { "status": "error", "message": "Reseller not found", } else: return status
def post(self): args = self.post_args.parse_args() status = check_token(args) if status["status"] == "success": reseller = Reseller.query.filter_by( resellername=args["resellername"]).first() if not reseller: try: new_reseller = Reseller(resellername=args["resellername"], email=args["email"]) new_reseller.save() return { "status": "success", "message": "Reseller was successfully created.", "data": [r.to_json() for r in Reseller.query.all()], } except Exception: return { "status": "error", "message": "Internal server error" } else: return {"status": "error", "message": "Reseller already exist"} else: return status
def get(self, account_id): log(log.INFO, "GET subscription by account id start") args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": log(log.DEBUG, "Token is valid") subscription = [ s.to_json() for s in Subscription.query.filter_by(account_id=account_id) ] if subscription: log(log.DEBUG, "Account has subscriptions") res = { "status": "success", "message": "There are subscriptions in this account", "data": subscription, } return res else: log(log.ERROR, "Incorrect query") res = { "status": "error", "message": "Incorrect query", } return res else: log(log.ERROR, "Token error: %s", status.message) return status
def get(self): args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": return [u.to_json() for u in User.query.all()] else: return status
def put(self, account_id): log(log.INFO, "PUT account by ID start") args = self.post_args.parse_args() status = check_token(args) log(log.DEBUG, "PUT account arguments: %s", args) if status["status"] == "success": log(log.DEBUG, "Token valid") account = Account.query.get(account_id) if account: log(log.DEBUG, "Account found") account.email = args["email"] account.ad_login = args["ad_login"] account.ad_password = args["ad_password"] account.created_by_id = args["created_by_id"] account.reseller_id = args["reseller_id"] account.sim = args["sim"] account.imei = args["imei"] account.comment = args["comment"] account.license_key = args["license_key"] account.acc_status = args["acc_status"] account.save() ldap.change_password(account.ad_login, account.ad_password) resp = { "status": "success", "message": "Account was updated", "data": [a.to_json() for a in Account.query.all()], } return resp else: log(log.ERROR, "No such account") return {"status": "error", "message": "No such account"} else: log(log.ERROR, "Token error: %s", status.message) return status
def get(self, user_id): args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": user = User.query.get(user_id) if user: return user.to_json() else: return {"status": "error", "message": "No such user"} else: return status
def get(self): args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": resp = { "status": "success", "message": "Resellers loaded", "data": [r.to_json() for r in Reseller.query.all()], } return resp else: return status
def post(self): log(log.INFO, "POST new account start") args = self.post_args.parse_args() log(log.DEBUG, "POST acc arguments %s", args) status = check_token(args) if status["status"] == "success": log(log.DEBUG, "Token valid") account = Account.query.filter_by( ad_login=args["ad_login"]).first() if not account: log(log.INFO, "New AD login is unique") try: # # create LDAP user ad_login = args["ad_login"] ad_password = args["ad_password"] ldap_user = ldap.add_user(ad_login) if not ldap_user: raise Exception("LDAP user not created!") ldap_user.reset_password(ad_password) # if not ldap_user.reset_password(ad_password): # raise Exception("LDAP user password don't created!") mdm.sync() new_account = Account() new_account.ad_login = ad_login new_account.ad_password = ad_password new_account.email = args["email"] new_account.created_by_id = args["created_by_id"] new_account.reseller_id = args["reseller_id"] new_account.sim = args["sim"] new_account.imei = args["imei"] new_account.comment = args["comment"] new_account.license_key = args["license_key"] new_account.acc_status = args["acc_status"] new_account.save() resp = { "status": "success", "message": "Account was created", "data": new_account.to_json(), } return resp except Exception as e: log(log.ERROR, "Internal error: %s", str(e)) return { "status": "error", "message": "Internal server error" } else: log(log.ERROR, "Account already exist") return {"status": "error", "message": "Account already exist"} else: log(log.ERROR, "Token error: %s", status.message) return status
def get(self): log(log.INFO, "GET all accounts start") args = self.get_args.parse_args() log(log.INFO, "GET arguments %s", args) status = check_token(args) if status["status"] == "success": log(log.DEBUG, "Token valid") resp = { "status": "success", "message": "Accounts loaded", "data": [a.to_json() for a in Account.query.all()], } log(log.DEBUG, "Data was prepared successfully!") return resp else: log(log.ERROR, "Token error: %s", status) return status
def post(self): log(log.INFO, "POST subscription start") args = self.post_args.parse_args() log(log.DEBUG, "subs args: %s", args) status = check_token(args) if status["status"] == "success": log(log.DEBUG, "Token is valid") account = Account.query.filter_by( ad_login=args["ad_login"]).first() if account: log(log.DEBUG, "Account was found") try: activeDate = datetime.fromtimestamp( int(args["activation_date"])) log(log.DEBUG, "Activation date from request: %s", activeDate) sub = Subscription() sub.subscr_type = Subscription.SubscriptionType( args["subscr_type"]) sub.subscr_period = Subscription.SubscriptionPeriod( args["subscr_period"]) sub.activated_at = activeDate sub.account_id = account.id sub.save() log(log.DEBUG, "Subscription was created") return { "status": "success", "message": "Subscription was created", "data": sub.to_json(), } except Exception as e: log(log.ERROR, "Internall error: %s", e) return { "status": "error", "message": str(e), } else: log(log.ERROR, "Wrong account id") return { "status": "error", "message": "Wrong account id", } else: log(log.ERROR, "Token error: %s", status.message) return status
def put(self, reseller_id): args = self.post_args.parse_args() status = check_token(args) if status["status"] == "success": reseller = Reseller.query.get(reseller_id) if reseller: reseller.resellername = args["resellername"] reseller.email = args["email"] reseller.save() return { "status": "success", "message": "Reseller was edited.", "data": [r.to_json() for r in Reseller.query.all()], } else: return {"status": "error", "message": "Internal server error"} else: return status
def put(self, user_id): args = self.post_args.parse_args() status = check_token(args) if status["status"] == "success": user = User.query.get(user_id) if user: user.username = args["username"] user.email = args["email"] user.password = args["password"] user.save() return { "status": "edited", "message": "User edited", "data": user.to_json(), } else: return {"status": "error", "message": "No such user"} else: return status
def get(self, reseller_id): args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": reseller = Reseller.query.get(reseller_id) if not reseller: resp = { "status": "error", "message": "Reseller does not exists", } return resp else: resp = { "status": "success", "message": "Reseller exists", "data": reseller.to_json(), } return resp else: return status
def get(self, account_id): args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": account = Account.query.get(account_id) if not account: return {"status": "error", "message": "Account not found"} if not account.mdm_device_id: for device in mdm.devices: if device.user: if device.user.name == account.ad_login: account.mdm_device_id = device.device_id account.save() break else: continue else: return {"status": "error", "message": "Device not found"} device = mdm[int(account.mdm_device_id)] device.alarm() return {"status": "success", "message": "Device detached!"} else: return status
def delete(self, account_id): log(log.INFO, "DELETE account by ID start") args = self.get_args.parse_args() status = check_token(args) if status["status"] == "success": log(log.DEBUG, "Token valid") account = Account.query.filter_by(id=account_id).first() if account is not None: log(log.DEBUG, "Account found") account.acc_status = Account.AccountStatus.CLOSED account.save() resp = { "status": "success", "message": "Account was deleted", "data": [a.to_json() for a in Account.query.all()], } return resp else: log(log.ERROR, "No such account") return {"status": "error", "message": "No such account"} else: log(log.ERROR, "Token error: %s", status.message) return status