def __init__(self): self.__acl = ACL() self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__notification_entity = NotificationEntity() self.__register_request_entity = RegisterRequestEntity() self.__task_core = TaskCore()
def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__task_entity = TaskEntity() self.__profile_entity = ProfileEntity() self.__app_name = self.__option_entity.get_value_by_key( "app_name").lower()
def __init__(self): self.__incident = IncidentEntity() self.__incident_update = IncidentUpdateEntity() self.__incident_update_notification = IncidentUpdateNotificationEntity( ) self.__incident_update_component = IncidentUpdateComponentEntity() self.__subscriber = SubscriberEntity() self.__user = UserEntity() self.__component = ComponentEntity() self.__component_group = ComponentGroupEntity() self.__metric = MetricEntity()
def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__data["AUTHOR"] = AUTHOR self.__data["COPYRIGHT"] = COPYRIGHT self.__data["LICENSE"] = LICENSE self.__data["VERSION"] = VERSION self.__data["MAINTAINER"] = MAINTAINER self.__data["EMAIL"] = EMAIL self.__data["STATUS"] = STATUS self.__data["REPO_URL"] = REPO_URL self.__data["AUTHOR_URL"] = AUTHOR_URL self.__data["RELEASES"] = RELEASES self.__data["SUPPORT_URL"] = SUPPORT_URL
def __init__(self): self.__reset_request_entity = ResetRequestEntity() self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__task_core = TaskCore() messages_count_option = self.__option_entity.get_one_by_key( "reset_mails_messages_count") reset_expire_option = self.__option_entity.get_one_by_key( "reset_mails_expire_after") if messages_count_option: self.__messages_count_option = int(messages_count_option.value) if reset_expire_option: self.__reset_expire_option = int(reset_expire_option.value)
class Statistics(): def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__task_entity = TaskEntity() self.__profile_entity = ProfileEntity() self.__app_name = self.__option_entity.get_value_by_key( "app_name").lower() def get_all_users(self): return { "type": "count", "record": "%s_all_users" % self.__app_name, "count": self.__user_entity.count_all(), "comment": "Current All Users on System" } def get_all_profiles(self): return { "type": "count", "record": "%s_all_profiles" % self.__app_name, "count": self.__profile_entity.count_all_profiles(), "comment": "Current All Profiles on System" } def get_all_tasks(self): return { "type": "count", "record": "%s_all_tasks" % self.__app_name, "count": self.__task_entity.count_all_tasks(), "comment": "Current All Tasks on System" }
class Login(): __option_entity = None __user_entity = None def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() def is_authenticated(self, request): if request.user and request.user.is_authenticated: return True else: return False def authenticate(self, username_email, password, request=None, with_login=True): is_email = False try: is_email = True if validate_email( username_email) is None else False except Exception: is_email = False if is_email: user = self.__user_entity.get_one_by_email(username_email) if user is not False and user.check_password(password) is True: if with_login: self.login(request, user) return True else: return False else: user = authenticate(request=request, username=username_email, password=password) if user is not None: if with_login: self.login(request, user) return True else: return False def login(self, request, user): return login(request, user)
class ResetPassword(): def __init__(self): self.__user_entity = UserEntity() self.__reset_request_entity = ResetRequestEntity() def check_token(self, token): request = self.__reset_request_entity.get_one_by_token(token) if request is not False and timezone.now() < request.expire_at: return True return False def reset_password(self, token, new_password): request = self.__reset_request_entity.get_one_by_token(token) if request is not False and timezone.now() < request.expire_at: return self.__user_entity.update_password_by_email(request.email, new_password) return False def delete_reset_request(self, token): return self.__reset_request_entity.delete_one_by_token(token)
class Context(): __data = {} __option_entity = None __user_entity = None def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__data["AUTHOR"] = AUTHOR self.__data["COPYRIGHT"] = COPYRIGHT self.__data["LICENSE"] = LICENSE self.__data["VERSION"] = VERSION self.__data["MAINTAINER"] = MAINTAINER self.__data["EMAIL"] = EMAIL self.__data["STATUS"] = STATUS self.__data["REPO_URL"] = REPO_URL self.__data["AUTHOR_URL"] = AUTHOR_URL self.__data["RELEASES"] = RELEASES self.__data["SUPPORT_URL"] = SUPPORT_URL def push(self, new_data): self.__data.update(new_data) def load_options(self, options): options_to_load = {} for key in options.keys(): options_to_load[key] = options[key] if key not in self.__data.keys(): self.__data[key] = options[key] if len(options_to_load.keys()) > 0: new_options = self.__option_entity.get_many_by_keys( options_to_load.keys()) for option in new_options: self.__data[option.key] = option.value def autoload_options(self): options = self.__option_entity.get_many_by_autoload(True) for option in options: self.__data[option.key] = option.value def autoload_user(self, user_id): user_data = { "user_first_name": "", "user_last_name": "", "user_username": "", "user_email": "", "user_avatar": "" } if user_id is not None: user = self.__user_entity.get_one_by_id(user_id) if user is not False: user_data["user_first_name"] = user.first_name user_data["user_last_name"] = user.last_name user_data["user_username"] = user.username user_data["user_email"] = user.email user_data["user_avatar"] = Gravatar(user.email).get_image() self.__data.update(user_data) def get(self, key=None, default=None): if key is not None: return self.__data[key] if key in self.__data else default return self.__data
def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__token = Token() self.__profile_entity = ProfileEntity()
class Controller(): """Base Controller""" __helpers = None __form = None __logger = None __option_entity = OptionEntity() __user_entity = UserEntity() __data = { "AUTHOR": AUTHOR, "COPYRIGHT": COPYRIGHT, "LICENSE": LICENSE, "VERSION": VERSION, "MAINTAINER": MAINTAINER, "EMAIL": EMAIL, "STATUS": STATUS, "REPO_URL": REPO_URL, "AUTHOR_URL": AUTHOR_URL, "RELEASES": RELEASES, "SUPPORT_URL": SUPPORT_URL } def json(self, messages, payload={}, status="success", status_code=HTTPStatus.OK): response = {"status": status} # if validation messages if isinstance(messages, dict): errors = [] for input_key, error_list in messages.items(): for error in error_list: errors.append({"type": "error", "message": error}) messages = errors if not isinstance(messages, list): raise ServerError( _("Invalid messages type %s passed to controller.json") % (type(messages))) response["messages"] = messages # Change status to failure if one message has type error for message in messages: if message["type"] == "error": response["status"] = "failure" if len(payload) > 0: response["payload"] = payload return JsonResponse(response, status=status_code) def correlation(self, request): return request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" def logger(self, request): if not self.__logger: self.__helpers = self.helpers() self.__logger = self.__helpers.get_logger(__name__) return self.__logger def get_request_data(self, request, method, predicted): request_data = {} log_data = {} data_bag = request.POST if method.lower() == "post" else request.GET for key, default in predicted.items(): if "password" in key: log_data[key] = "<hidden>" if key in data_bag else default elif "token" in key: log_data[key] = "<hidden>" if key in data_bag else default else: log_data[key] = data_bag[key] if key in data_bag else default request_data[key] = data_bag[key] if key in data_bag else default self.logger(request).info( _("Required request data: %(data)s") % {"data": self.helpers().json_dumps(log_data)}) return request_data def helpers(self): if not self.__helpers: self.__helpers = Helpers() return self.__helpers def form(self): if not self.__form: self.__form = Form() self.__form.add_validator(ExtraRules()) return self.__form def load_options(self, options): options_to_load = {} for key in options.keys(): options_to_load[key] = options[key] if key not in self.__data.keys(): self.__data[key] = options[key] if len(options_to_load.keys()) > 0: new_options = self.__option_entity.get_many_by_keys( options_to_load.keys()) for option in new_options: self.__data[option.key] = option.value def autoload_options(self): options = self.__option_entity.get_many_by_autoload(True) for option in options: self.__data[option.key] = option.value def autoload_user(self, user_id): user_data = { "user_first_name": "", "user_last_name": "", "user_username": "", "user_email": "", "user_avatar": "" } if user_id is not None: user = self.__user_entity.get_one_by_id(user_id) if user is not False: user_data["user_first_name"] = user.first_name user_data["user_last_name"] = user.last_name user_data["user_username"] = user.username user_data["user_email"] = user.email user_data["user_avatar"] = Gravatar(user.email).get_image() self.__data.update(user_data) def context_push(self, new_data): self.__data.update(new_data) def context_get(self, key=None, default=None): if key is not None: return self.__data[key] if key in self.__data else default return self.__data
class Dashboard(): def __init__(self): self.__incident = IncidentEntity() self.__incident_update = IncidentUpdateEntity() self.__incident_update_notification = IncidentUpdateNotificationEntity( ) self.__incident_update_component = IncidentUpdateComponentEntity() self.__subscriber = SubscriberEntity() self.__user = UserEntity() self.__component = ComponentEntity() self.__component_group = ComponentGroupEntity() self.__metric = MetricEntity() def incidents_count(self): return self.__incident.count_all() def subscribers_count(self): return self.__subscriber.count_all() def components_count(self): return self.__component.count_all() def component_groups_count(self): return self.__component_group.count_all() def metrics_count(self): return self.__metric.count_all() def users_count(self): return self.__user.count_all() def notifications_count(self, status): return self.__incident_update_notification.count_by_status(status) def subscribers_chart(self, days=14): subscribers = self.__subscriber.count_over_days(days) points = self.__build_points(days) for subscriber in subscribers: key = str(subscriber["day"].strftime("%d-%m-%Y")) if key in points.keys(): points[key] = subscriber["count"] return ", ".join(str(x) for x in list(points.values())) def components_chart(self, days=14): components = self.__incident_update_component.count_over_days(days) points = self.__build_points(days) for component in components: key = str(component["day"].strftime("%d-%m-%Y")) if key in points.keys(): points[key] = component["count"] return ", ".join(str(x) for x in list(points.values())) def notifications_chart(self, status, days=14): notifications = self.__incident_update_notification.count_over_days( status, days) points = self.__build_points(days) for notification in notifications: key = str(notification["day"].strftime("%d-%m-%Y")) if key in points.keys(): points[key] = notification["count"] return ", ".join(str(x) for x in list(points.values())) def incidents_chart(self, days=14): incidents = self.__incident.count_over_days(days) points = self.__build_points(days) for incident in incidents: key = str(incident["day"].strftime("%d-%m-%Y")) if key in points.keys(): points[key] = incident["count"] return ", ".join(str(x) for x in list(points.values())) def get_open_incidents(self): incidents = self.__incident.get_by_status("open") incidents_list = [] for incident in incidents: incidents_list.append({ "id": incident.id, "name": incident.name, "uri": incident.uri, "status": incident.status.title(), "created_at": incident.created_at.strftime("%b %d %Y %H:%M:%S") }) return incidents_list def get_affected_components(self): affected_components = self.__incident_update_component.get_affected_components( 0) affected_components_list = [] for affected_component in affected_components: affected_components_list.append({ "id": affected_component.id, "name": affected_component.component.name, "type": affected_component.type.replace("_", " ").title(), "update_id": affected_component.incident_update.id, "incident_id": affected_component.incident_update.incident.id }) return affected_components_list def __build_points(self, days): i = days points = {} while i >= 0: last_x_days = timezone.now() - datetime.timedelta(i) points[str(last_x_days.strftime("%d-%m-%Y"))] = 0 i -= 1 return points
class User(): __notification_entity = None __option_entity = None __user_entity = None __acl = None __register_request_entity = None __task_core = None __register_expire_option = 24 def __init__(self): self.__acl = ACL() self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__notification_entity = NotificationEntity() self.__register_request_entity = RegisterRequestEntity() self.__task_core = TaskCore() def username_used(self, username): return False if self.__user_entity.get_one_by_username( username) is False else True def email_used(self, email): return False if self.__user_entity.get_one_by_email( email) is False else True def username_used_elsewhere(self, user_id, username): user = self.__user_entity.get_one_by_username(username) if user is False or user.id == user_id: return False return True def email_used_elsewhere(self, user_id, email): user = self.__user_entity.get_one_by_email(email) if user is False or user.id == user_id: return False return True def get_one_by_id(self, id): user = self.__user_entity.get_one_by_id(id) if not user: return False return { "id": user.id, "username": user.username, "first_name": user.first_name, "last_name": user.last_name, "email": user.email, "role": "admin" if user.is_superuser else "user", } def insert_one(self, user): return self.__user_entity.insert_one(user) def create_user(self, user_data): status = True user = self.__user_entity.insert_one({ "username": user_data["username"], "email": user_data["email"], "password": user_data["password"], "first_name": user_data["first_name"], "last_name": user_data["last_name"], "is_superuser": False, "is_active": True, "is_staff": False }) if user is not False: self.__acl.add_role_to_user("normal_user", user.id) status &= (user is not False) return status def update_one_by_id(self, id, user_data): return self.__user_entity.update_one_by_id(id, user_data) def check_register_request(self, token): request = self.__register_request_entity.get_one_by_token(token) if request is not False and timezone.now() < request.expire_at: return True return False def get_register_request_by_token(self, token): return self.__register_request_entity.get_one_by_token(token) def delete_register_request_by_token(self, token): return self.__register_request_entity.delete_one_by_token(token) def delete_register_request_by_email(self, email): return self.__register_request_entity.delete_one_by_email(email) def create_register_request(self, email, role): request = self.__register_request_entity.insert_one({ "email": email, "payload": json.dumps({"role": role}), "expire_after": self.__register_expire_option }) return request.token if request is not False else False def send_register_request_message(self, email, token): app_name = self.__option_entity.get_value_by_key("app_name") app_email = self.__option_entity.get_value_by_key("app_email") app_url = self.__option_entity.get_value_by_key("app_url") return self.__task_core.delay( "register_request_email", { "app_name": app_name, "app_email": app_email, "app_url": app_url, "recipient_list": [email], "token": token, "subject": _("%s Signup Invitation") % (app_name), "template": "mails/register_invitation.html", "fail_silently": False }, 1) def count_all(self): return self.__user_entity.count_all() def get_all(self, offset=None, limit=None): return self.__user_entity.get_all(offset, limit) def delete_one_by_id(self, id): return self.__user_entity.delete_one_by_id(id)
class Profile(): def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__token = Token() self.__profile_entity = ProfileEntity() def get_profile(self, user_id): profile_data = { "first_name": "", "last_name": "", "username": "", "email": "", "job_title": "", "company": "", "address": "", "github_url": "", "twitter_url": "", "facebook_url": "", "access_token": "", "refresh_token": "", "avatar": "" } user = self.__user_entity.get_one_by_id(user_id) profile = self.__profile_entity.get_profile_by_user_id(user_id) if user is not False: profile_data["first_name"] = user.first_name profile_data["last_name"] = user.last_name profile_data["username"] = user.username profile_data["email"] = user.email profile_data["avatar"] = Gravatar(user.email).get_image() if profile is not False: profile_data["job_title"] = profile.job_title profile_data["company"] = profile.company profile_data["address"] = profile.address profile_data["github_url"] = profile.github_url profile_data["twitter_url"] = profile.twitter_url profile_data["facebook_url"] = profile.facebook_url profile_data["access_token"] = profile.access_token profile_data["refresh_token"] = profile.refresh_token return profile_data def update_profile(self, user_id, user_data): user_data["user"] = user_id if self.__profile_entity.profile_exists(user_data["user"]): status = self.__profile_entity.update_profile(user_data) status &= self.__user_entity.update_one_by_id(user_data["user"], user_data) return status else: status = (self.__profile_entity.create_profile(user_data) is not False) status &= self.__user_entity.update_one_by_id(user_data["user"], user_data) return status def update_access_token(self, user_id): token = self.__token.generate_token() while self.__profile_entity.token_used(token) is not False: token = self.__token.generate_token() return token if self.__profile_entity.update_access_token(user_id, token) else False def update_refresh_token(self, user_id): token = self.__token.generate_token() while self.__profile_entity.token_used(token) is not False: token = self.__token.generate_token() return token if self.__profile_entity.update_refresh_token(user_id, token) else False def get_profile_by_access_token(self, access_token): return self.__profile_entity.get_profile_by_access_token(access_token) def change_password(self, user_id, password): return self.__user_entity.update_password_by_user_id(user_id, password) def restore_session(self, user_id, request): return update_session_auth_hash(request, self.__user_entity.get_one_by_id(user_id)) def validate_password(self, user_id, password): return self.__user_entity.validate_password_by_user_id(user_id, password) def update_user(self, user_id, user_data): return self.__user_entity.update_one_by_id(self, user_id, user_data) def username_used_elsewhere(self, user_id, username): user = self.__user_entity.get_one_by_username(username) return False if user is False or user.id == user_id else True def email_used_elsewhere(self, user_id, email): user = self.__user_entity.get_one_by_email(email) return False if user is False or user.id == user_id else True
class ForgotPassword(): __reset_expire_option = 24 __messages_count_option = 5 def __init__(self): self.__reset_request_entity = ResetRequestEntity() self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__task_core = TaskCore() messages_count_option = self.__option_entity.get_one_by_key( "reset_mails_messages_count") reset_expire_option = self.__option_entity.get_one_by_key( "reset_mails_expire_after") if messages_count_option: self.__messages_count_option = int(messages_count_option.value) if reset_expire_option: self.__reset_expire_option = int(reset_expire_option.value) def check_email(self, email): return True if self.__user_entity.get_one_by_email( email) is not False else False def reset_request_exists(self, email): return self.__reset_request_entity.get_one_by_email(email) def is_spam(self, request): if request.messages_count >= self.__messages_count_option and timezone.now( ) < request.expire_at: return True return False def update_request(self, request): # Delete Old Request self.__reset_request_entity.delete_one_by_id(request.id) # Create a Fresh Request if timezone.now() > request.expire_at: return self.create_request(request.email) # Create from the Old Request request = self.__reset_request_entity.insert_one({ "email": request.email, "expire_at": request.expire_at, "messages_count": request.messages_count + 1 }) return request.token if request is not False else False def create_request(self, email): request = self.__reset_request_entity.insert_one({ "email": email, "expire_after": self.__reset_expire_option, "messages_count": 0 }) return request.token if request is not False else False def send_message(self, email, token): app_name = self.__option_entity.get_value_by_key("app_name") app_email = self.__option_entity.get_value_by_key("app_email") app_url = self.__option_entity.get_value_by_key("app_url") user = self.__user_entity.get_one_by_email(email) return self.__task_core.delay( "forgot_password_email", { "app_name": app_name, "app_email": app_email, "app_url": app_url, "recipient_list": [email], "token": token, "subject": _("%s Password Reset") % (app_name), "template": "mails/reset_password.html", "fail_silently": False }, user.id)
def __init__(self): self.__user_entity = UserEntity() self.__reset_request_entity = ResetRequestEntity()
def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity()
class Install(): __options = [{ "key": "app_installed", "value": "true", "autoload": True }, { "key": "app_description", "value": "", "autoload": False }, { "key": "google_analytics_account", "value": "", "autoload": True }, { "key": "reset_mails_messages_count", "value": "5", "autoload": False }, { "key": "reset_mails_expire_after", "value": "24", "autoload": False }, { "key": "access_tokens_expire_after", "value": "48", "autoload": False }, { "key": "prometheus_token", "value": "", "autoload": False }, { "key": "newrelic_api_key", "value": "", "autoload": False }] __admin = { "username": "", "email": "", "password": "", "is_superuser": True, "is_active": True, "is_staff": False } def __init__(self): self.__option_entity = OptionEntity() self.__user_entity = UserEntity() self.__acl = ACL() def is_installed(self): return False if self.__option_entity.get_one_by_key( "app_installed") is False else True def set_app_data(self, name, email, url): self.__options.append({ "key": "app_name", "value": name, "autoload": True }) self.__options.append({ "key": "app_email", "value": email, "autoload": True }) self.__options.append({ "key": "app_url", "value": url, "autoload": True }) def set_admin_data(self, username, email, password): self.__admin["username"] = username self.__admin["email"] = email self.__admin["password"] = password def init_base_acl(self, user_id): self.__acl.truncate_default_permissions() self.__acl.new_role("super_admin") self.__acl.new_role("normal_user") self.__acl.new_permission( "Manage Settings", self.__acl.get_content_type_id("auth", "user"), "manage_settings") self.__acl.add_permission_to_role("super_admin", "manage_settings") self.__acl.add_role_to_user("super_admin", user_id) return True def install(self): execute_from_command_line(["manage.py", "migrate"]) status = True status &= self.__option_entity.insert_many(self.__options) user = self.__user_entity.insert_one(self.__admin) status &= (user is not False) if user is not False: status &= self.init_base_acl(user.id) return user.id if status else False def uninstall(self): self.__option_entity.truncate() self.__user_entity.truncate()