Example #1
0
class Task(AttributedDocument):
    meta = {
        "db_alias":
        Database.backend,
        "strict":
        strict,
        "indexes": [
            "created",
            "started",
            "completed",
            {
                "name":
                "%s.task.main_text_index" % Database.backend,
                "fields": [
                    "$name",
                    "$id",
                    "$comment",
                    "$execution.model",
                    "$output.model",
                    "$script.repository",
                    "$script.entry_point",
                ],
                "default_language":
                "english",
                "weights": {
                    "name": 10,
                    "id": 10,
                    "comment": 10,
                    "execution.model": 2,
                    "output.model": 2,
                    "script.repository": 1,
                    "script.entry_point": 1,
                },
            },
        ],
    }

    id = StringField(primary_key=True)
    name = StrippedStringField(required=True,
                               user_set_allowed=True,
                               sparse=False,
                               min_length=3)

    type = StringField(required=True, choices=get_options(TaskType))
    status = StringField(default=TaskStatus.created,
                         choices=get_options(TaskStatus))
    status_reason = StringField()
    status_message = StringField()
    status_changed = DateTimeField()
    comment = StringField(user_set_allowed=True)
    created = DateTimeField(required=True, user_set_allowed=True)
    started = DateTimeField()
    completed = DateTimeField()
    published = DateTimeField()
    parent = StringField()
    project = StringField(reference_field=Project, user_set_allowed=True)
    output = EmbeddedDocumentField(Output, default=Output)
    execution: Execution = EmbeddedDocumentField(Execution, default=Execution)
    tags = ListField(StringField(required=True), user_set_allowed=True)
    system_tags = ListField(StringField(required=True), user_set_allowed=True)
    script = EmbeddedDocumentField(Script)
    last_worker = StringField()
    last_worker_report = DateTimeField()
    last_update = DateTimeField()
    last_iteration = IntField(default=DEFAULT_LAST_ITERATION)
    last_metrics = SafeMapField(
        field=SafeMapField(EmbeddedDocumentField(MetricEvent)))
Example #2
0
class Garden(Document):
    name = StringField()
    username = StringField(unique_with="name")
    location = GeoPointField()
    width = IntField()
    height = IntField()
Example #3
0
class Person(Document):
    name = StringField(required=True)
    age = IntField(min_value=1, required=True)
Example #4
0
class ConversationMessage(EmbeddedDocument):
  timestamp = IntField(required=True)
  message = StringField(required=True)
  owner = ReferenceField(User)
Example #5
0
class Page(Document):
    page_num = IntField()
    title = StringField(required=True, max_length=50)
    body = StringField(max_length=1250)
Example #6
0
class CASI(Request):

    full_name = 'Cancelación de Asignaturas'

    CN_ANSWER_NO_DILIGENTE = 'ND'
    CN_ANSWER_MOTIVOS_LABORALES = 'ML'
    CN_ANSWER_INFORMACION_FALSA = 'TR'
    CN_ANSWER_SOPORTES_NO_SOPORTAN = 'SN'
    CN_ANSWER_FALTA_DE_CONOCIMIENTO = 'FC'
    CN_ANSWER_ARGUMENTOS_INSUFICIENTES = 'AI'
    CN_ANSWER_INCOHERENTE_O_INCONSECUENTE = 'II'
    CN_ANSWER_OTRO = 'OT'
    CN_ANSWER_CHOICES = ((CN_ANSWER_NO_DILIGENTE, 'No diligente'),
                         (CN_ANSWER_MOTIVOS_LABORALES,
                          'Motivos Laborales'), (CN_ANSWER_INFORMACION_FALSA,
                                                 'Información Falsa'),
                         (CN_ANSWER_SOPORTES_NO_SOPORTAN,
                          'Argumento cuando los soportes no soportan'),
                         (CN_ANSWER_FALTA_DE_CONOCIMIENTO,
                          'Falta de conocimiento'),
                         (CN_ANSWER_ARGUMENTOS_INSUFICIENTES,
                          'Argumentos insuficientes'),
                         (CN_ANSWER_INCOHERENTE_O_INCONSECUENTE,
                          'Incoherente o no consecuente'), (CN_ANSWER_OTRO,
                                                            'Otro'))

    subjects = EmbeddedDocumentListField(Subject,
                                         required=True,
                                         display='Asignaturas')
    advance = FloatField(required=True, display='% de Avance')
    enrolled_academic_periods = IntField(required=True,
                                         display='# Periodos Matriculados')
    papa = FloatField(required=True, display='PAPA')
    available_credits = IntField(required=True, display='Creditos Disponibles')
    current_credits = IntField(required=True, display='Creditos Inscritos')
    nrc_answer = StringField(choices=CN_ANSWER_CHOICES,
                             default=CN_ANSWER_OTRO,
                             display='Motivo de rechazo')

    regulation_list = ['008|2008|CSU']  # List of regulations

    str_cm = [
        'cancelar la(s) siguiente(s) asignatura(s) inscrita(s) del periodo académico {}',
        'porque {}justifica debidamente la solicitud.'
    ]

    str_pcm = [
        'SIA: Porcentaje de avance en el plan: {}. Número de matrículas: {}. PAPA: {}.',
        'SIA: Créditos disponibles: {}.',
        'SIA: Al aprobar la cancelación de la asignatura {} ({}) el estudiante quedaría con {} cr'
        + 'éditos inscritos.'
    ]

    str_pcmap = [
        ' cancelar la(s) siguiente(s) asignatura(s) inscrita(s) del periodo académico {}, ',
    ]

    str_pcma_cap = ['porque se justifica debidamente la solicitud.']

    str_pcma_cna = [
        'porque no existe coherencia entre la documentación y justificación que presenta.',
        'porque lo expuesto es un hecho de su conocimiento desde el inicio del periodo académico'
        +
        '; tuvo la oportunidad de resolverlo oportunamente hasta el 50% del periodo académico, p'
        +
        'or tanto, no constituye causa extraña que justifique la cancelación de la(s) asignatura'
        + '(s).',
        'porque de acuerdo con la documentación que presenta, su situación laboral no le impide '
        +
        'asistir a las clases y tiene el tiempo suficiente para responder por las actividades ac'
        + 'adémicas de la(s) asignatura(s). ',
        'porque verificada la información de los soportes, se encontró que el contenido de los m'
        + 'ismos no coincide con lo que en ellos se afirma.',
        'poque es responsabilidad del estudiante indagar sobre el conocimiento requerido y la pr'
        +
        'eparación necesaria para cursar la(s) asignatura(s) antes de inscribir.',
        'porque lo expuesto no es un hecho que constituya causa extraña que justifique la cancel'
        + 'ación de la(s) asignatura(s).',
        'porque de la documentación aportada, se tiene que no hay justificación para acceder a l'
        + 'o pedido. '
    ]

    def cm(self, docx):
        paragraph = docx.add_paragraph()
        paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
        paragraph.paragraph_format.space_after = Pt(0)
        self.cm_answer(paragraph)
        table_subjects(docx, Subject.subjects_to_array(self.subjects))

    def cm_answer(self, paragraph):
        paragraph.add_run(self.str_council_header + ' ')
        paragraph.add_run(
            # pylint: disable=no-member
            self.get_approval_status_display().upper() + ' ').font.bold = True
        paragraph.add_run(self.str_cm[0].format(self.academic_period) + ', ')
        if self.is_affirmative_response_approval_status():
            self.cm_ap(paragraph)
        else:
            self.cm_na(paragraph)
        paragraph.add_run('({}).'.format(self.regulations['008|2008|CSU'][0]))

    def pcm(self, docx):
        self.pcm_analysis(docx)
        paragraph = docx.add_paragraph()
        paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
        paragraph.paragraph_format.space_after = Pt(0)
        paragraph.add_run(self.str_answer + ': ').bold = True
        self.pcm_answer(paragraph)
        table_subjects(docx, Subject.subjects_to_array(self.subjects))

    def pcm_answer(self, paragraph):
        paragraph.add_run(self.str_comittee_header)
        paragraph.add_run(
            # pylint: disable=no-member
            ' ' + self.get_advisor_response_display().upper()).font.bold = True
        paragraph.add_run(self.str_pcmap[0].format(self.academic_period))
        if self.is_affirmative_response_advisor_response():
            self.pcm_answers_cr(paragraph)
        else:
            self.pcm_answers_cn(paragraph)

    def cm_ap(self, paragraph):
        paragraph.add_run(self.str_cm[1].format('') + ' ')

    def cm_na(self, paragraph):
        paragraph.add_run(self.str_cm[1].format('no ') + ' ')

    def pcm_analysis(self, docx):
        analysis_list = []
        analysis_list += [
            self.str_pcm[0].format(self.advance,
                                   self.enrolled_academic_periods, self.papa)
        ]
        analysis_list += [self.str_pcm[1].format(self.available_credits)]
        analysis_list += self.pcm_analysis_subject_list()
        analysis_list += self.extra_analysis
        add_analysis_paragraph(docx, analysis_list)

    def pcm_analysis_subject_list(self):
        analysis_subject_list = []
        for subject in self.subjects:
            current_credits = self.current_credits
            subject_credits = subject.credits
            subject_info = {
                'remaining': int(current_credits) - int(subject_credits),
                'code': subject.code,
                'name': subject.name
            }
            analysis_subject_list += [
                self.str_pcm[2].format(subject_info['name'],
                                       subject_info['code'],
                                       subject_info['remaining'])
            ]
        return analysis_subject_list

    def pcm_answers_cr(self, paragraph):
        paragraph.add_run(self.str_pcma_cap[0])
        paragraph.add_run(' ({}).'.format(self.regulations['008|2008|CSU'][0]))

    def pcm_answers_cn(self, paragraph):
        if self.nrc_answer == self.CN_ANSWER_INCOHERENTE_O_INCONSECUENTE:
            paragraph.add_run(self.str_pcma_cna[0] + ' ')
        elif self.nrc_answer == self.CN_ANSWER_NO_DILIGENTE:
            paragraph.add_run(self.str_pcma_cna[1] + ' ')
        elif self.nrc_answer == self.CN_ANSWER_MOTIVOS_LABORALES:
            paragraph.add_run(self.str_pcma_cna[2] + ' ')
        elif self.nrc_answer == self.CN_ANSWER_INFORMACION_FALSA:
            paragraph.add_run(self.str_pcma_cna[3] + ' ')
        elif self.nrc_answer == self.CN_ANSWER_FALTA_DE_CONOCIMIENTO:
            paragraph.add_run(self.str_pcma_cna[4] + ' ')
        elif self.nrc_answer == self.CN_ANSWER_ARGUMENTOS_INSUFICIENTES:
            paragraph.add_run(self.str_pcma_cna[5] + ' ')
        elif self.nrc_answer == self.CN_ANSWER_SOPORTES_NO_SOPORTAN:
            paragraph.add_run(self.str_pcma_cna[6] + ' ')
        elif self.nrc_answer == self.CN_ANSWER_OTRO:
            paragraph.add_run(self.str_pcma_cna[6] + ' ')
        else:
            raise AssertionError(
                self.assertionerror['CHOICES'].format('NRC_answer'))
        paragraph.add_run(' ({}).'.format(self.regulations['008|2008|CSU'][0]))
Example #7
0
class UserRate(Document):
    uid=IntField(required=True,default=0)
    mediaid=IntField(required=True,default=0)
    score = FloatField(required=True)
    ratedate = DateTimeField(default=datetime.utcnow)
Example #8
0
class TeamWork(EmbeddedDocument):
    id = UUIDField(required=True)
    description = StringField()
    type = IntField(required=True)  # see TEAM_SHOW_TYPE
    uri = StringField()
    create_time = DateTimeField(default=get_now())
Example #9
0
class TeamScore(EmbeddedDocument):
    type = IntField(default=0)
    score = IntField(required=True, min_value=0)
    reason = StringField()
    score_date = DateTimeField(default=get_now())
    judge = ReferenceField(User)
Example #10
0
class NetworkConfigTemplate(HDocumentBase):
    """ Network config for generate K8s yml"""

    name = StringField(required=True, unique=True)
    protocol = StringField()
    port = IntField(max_value=65535)
Example #11
0
class HackathonStat(HDocumentBase):
    type = StringField()  # class HACKATHON_STAT
    count = IntField(min_value=0)
    hackathon = ReferenceField(Hackathon)
class River(Document):
    name = StringField()
    continent = StringField()
    length = IntField()
class MongoEvent(Document):
    server_ip = StringField()  # track server that the event belongs to
    key = StringField()
    value = StringField()
    event_type = StringField()
    version = SequenceField()  # use 'version' as the auto increase id
    create_time = IntField()
    context = StringField()
    namespace = StringField()
    uuid = StringField()

    def to_dict(self):
        return {
            "key": self.key,
            "value": self.value,
            "event_type": self.event_type,
            "version": int(self.version),
            "create_time": self.create_time,
            "context": self.context,
            "namespace": self.namespace
        }

    def to_base_event(self):
        base_event = BaseEvent(**self.to_dict())
        return base_event

    @classmethod
    def convert_to_base_events(cls, mongo_events: list = None):
        if not mongo_events:
            return []
        base_events = []
        for mongo_event in mongo_events:
            base_events.append(mongo_event.to_base_event())
        return base_events

    @classmethod
    def get_base_events_by_version(cls,
                                   start_version: int,
                                   end_version: int = None):
        conditions = dict()
        conditions["version__gt"] = start_version
        if end_version is not None and end_version > 0:
            conditions["version__lte"] = end_version
        mongo_events = cls.objects(**conditions).order_by("version")
        return cls.convert_to_base_events(mongo_events)

    @classmethod
    def get_base_events(cls,
                        key: Tuple[str],
                        version: int = None,
                        event_type: str = None,
                        start_time: int = None,
                        namespace: str = None):
        conditions = dict()
        if len(key) == 1:
            conditions["key"] = key[0]
        elif len(key) > 1:
            conditions["key__in"] = list(key)
        if version is not None and version > 0:
            conditions["version__gt"] = version
        if event_type is not None:
            conditions["event_type"] = event_type
        if start_time is not None and start_time > 0:
            conditions["start_time_gte"] = start_time
        conditions["namespace"] = namespace
        mongo_events = cls.objects(**conditions).order_by("version")
        return cls.convert_to_base_events(mongo_events)

    @classmethod
    def get_base_events_by_time(cls, create_time: int = None):
        mongo_events = cls.objects(
            create_time__gte=create_time).order_by("version")
        return cls.convert_to_base_events(mongo_events)

    @classmethod
    def get_by_key(cls,
                   key: str = None,
                   start: int = None,
                   end: int = None,
                   sort_key: str = "version"):
        if key:
            return cls.objects(key=key).order_by(sort_key)[start:end]
        else:
            raise Exception("key is empty, please provide valid key")

    @classmethod
    def delete_by_client(cls, server_ip):
        cls.objects(server_ip=server_ip).delete()
Example #14
0
class Campaign(CritsBaseAttributes, CritsActionsDocument, Document):
    """
    Campaign Class.
    """

    meta = {
        "collection": settings.COL_CAMPAIGNS,
        "crits_type": 'Campaign',
        "latest_schema_version": 3,
        "schema_doc": {
            'schema_version':
            'Version of the Campaign schema doc',
            'active':
            'Enabled in the UI (on/off)',
            'aliases':
            'List [] of aliases this Campaign goes by',
            'domain_count':
            'Domains tagged with Campaign. Added by MapReduce',
            'email_count':
            'Emails tagged with Campaign. Added by MapReduce',
            'event_count':
            'Events tagged with Campaign. Added by MapReduce',
            'indicator_count': ('Indicators tagged with Campaign. Added by '
                                'MapReduce'),
            'ip_count':
            'IPs tagged with Campaign. Added by MapReduce',
            'name':
            'Name this Campaign goes by',
            'pcap_count':
            'PCAPs tagged with Campaign. Added by MapReduce',
            'sample_count':
            'Samples tagged with Campaign. Added by MapReduce',
            'ttps':
            'List [] of TTPs this Campaign is associated with',
        },
        "jtable_opts": {
            'details_url':
            'crits.campaigns.views.campaign_details',
            'details_url_key':
            'name',
            'default_sort':
            "name ASC",
            'searchurl':
            'crits.campaigns.views.campaigns_listing',
            'fields': [
                "name", "aliases", "actor_count", "backdoor_count",
                "exploit_count", "indicator_count", "email_count",
                "domain_count", "sample_count", "event_count", "ip_count",
                "pcap_count", "modified", "id", "status"
            ],
            'jtopts_fields': [
                "details", "name", "aliases", "status", "actors", "backdoors",
                "exploits", "indicators", "emails", "domains", "samples",
                "events", "ips", "pcaps", "modified", "favorite", "id"
            ],
            'hidden_fields': [],
            'linked_fields': [],
            'details_link':
            'details',
            'no_sort': ['details']
        }
    }

    active = StringField(default="on")
    aliases = ListField(StringField(), default=[])
    actor_count = IntField(default=0)
    backdoor_count = IntField(default=0)
    domain_count = IntField(default=0)
    email_count = IntField(default=0)
    event_count = IntField(default=0)
    exploit_count = IntField(default=0)
    indicator_count = IntField(default=0)
    ip_count = IntField(default=0)
    name = StringField(default=0)
    pcap_count = IntField(default=0)
    sample_count = IntField(default=0)
    ttps = ListField(EmbeddedDocumentField(EmbeddedTTP), default=[])

    def migrate(self):
        """
        Migrate the Campaign to the latest schema version.
        """

        migrate_campaign(self)

    def activate(self):
        """
        Set the Campaign as active.
        """

        self.active = "on"

    def deactivate(self):
        """
        Set the Campaign as inactive.
        """

        self.active = "off"

    def edit_description(self, description):
        """
        Set the Campaign description.

        :param description: The campaign description.
        :type description: str

        """

        self.description = description

    def edit_name(self, name):
        """
        Change the Campaign name.

        :param name: The new campaign name.
        :type name: str

        """

        self.name = name

    def add_alias(self, alias):
        """
        Add a Campaign alias.

        :param alias: The campaign alias(es)
        :type alias: string or list of strings.

        """

        if isinstance(alias, basestring):
            alias = [alias]
        for a in alias:
            if a not in self.aliases and isinstance(a, basestring):
                self.aliases.append(a)

    def remove_alias(self, alias):
        """
        Remove a Campaign alias.

        :param alias: The alias to remove.
        :type alias: str

        """

        self.aliases.remove(alias)

    def set_aliases(self, aliases):
        """
        Set the Campaign aliases to a specified list.

        :param aliases: The alias to set.
        :type alias: list

        """

        if isinstance(aliases, list):
            self.aliases = aliases

    def get_aliases(self):
        """
        Get the list of Campaign aliases.

        :returns: list of aliases.

        """

        return [alias for alias in self._data['aliases']]

    def add_ttp(self, ttp_item):
        """
        Add a TTP to this Campaign.

        :param ttp_item: The TTP to add.
        :type ttp_item: EmbeddedTTP

        """

        if isinstance(ttp_item, EmbeddedTTP):
            found = False
            for ttp in self.ttps:
                if ttp.ttp == ttp_item.ttp:
                    found = True
            if not found:
                self.ttps.append(ttp_item)

    def edit_ttp(self, old_ttp=None, new_ttp=None):
        """
        Edit an existing TTP for this Campaign.

        :param old_ttp: The old TTP value.
        :type old_ttp: str
        :param new_ttp: The new TTP value.
        :type new_ttp: str

        """

        if old_ttp and new_ttp:
            for c, ttp in enumerate(self.ttps):
                if ttp.ttp == old_ttp:
                    self.ttps[c].ttp = new_ttp

    def remove_ttp(self, ttp_value=None):
        """
        Remove a TTP from this Campaign.

        :param ttp_value: The TTP value to remove.
        :type ttp_value: str

        """

        if ttp_value:
            for c, ttp in enumerate(self.ttps):
                if ttp_value == ttp.ttp:
                    self.ttps.remove(ttp)
                    break
Example #15
0
class EmbeddedStatus(EmbeddedDocument):
    status = IntField()
    operated_at = DateTimeField()
Example #16
0
class TeamMember(EmbeddedDocument):
    join_time = DateTimeField()
    status = IntField()  # 0:unaudit ,1:audit_passed, 2:audit_refused
    user = ReferenceField(User)
Example #17
0
class EBAP(Request):

    full_name = 'Eliminación de la historia académica BAPI'

    commite_cm = IntField(default=1, display='Acta de comité')
    commite_cm_date = DateField(display='Fecha acta de comité',
                                default=datetime.date.today)

    regulation_list = ['008|2008|CSU']  # List of regulations

    str_cm = [
        'eliminar la historia académica BAPI, debido a que {}realiza debidamente la solicitud.',
    ]

    str_pcm = [
        'Modalidad de trabajo de grado: Asignaturas de posgrado. Acta de comité {}, del {} de {} '
        + 'del {}.'
    ]

    def cm(self, docx):
        paragraph = docx.add_paragraph()
        paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
        paragraph.paragraph_format.space_after = Pt(0)
        self.cm_answer(paragraph)
        paragraph.add_run(' ({}). '.format(
            self.regulations[self.regulation_list[0]][0]))

    def cm_answer(self, paragraph):
        paragraph.add_run(self.str_council_header + ' ')
        paragraph.add_run(
            # pylint: disable=no-member
            self.get_approval_status_display().upper() + ' ').font.bold = True
        paragraph.add_run(self.str_cm[0].format(
            '' if self.is_affirmative_response_approval_status() else 'no ') +
                          '.')

    def pcm(self, docx):
        self.pcm_analysis(docx)
        paragraph = docx.add_paragraph()
        paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
        paragraph.paragraph_format.space_after = Pt(0)
        paragraph.add_run(self.str_answer + ': ').bold = True
        self.pcm_answer(paragraph)

    def pcm_analysis(self, docx):
        analysis_list = []
        analysis_list += [
            self.str_pcm[0].format(
                # pylint: disable=no-member
                self.commite_cm,
                self.commite_cm_date.day,
                num_to_month(self.commite_cm_date.month),
                self.commite_cm_date.year)
        ]
        analysis_list += self.extra_analysis
        add_analysis_paragraph(docx, analysis_list)

    def pcm_answer(self, paragraph):
        paragraph.add_run(self.str_comittee_header + ' ')
        paragraph.add_run(
            # pylint: disable=no-member
            self.get_advisor_response_display().upper() + ' ').font.bold = True
        paragraph.add_run(self.str_cm[0].format(
            '' if self.is_affirmative_response_advisor_response() else 'no '))
Example #18
0
class AzureEndPoint(DynamicEmbeddedDocument):
    name = StringField()
    protocol = StringField()
    public_port = IntField()
    private_port = IntField()
    url = StringField()
Example #19
0
class CRITsConfig(CritsDocument, Document):
    """
    CRITs Configuration Class.
    """

    from django.conf import settings

    meta = {
        "collection": settings.COL_CONFIG,
        "crits_type": 'Config',
        "latest_schema_version": 1,
        "schema_doc": {
        },
    }

    allowed_hosts = ListField(StringField(), default=['*'])
    classification = StringField(default='unclassified')
    company_name = StringField(default='My Company')
    create_unknown_user = BooleanField(default=False)
    crits_message = StringField(default='')
    crits_email = StringField(default='')
    crits_email_subject_tag = StringField(default='')
    crits_email_end_tag = BooleanField(default=True)
    # This is actually the internal DB version, but is named crits_version
    # for historical reasons.
    crits_version = StringField(required=True,
                                default=settings.CRITS_VERSION)
    debug = BooleanField(default=True)
    depth_max = IntField(default=10)
    email_host = StringField(default='')
    email_port = StringField(default='')
    enable_api = BooleanField(default=False)
    enable_toasts = BooleanField(default=False)
    git_repo_url = StringField(default='https://github.com/crits/crits')
    http_proxy = StringField(default='')
    instance_name = StringField(default='My Instance')
    instance_url = StringField(default='')
    invalid_login_attempts = IntField(default=3)
    language_code = StringField(default='en-us')
    ldap_auth = BooleanField(default=False)
    ldap_tls = BooleanField(default=False)
    ldap_server = StringField(default='')
    ldap_usercn = StringField(default='')
    ldap_userdn = StringField(default='')
    ldap_update_on_login = BooleanField(default=False)
    log_directory = StringField(default=os.path.join(settings.SITE_ROOT, 'logs'))
    log_level = StringField(default='INFO')
    password_complexity_desc = StringField(default='8 characters, at least 1 capital, 1 lowercase and 1 number/special')
    password_complexity_regex = StringField(default='(?=^.{8,}$)((?=.*\d)|(?=.*\W+))(?![.\n])(?=.*[A-Z])(?=.*[a-z]).*$')
    query_caching = BooleanField(default=False)
    rar_path = StringField(default='/usr/bin/unrar')
    rel_max = IntField(default=50)
    remote_user = BooleanField(default=False)
    rt_url = StringField(default='')
    secure_cookie = BooleanField(default=True)
    service_dirs = ListField(StringField())
    service_model = StringField(default='process')
    service_pool_size = IntField(default=12)
    session_timeout = IntField(default=12)
    splunk_search_url = StringField(default='')
    temp_dir = StringField(default='/tmp')
    timezone = StringField(default='America/New_York')
    total_max = IntField(default=250)
    totp_web = StringField(default='Disabled')
    totp_cli = StringField(default='Disabled')
    zip7_path = StringField(default='/usr/bin/7za')

    def migrate(self):
        """
        Migrate the Configuration Schema to the latest version.
        """

        pass
Example #20
0
class RDEF(Request):

    full_name = 'Retiro definitivo del programa'

    advance = FloatField(required=True, display='% de Avance', default=0.0)
    enrolled_academic_periods = IntField(
        required=True, display='# Periodos Matriculados', default=0)
    papa = FloatField(required=True, display='PAPA', default=0.0)

    regulation_list = []  # List of regulations

    str_cm = ['presentar con concepto positivo a la División de Registro y Matrícula, ' +
              'el retiro voluntario del programa {} ({})',
              'debido a que']

    list_analysis = [
        'SIA: Porcentaje de avance en el plan: {}%',
        'SIA: Número de matrículas: {}',
        'SIA: P.A.P.A.: {}'
    ]

    def cm(self, docx):
        paragraph = docx.add_paragraph()
        paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
        paragraph.paragraph_format.space_after = Pt(0)
        self.cm_answer(paragraph)

    def cm_answer(self, paragraph):
        paragraph.add_run(self.str_council_header + ' ')
        # pylint: disable=no-member
        paragraph.add_run(
            self.get_approval_status_display().upper() + ' ').font.bold = True
        paragraph.add_run(self.str_cm[0].format(
            self.get_academic_program_display(), self.academic_program))
        if not self.is_affirmative_response_approval_status():
            paragraph.add_run(
                ' ' + self.str_cm[1] + ' ' + self.council_decision)
        paragraph.add_run('.')

    def pcm(self, docx):
        self.pcm_analysis(docx)
        self.pcm_answer(docx)

    def pcm_answer(self, docx):
        paragraph = docx.add_paragraph()
        paragraph.alignment = WD_ALIGN_PARAGRAPH.JUSTIFY
        paragraph.paragraph_format.space_after = Pt(0)
        paragraph.add_run(self.str_answer + ': ').font.bold = True
        paragraph.add_run(self.str_comittee_header + ' ')
        # pylint: disable=no-member
        paragraph.add_run(
            self.get_advisor_response_display().upper() + ' ').font.bold = True
        paragraph.add_run(self.str_cm[0].format(
            self.get_academic_program_display(), self.academic_program))
        if not self.is_affirmative_response_approval_status():
            paragraph.add_run(
                ' ' + self.str_cm[1] + ' ' + self.council_decision)
        paragraph.add_run('.')

    def pcm_analysis(self, docx):
        final_analysis = []
        final_analysis += [self.list_analysis[0].format(self.advance)]
        final_analysis += [self.list_analysis[1]
                           .format(self.enrolled_academic_periods)]
        final_analysis += [self.list_analysis[2].format(self.papa)]
        for extra_a in self.extra_analysis:
            final_analysis += [extra_a]
        add_analysis_paragraph(docx, final_analysis)
Example #21
0
class Message(Document):
    topicid = IntField()
    comments = ListField(ReferenceField(Comment))
Example #22
0
class PasteFile(Document):
    id = SequenceField(primary_key=True)
    filename = StringField(max_length=5000, null=False)
    filehash = StringField(max_length=128, null=False, unique=True)
    filemd5 = StringField(max_length=128, null=False, unique=True)
    uploadtime = DateTimeField(null=False)
    mimetype = StringField(max_length=128, null=False)
    size = IntField(null=False)
    meta = {'collection': 'paste_file'}  # 自定义集合的名字

    def __init__(self,
                 filename='',
                 mimetype='application/octet-stream',
                 size=0,
                 filehash=None,
                 filemd5=None,
                 *args,
                 **kwargs):
        # 初始化父类的__init__方法
        super(PasteFile, self).__init__(filename=filename,
                                        mimetype=mimetype,
                                        size=size,
                                        filehash=filehash,
                                        filemd5=filemd5,
                                        *args,
                                        **kwargs)
        self.uploadtime = datetime.now()
        self.mimetype = mimetype
        self.size = int(size)
        self.filehash = filehash if filehash else self._hash_filename(filename)
        self.filename = filename if filename else self.filehash
        self.filemd5 = filemd5

    @staticmethod
    def _hash_filename(filename):
        _, _, suffix = filename.rpartition('.')
        return '%s.%s' % (uuid.uuid4().hex, suffix)

    @cached_property
    def symlink(self):
        return short_url.encode_url(self.id)

    @classmethod
    def get_by_symlink(cls, symlink, code=404):
        id = short_url.decode_url(symlink)
        return cls.objects.get_or_404(id=id)

    @classmethod
    def get_by_filehash(cls, filehash, code=404):
        return cls.objects.get_or_404(filehash=filehash)

    @classmethod
    def get_by_md5(cls, filemd5):
        rs = cls.objects(filemd5=filemd5)
        return rs[0] if rs else None

    @classmethod
    def create_by_upload_file(cls, uploaded_file):
        rst = cls(uploaded_file.filename, uploaded_file.mimetype, 0)
        uploaded_file.save(rst.path)
        with open(rst.path) as f:
            filemd5 = get_file_md5(f)
            uploaded_file = cls.get_by_md5(filemd5)
            if uploaded_file:
                os.remove(rst.path)
                return uploaded_file
        filestat = os.stat(rst.path)
        rst.size = filestat.st_size
        rst.filemd5 = filemd5
        return rst

    @classmethod
    def create_by_old_paste(cls, filehash):
        filepath = get_file_path(filehash)
        mimetype = magic.from_file(filepath, mime=True)
        filestat = os.stat(filepath)
        size = filestat.st_size

        rst = cls(filehash, mimetype, size, filehash=filehash)
        return rst

    @property
    def path(self):
        return get_file_path(self.filehash)

    def get_url(self, subtype, is_symlink=False):
        hash_or_link = self.symlink if is_symlink else self.filehash
        return 'http://{host}/{subtype}/{hash_or_link}'.format(
            subtype=subtype, host=request.host, hash_or_link=hash_or_link)

    @property
    def url_i(self):
        return self.get_url('i')

    @property
    def url_p(self):
        return self.get_url('p')

    @property
    def url_s(self):
        return self.get_url('s', is_symlink=True)

    @property
    def url_d(self):
        return self.get_url('d')

    @property
    def image_size(self):
        if self.is_image:
            im = Image.open(self.path)
            return im.size
        return (0, 0)

    @property
    def quoteurl(self):
        return urllib.quote(self.url_i)

    @classmethod
    def rsize(cls, old_paste, weight, height):
        assert old_paste.is_image, TypeError('Unsupported Image Type.')

        img = cropresize2.crop_resize(Image.open(old_paste.path),
                                      (int(weight), int(height)))

        rst = cls(old_paste.filename, old_paste.mimetype, 0)
        img.save(rst.path)
        filestat = os.stat(rst.path)
        rst.size = filestat.st_size
        return rst

    @property
    def is_image(self):
        return self.mimetype in IMAGE_MIMES

    @property
    def is_audio(self):
        return self.mimetype in AUDIO_MIMES

    @property
    def is_video(self):
        return self.mimetype in VIDEO_MIMES

    @property
    def is_pdf(self):
        return self.mimetype == 'application/pdf'

    @property
    def type(self):
        for t in ('image', 'pdf', 'video', 'audio'):
            if getattr(self, 'is_' + t):
                return t
        return 'binary'
Example #23
0
class System(Document, BrewtilsSystem):
    """Mongo-Backed BREWMASTER System Object"""

    name = StringField(required=True)
    description = StringField()
    version = StringField(required=True)
    max_instances = IntField(default=1)
    instances = ListField(ReferenceField(Instance, reverse_delete_rule=PULL))
    commands = ListField(ReferenceField(Command, reverse_delete_rule=PULL))
    icon_name = StringField()
    display_name = StringField()
    metadata = DictField()

    meta = {
        "auto_create_index":
        False,  # We need to manage this ourselves
        "index_background":
        True,
        "indexes": [{
            "name": "unique_index",
            "fields": ["name", "version"],
            "unique": True
        }],
    }

    def __str__(self):
        return BrewtilsSystem.__str__(self)

    def __repr__(self):
        return BrewtilsSystem.__repr__(self)

    def clean(self):
        """Validate before saving to the database"""

        if len(self.instances) > self.max_instances > -1:
            raise ModelValidationError(
                "Can not save System %s: Number of instances (%s) "
                "exceeds system limit (%s)" %
                (str(self), len(self.instances), self.max_instances))

        if len(self.instances) != len(
                set(instance.name for instance in self.instances)):
            raise ModelValidationError(
                "Can not save System %s: Duplicate instance names" % str(self))

    def upsert_commands(self, commands):
        """Updates or inserts a list of commands.

        Assumes the commands passed in are more important than what is currently in the db. It
        will delete commands that are not listed in the dictionary.

        :param commands: The list of new commands
        :return: None
        """
        old_commands = Command.objects(system=self)
        old_names = {command.name: command.id for command in old_commands}

        new_commands = copy.copy(commands)
        for command in new_commands:
            # If this command is already in the DB we want to preserve the ID
            if command.name in list(old_names.keys()):
                command.id = old_names[command.name]

            command.system = self
            command.save()

        # Clean up orphan commands
        new_names = [command.name for command in new_commands]
        for command in old_commands:
            if command.name not in new_names:
                command.delete()

        self.commands = new_commands
        self.save()

    def deep_save(self):
        """Deep save. Saves Commands, Instances, and the System

        Mongoengine cannot save bidirectional references in one shot because
        'You can only reference documents once they have been saved to the database'
        So we must mangle the System to have no Commands, save it, save the individual Commands
        with the System reference, update the System with the Command list, and then save the
        System again
        """

        # Note if this system is already saved
        delete_on_error = self.id is None

        # Save these off here so we can 'revert' in case of an exception
        temp_commands = self.commands
        temp_instances = self.instances

        try:
            # Before we start saving things try to make sure everything will validate correctly
            # This means multiple passes through the collections, but we want to minimize the
            # chances of having to bail out after saving something since we don't have transactions

            # However, we have to start by saving the System. We need it in the database so the
            # Commands will validate against it correctly (the ability to undo this is why we
            # saved off delete_on_error earlier) The reference lists must be empty or else we
            # encounter the bidirectional reference issue
            self.commands = []
            self.instances = []
            self.save()

            # Make sure all commands have the correct System reference
            for command in temp_commands:
                command.system = self

            # Now validate
            for command in temp_commands:
                command.validate()
            for instance in temp_instances:
                instance.validate()

            # All validated, now save everything
            for command in temp_commands:
                command.save(validate=False)
            for instance in temp_instances:
                instance.save(validate=False)
            self.commands = temp_commands
            self.instances = temp_instances
            self.save()

        # Since we don't have actual transactions we are not in a good position here, try our
        # best to 'roll back'
        except Exception:
            self.commands = temp_commands
            self.instances = temp_instances
            if delete_on_error and self.id:
                self.delete()
            raise

    def deep_delete(self):
        """Completely delete a system"""
        self.delete_commands()
        self.delete_instances()
        return self.delete()

    def delete_commands(self):
        """Delete all commands associated with this system"""
        for command in self.commands:
            command.delete()

    def delete_instances(self):
        """Delete all instances associated with this system"""
        for instance in self.instances:
            instance.delete()

    @classmethod
    def find_unique(cls, name, version):
        """Find a unique system using its name and version

        :param name: The name
        :param version: The version
        :return: One system instance if found, None otherwise
        """
        try:
            return cls.objects.get(name=name, version=version)
        except DoesNotExist:
            return None
Example #24
0
class Certificate(CritsBaseAttributes, CritsSourceDocument, Document):
    """
    Certificate Class.
    """

    meta = {
        "collection": settings.COL_CERTIFICATES,
        "crits_type": 'Certificate',
        "latest_schema_version": 1,
        "schema_doc": {
            'filename': 'The filename of the certificate',
            'filetype': 'The filetype of the certificate',
            'md5': 'The MD5 of the certificate file',
            'size': 'The filesize of the certificate',
            'description': 'Description of what the certificate contains',
            'source': 'List [] of source information about who provided the certificate'
        },
        "jtable_opts": {
                         'details_url': 'crits.certificates.views.certificate_details',
                         'details_url_key': 'md5',
                         'default_sort': "modified DESC",
                         'searchurl': 'crits.certificates.views.certificates_listing',
                         'fields': [ "filename", "description", "filetype",
                                     "size", "modified", "source", "campaign",
                                     "id", "md5", "status" ],
                         'jtopts_fields': [ "details",
                                            "filename",
                                            "description",
                                            "filetype",
                                            "size",
                                            "modified",
                                            "source",
                                            "campaign",
                                            "status",
                                            "md5",
                                            "favorite",
                                            "id" ],
                         'hidden_fields': ["md5"],
                         'linked_fields': ["source", "campaign"],
                         'details_link': 'details',
                         'no_sort': ['details']
                       },
    }

    description = StringField()
    filedata = getFileField(collection_name=settings.COL_CERTIFICATES)
    filename = StringField(required=True)
    filetype = StringField(required=True)
    size = IntField(default=0)
    md5 = StringField()

    def migrate(self):
        """
        Migrate the Certificate tot he latest schema version.
        """
        pass

    def add_file_data(self, file_data):
        """
        Add the Certificate to GridFS.

        :param file_data: The Certificate.
        :type file_data: str
        """

        self._generate_file_metadata(file_data)
        self.filedata = file_data

    def add_file_obj(self, file_obj):
        """
        Add the Certificate to GridFS.

        :param file_obj: The Certificate.
        :type file_data: file handle
        """

        data = file_obj.read()
        self._generate_file_metadata(data)
        self.filedata = data

    def _generate_file_metadata(self, data):
        """
        Set the filetype, size, and MD5 of the Certificate.

        :param data: The Certificate.
        :type data: str
        """

        import magic
        from hashlib import md5
        self.filetype = magic.from_buffer(data)
        self.size = len(data)
        # this is a shard key. you can't modify it once it's set.
        # MongoEngine will still mark the field as modified even if you set it
        # to the same value.
        if not self.md5:
            self.md5 = md5(data).hexdigest()

    def discover_binary(self):
        """
        Queries GridFS for a matching binary to this Certificate document.
        """

        from crits.core.mongo_tools import mongo_connector

        fm = mongo_connector("%s.files" % self._meta['collection'])
        objectid = fm.find_one({'md5': self.md5}, {'_id': 1})
        if objectid:
            self.filedata.grid_id = objectid['_id']
            self.filedata._mark_as_changed()
class Nonce(Document, NonceMixin):
    """One use numbers"""
    server_url = StringField(max_length=255)
    timestamp = IntField()
    salt = StringField(max_length=40)
Example #26
0
class Target(CritsBaseAttributes, CritsActionsDocument, Document):
    """
    Target class.
    """

    meta = {
        "collection": settings.COL_TARGETS,
        "auto_create_index": False,
        "crits_type": 'Target',
        "latest_schema_version": 3,
        "schema_doc": {
            'department': 'Target department name',
            'division': 'Target division',
            'email_address': 'Target email address',
            'email_count': 'Emails destined for this user. Added by MapReduce',
            'organization_id': 'Target organization ID number',
            'firstname': 'Target first name',
            'lastname': 'Target last name',
            'title': 'Target job title',
            'note': 'Custom note about target'
        },
        "jtable_opts": {
                         'details_url': 'crits-targets-views-target_info',
                         'details_url_key': 'email_address',
                         'default_sort': "email_count DESC",
                         'searchurl': 'crits-targets-views-targets_listing',
                         'fields': [ "email_address","firstname", "lastname",
                                     "email_count", "department", "division",
                                     "status", "id"],
                         'jtopts_fields': [ "details",
                                            "email_address",
                                            "firstname",
                                            "lastname",
                                            "email_count",
                                            "department",
                                            "division",
                                            "status",
                                            "favorite",
                                            "id"],
                         'hidden_fields': [],
                         'linked_fields': [ "department", "division" ],
                         'details_link': 'details',
                         'no_sort': ['details']
                       }

    }

    email_address = EmailField(required=True)
    email_count = IntField(default=0)
    department = StringField()
    division = StringField()
    organization_id = StringField()
    firstname = StringField()
    lastname = StringField()
    title = StringField()
    note = StringField()

    def migrate(self):
        migrate_target(self)

    def find_emails(self, username):
        sources = user_sources(username)
        emails = Email.objects(to__iexact=self.email_address,
                               source__name__in=sources)
        return emails
Example #27
0
class ContentUnit(Document):
    """
    The base class for all content units.

    All classes inheriting from this class must override the unit_type_id and _ns to ensure
    they are populated properly.

    :ivar id: content unit id
    :type id: mongoengine.StringField
    :ivar last_updated: last time this unit was updated (since epoch, zulu time)
    :type last_updated: mongoengine.IntField
    :ivar user_metadata: Bag of User supplied data to go along with this unit
    :type user_metadata: mongoengine.DictField
    :ivar storage_path: Location on disk where the content associated with this unit lives
    :type storage_path: mongoengine.StringField

    :ivar _ns: (Deprecated), Contains the name of the collection this model represents
    :type _ns: mongoengine.StringField
    :ivar unit_type_id: content unit type
    :type unit_type_id: mongoengine.StringField
    """

    id = StringField(primary_key=True)
    last_updated = IntField(db_field='_last_updated', required=True)
    user_metadata = DictField(db_field='pulp_user_metadata')
    storage_path = StringField(db_field='_storage_path')

    # For backward compatibility
    _ns = StringField(required=True)
    unit_type_id = StringField(db_field='_content_type_id', required=True)

    meta = {
        'abstract': True,
    }

    _NAMED_TUPLE = None

    @classmethod
    def attach_signals(cls):
        """
        Attach the signals to this class.

        This is provided as a class method so it can be called on subclasses
        and all the correct signals will be applied.
        """
        signals.post_init.connect(cls.post_init_signal, sender=cls)
        signals.pre_save.connect(cls.pre_save_signal, sender=cls)
        # Create the named tuple here so it happens during server startup
        cls.NAMED_TUPLE = namedtuple(cls.unit_type_id.default,
                                     cls.unit_key_fields)

    @classmethod
    def post_init_signal(cls, sender, document):
        """
        The signal that is triggered before a unit is initialized

        This is used to validate that the unit_key_fields attribute is set properly

        :param sender: sender class
        :type sender: object
        :param document: Document that sent the signal
        :type document: ContentUnit
        :raises: PLP0035 if the unit_key_fields attribute has not been defined
        """
        if not hasattr(document, 'unit_key_fields'):
            class_name = type(document).__name__
            raise exceptions.PulpCodedException(error_codes.PLP0035,
                                                class_name=class_name)

    @classmethod
    def pre_save_signal(cls, sender, document, **kwargs):
        """
        The signal that is triggered before a unit is saved, this is used to
        support the legacy behavior of generating the unit id and setting
        the last_updated timestamp

        :param sender: sender class
        :type sender: object
        :param document: Document that sent the signal
        :type document: ContentUnit
        """
        if not document.id:
            document.id = str(uuid.uuid4())
        document.last_updated = dateutils.now_utc_timestamp()

    def get_repositories(self):
        """
        Get an iterable of Repository models for all the repositories that contain this unit

        :return: Repositories that contain this content unit
        :rtype: iterable of Repository
        """
        content_list = RepositoryContentUnit.objects(unit_id=self.id)
        id_list = [item.repo_id for item in content_list]
        return Repository.objects(repo_id__in=id_list)

    @property
    def unit_key(self):
        """
        Dictionary representation of the unit key
        """
        return dict((key, getattr(self, key)) for key in self.unit_key_fields)

    @property
    def unit_key_str(self):
        """
        The unit key represented as a string ordered by unit key fields alphabetically
        """
        return str(sorted([getattr(self, key)
                           for key in self.unit_key_fields]))

    @property
    def unit_key_as_named_tuple(self):
        """
        The unit key represented as a named_tuple by field name
        """
        return self.NAMED_TUPLE(**self.unit_key)

    def __hash__(self):
        """
        This should provide a consistent and unique hash where units of the same
        type and the same unit key will get the same hash value.
        """
        return hash(self.unit_type_id + self.unit_key_str)
Example #28
0
 def __init__(self, *args, **kwargs):
     super(ModelLabels, self).__init__(field=IntField(), *args, **kwargs)
Example #29
0
class MissionStep(Document):
    mission = ReferenceField(Mission)
    order = IntField(required=True)
    command = StringField(required=True, max_length=50)
    sleep = FloatField(required=False)
Example #30
0
class Category1(Document):
    primary = IntField(primary_key=True)
    name = StringField()