示例#1
0
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"
        }
示例#2
0
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
示例#3
0
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)