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 Install(View): template_name = 'templates/install.html' __context = None __install = None __option_entity = None __correlation_id = None def get(self, request): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" self.__context = Context() self.__install = InstallModule() self.__option_entity = OptionEntity() if self.__install.is_installed(): return redirect("app.web.login") self.__context.push({ "page_title": _("Installation · %s") % self.__option_entity.get_value_by_key( "app_name", os.getenv("APP_NAME", "Silverback")) }) return render(request, self.template_name, self.__context.get())
def globals(request): option_entity = OptionEntity() return { "google_account": option_entity.get_value_by_key("google_analytics_account", ""), "app_timezone": os.getenv("APP_TIMEZONE", "UTC"), "activate_notifications": os.getenv("ACTIVATE_NOTIFICATIONS", "false") == "true", }
class Settings(): def __init__(self): self.__option_entity = OptionEntity() def update_options(self, options): status = True for key, value in options.items(): status &= self.__option_entity.update_value_by_key(key, value) return status def get_value_by_key(self, key, default=""): return self.__option_entity.get_value_by_key(key, default)
class Install(View, Controller): """Install Page Controller""" template_name = 'templates/install.html' def get(self, request): self.__install = InstallModule() self.__option_entity = OptionEntity() if self.__install.is_installed(): return redirect("app.web.login") self.context_push({ "page_title": _("Installation · %s") % self.__option_entity.get_value_by_key( "app_name", os.getenv("APP_NAME", "Silverback")) }) return render(request, self.template_name, self.context_get())
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)
def notify_subscriber(notification_id): option_entity = OptionEntity() incident_update_notification_module = IncidentUpdateNotificationModule() app_name = option_entity.get_value_by_key("app_name") app_email = option_entity.get_value_by_key("app_email") app_url = option_entity.get_value_by_key("app_url") notification = incident_update_notification_module.get_one_by_id( notification_id) incident_update = notification["incident_update"] incident = notification["incident_update"].incident subscriber = notification["subscriber"] data = {} data["incident_uri"] = incident.uri data["incident_update_time"] = incident_update.datetime.strftime( "%b %d %Y %H:%M:%S") data["incident_type"] = incident_update.status.title() data["incident_update"] = markdown2.markdown(incident_update.message) if notification["status"] == "pending": # send the notification for the first time if subscriber.type == SubscriberModule.EMAIL: status = __deliver_email( app_name, app_email, app_url, [subscriber.email], _("%(app_name)s Incident Update: %(incident_name)s") % { "app_name": app_name, "incident_name": incident.name }, "mails/incident_update.html", data, False) elif subscriber.type == SubscriberModule.PHONE: status = __deliver_sms( app_name, subscriber.phone, "%s%s" % (app_url.strip("/"), reverse("app.web.incidents", kwargs={'uri': incident.uri}))) elif subscriber.type == SubscriberModule.ENDPOINT: status = __deliver_webhook(subscriber.endpoint, subscriber.auth_token, '{}' % ()) if status: # message sent incident_update_notification_module.update_one_by_id( notification["id"], {"status": "success"}) else: # message failed incident_update_notification_module.update_one_by_id( notification["id"], {"status": "failed"}) elif notification["status"] == "failed": # Retry to send the notification if subscriber.type == SubscriberModule.EMAIL: status = __deliver_email( app_name, app_email, app_url, [subscriber.email], _("%(app_name)s Incident Update: %(incident_name)s") % { "app_name": app_name, "incident_name": incident.name }, "mails/incident_update.html", data, False) elif subscriber.type == SubscriberModule.PHONE: status = __deliver_sms( app_name, subscriber.phone, "%s%s" % (app_url.strip("/"), reverse("app.web.incidents", kwargs={'uri': incident.uri}))) elif subscriber.type == SubscriberModule.ENDPOINT: status = __deliver_webhook(subscriber.endpoint, subscriber.auth_token, '{}' % ()) if status: # message sent again incident_update_notification_module.update_one_by_id( notification["id"], {"status": "success"}) else: print("skip subscriber#%s!" % subscriber.id) return {"status": "passed", "result": "{}", "notify_type": "passed"}
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)