Beispiel #1
0
class PostLike(Document):
    member = ReferenceField(Member, required=True, description='글쓴이(member_id)')
    post = ReferenceField(Post, required=True, description='게시물 아이디')
    status = StringField(description='좋아요 상태')
 class GroupMembership(me.Document):
     user = ReferenceField(User)
     group = ReferenceField(Group)
Beispiel #3
0
class Comment(EmbeddedDocument):
    cid = ObjectIdField(default=lambda: ObjectId())
    author = ReferenceField(User)
    content = StringField()
    reply = ObjectIdField()
Beispiel #4
0
class Sockets(Document):
    client = ReferenceField(Client, required=True)
    fd = IntField(unique=True, required=True)
Beispiel #5
0
class User(Document, BaseEventumDocument):
    """A user model.

    The :class:`User` object is only created once the user logs in for the
    first time and confirms the details of their account.
    :ivar date_created: :class:`mongoengine.fields.DateTimeField` - The date
        that this user was created.
    :ivar date_modified: :class:`mongoengine.fields.DateTimeField` - The date
        the this user was last modified.
    :ivar gplus_id: :class:`mongoengine.fields.StringField` - The Google+ ID
        for this user.  It's what we use in the Google+ authentication.
    :ivar name: :class:`mongoengine.fields.StringField` - The user's name.
    :ivar slug: :class:`mongoengine.fields.StringField` - A URL slug  their
        internal profile page.
    :ivar email: :class:`mongoengine.fields.EmailField` - The user's email
        address.
    :ivar roles: :class:`mongoengine.fields.ListField` of
        :class:`mongoengine.fields.StringField` - A list of roles that the user
        has.
    :ivar privileges: :class:`mongoengine.fields.DictField` - A dictionary of
        privileges that the user has.  Often determined soley by their
        ``user_type``.
    :ivar image_url: :class:`mongoengine.fields.URLField` - The URL of the
        profile picture for the user's profile picture.
    :ivar image: :class:`mongoengine.fields.ReferenceField` - The local image
        for the user's profile picture.
    :ivar user_type: :class:`mongoengine.fields.StringField` - The type of the
        user. Can either be ``"fake_user"``, ``"editor"``, ``"publisher"``, or
        ``"admin"``.  The selection of user type determines their
        ``privileges``.
    :ivar last_logon: :class:`mongoengine.fields.DateTimeField` - The date of
        this user's last logon.
    """

    date_created = DateTimeField(required=True, default=now)
    date_modified = DateTimeField(required=True, default=now)
    gplus_id = StringField(required=True, unique=True)
    name = StringField(required=True, max_length=510)
    slug = StringField(required=True,
                       max_length=510,
                       unique=True,
                       regex=Regex.SLUG_REGEX)
    email = EmailField(required=True, unique=True)
    roles = ListField(StringField(db_field="role"), default=list)
    privileges = DictField(required=True, default={})
    image_url = URLField()
    image = ReferenceField('Image')
    user_type = StringField(default='editor', regex=USER_TYPE_REGEX)
    last_logon = DateTimeField()

    # MongoEngine ORM metadata
    meta = {'allow_inheritance': True, 'indexes': ['email', 'gplus_id']}

    def can(self, privilege):
        """Returns True if the user has ``privilege``.

        :returns: True if the user has ``privilege``
        :rtype: bool
        """
        return self.privileges.get(privilege)

    def get_profile_picture(self, size=50):
        """Returns the url to the profile picture for the user.

        TODO: This method needs major fixing.  What's going on with that URL?

        :param int size: The size of the image to pass, if the size can be
            changed.

        :returns: The URL of the image.
        :rtype: str
        """
        if self.image:
            return self.image.url()
        if not self.image_url:
            # Import app in the function body to avoid importing `None` when
            # the module is first loaded.
            return url_for(
                'eventum.static',
                filename=current_app.config['EVENTUM_DEFAULT_PROFILE_PICTURE'])
        if "googleusercontent.com" in self.image_url:
            return self.image_url + str(size)
        return self.image_url

    def register_login(self):
        """Update the model as having logged in."""
        self.last_logon = now()

    def clean(self):
        """Called by Mongoengine on every ``.save()`` to the object.

        Update date_modified and apply privileges shorthand notation.

        :raises: :class:`wtforms.validators.ValidationError`
        """
        self.date_modified = now()

        # Update self.privileges with one of the USER_TYPES dictionaries
        self.privileges.update(USER_TYPES[self.user_type])

        # Update the slug for the user (used in URLs)
        new_slug = self.name.lower().replace(' ', '-')
        new_slug = re.sub(r"\'|\.|\_|", "", new_slug)
        if User.objects(slug=new_slug).count() > 0:
            i = 2
            new_slug = new_slug + "-{}".format(i)
            while User.objects(slug=new_slug).count() > 0:
                i += 1
                new_slug = re.sub(r"-([0-9])*$", "-{}".format(i), new_slug)
        self.slug = new_slug

        if self.image_url and "googleusercontent.com" in self.image_url:
            self.image_url = re.sub(r"sz=([0-9]*)$", "sz=", self.image_url)

    def id_str(self):
        """The id of this object, as a string.

        :returns: The id
        :rtype: str
        """
        return str(self.id)

    def role(self):
        """Returns the role of the user, in plain English.  It is either
        ``"Admin"``, ``"Publisher"``, ``"Editor"``, or ``"Fake User"``.

        :returns: The role.
        :rtype: str
        """
        if self.can('admin'):
            return "Admin"
        if self.can('publish'):
            return "Publisher"
        if self.can('edit'):
            return "Editor"
        return "Fake User"

    def __repr__(self):
        """The representation of this user.

        :returns: The user's details.
        :rtype: str
        """
        return ('User(id=%r, name=%r, email=%r, roles=%r, privileges=%r, '
                'gplus_id=%r, date_created=%r)' %
                (self.id, self.name, self.email, self.roles, self.privileges,
                 self.gplus_id, self.date_created))

    def __unicode__(self):
        """This user, as a unicode string.

        :returns: The user encoded as a string.
        :rtype: str
        """
        if self.can('admin'):
            return '%r <%r> (Admin)' % (self.name, self.email)
        if self.can('publish'):
            return '%r <%r> (Publisher)' % (self.name, self.email)
        if self.can('edit'):
            return '%r <%r> (Editor)' % (self.name, self.email)
        else:
            return '%r <%r> (Fake User)' % (self.name, self.email)
Beispiel #6
0
class RunningModule(EmbeddedDocument):
    module = ReferenceField('Module')
    version = StringField()
Beispiel #7
0
class SummonerDoc(Document):
    name = StringField()
    game_refs = ListField(ReferenceField('GameDoc'))
Beispiel #8
0
class Tip(Document):
    from_user = ReferenceField(User, required=True)
    to_user = ReferenceField(User, required=True)
    amount = LongField(required=True)
    date = DateTimeField(required=True)
    tx_hash = StringField()
Beispiel #9
0
class Withdrawal(Document):
    user = ReferenceField(User, required=True)
    amount = LongField(required=True)
    date = DateTimeField(required=True)
    tx_hash = StringField()
Beispiel #10
0
class Signatures(Document):
    tx_id = ReferenceField(Swap, required=True)
    signed_tx = StringField(required=True)
    signer = StringField(required=True)
    creation = DateTimeField(default=datetime.now, required=True)
Beispiel #11
0
class Offer(BaseDocument):
    TYPE_CREDIT = 'credit'
    TYPE_FUTURES = 'futures'
    TYPE_FACTORING = 'factoring'

    salesman = ReferenceField('User',
                              reverse_delete_rule=mongoengine.NULLIFY,
                              default=None)
    type = StringField(choices=(TYPE_CREDIT, TYPE_FUTURES, TYPE_FACTORING),
                       default=None)

    description = StringField(default=None)
    zip_code = StringField(default=None)
    reg_service = StringField(default=None)
    options = DictField(default={})
    price = IntField(default=None)

    bc_hash = StringField(default=None)

    ru_card = DictField(default=None)
    en_card = DictField(default=None)

    creation_date = DateTimeField()
    update_date = DateTimeField()

    @property
    def fprice(self):
        try:
            return '{:,}'.format(self.price).replace(',', ' ')
        except:
            return ''

    def process(self, bot):
        offer_type = bot.match_command(
            bot.t(['SELL_CREDIT', 'SELL_FUTURES', 'SELL_FACTORING']))
        if offer_type and self.type is None:
            self.set_type(bot, offer_type.get('command'))
            if self.type == self.TYPE_CREDIT:
                return bot.send(bot.t('CREDIT_ENTER_DESC'),
                                reply_markup=ReplyKeyboardHide())
            elif self.type == self.TYPE_FUTURES:
                return bot.send(bot.t('FUTURES_ENTER_DESC'),
                                reply_markup=ReplyKeyboardHide())
            elif self.type == self.TYPE_FACTORING:
                return bot.send(bot.t('FACTORING_ENTER_DESC'),
                                reply_markup=ReplyKeyboardHide())

        if self.type == self.TYPE_CREDIT:
            self._process_credit(bot)
        elif self.type == self.TYPE_FUTURES:
            self._process_futures(bot)
        elif self.type == self.TYPE_FACTORING:
            self._process_factoring(bot)

    def _process_credit(self, bot):
        if self.description is None:
            self.description = bot.text
            bot.send(bot.t('CREDIT_ENTER_ZIP'))
        elif self.zip_code is None:
            self.zip_code = bot.text
            bot.send(bot.t('CREDIT_ENTER_REG_SERVICE'))
        elif self.reg_service is None:
            self.reg_service = bot.text
            bot.send(bot.t('CREDIT_ENTER_LOAN_ID'))
        elif self.options.get('loan_id') is None:
            self.options['loan_id'] = bot.text
            bot.send(bot.t('CREDIT_ENTER_LOAN_AMOUNT'))
        elif self.options.get('loan_amount') is None:
            self.options['loan_amount'] = utils.to_int(bot.text, None)
            if self.options['loan_amount'] is None:
                return bot.send(bot.t('ENTER_NUMBER'))
            bot.send(bot.t('CREDIT_ENTER_INTEREST_RATE'))
        elif self.options.get('interest_rate') is None:
            self.options['interest_rate'] = bot.text
            bot.send(bot.t('CREDIT_ENTER_LOAN_LENGTH'))
        elif self.options.get('loan_length') is None:
            self.options['loan_length'] = bot.text
            bot.send(bot.t('CREDIT_ENTER_LOAN_STATUS'),
                     reply_markup=ReplyKeyboard([[bot.t('LOAN_STATUS_EARLY')],
                                                 [bot.t('LOAN_STATUS_NORMAL')],
                                                 [bot.t('LOAN_STATUS_LATE')]]))
        elif self.options.get('loan_status') is None:
            self.options['loan_status'] = bot.text
            bot.send(bot.t('CREDIT_ENTER_SELLERS_WARRANTY'),
                     reply_markup=ReplyKeyboard([[
                         bot.t('WARRANTY_FULL'),
                         bot.t('WARRANTY_PARTLY'),
                         bot.t('WARRANTY_NONE')
                     ]],
                                                one_time_keyboard=True))
        elif self.options.get('sellers_warranty') is None:
            self.options['sellers_warranty'] = bot.text
            bot.send(bot.t('CREDIT_ENTER_PRICE'),
                     reply_markup=ReplyKeyboardHide())
        elif self.price is None:
            self.price = utils.to_int(bot.text, None)
            if self.price is None:
                return bot.send(bot.t('ENTER_NUMBER'))
            self._create_contract_send(bot)
        else:
            self._create_contract_process(bot)

    def _process_futures(self, bot):
        if self.description is None:
            self.description = bot.text
            bot.send(bot.t('FUTURES_ENTER_ZIP'))
        elif self.zip_code is None:
            self.zip_code = bot.text
            bot.send(bot.t('FUTURES_ENTER_REG_SERVICE'))
        elif self.reg_service is None:
            self.reg_service = bot.text
            bot.send(bot.t('FUTURES_ENTER_LOAN_ID'))
        elif self.options.get('loan_id') is None:
            self.options['loan_id'] = bot.text
            bot.send(bot.t('FUTURES_CHOOSE_CONTRACT_TYPE'),
                     reply_markup=ReplyKeyboard([
                         [bot.t('FUTURES_TYPE_SETTLEMENT')],
                         [bot.t('FUTURES_TYPE_DELIVERABLE')],
                     ],
                                                one_time_keyboard=True))
        elif self.options.get('contract_type') is None:
            self.options['contract_type'] = bot.text
            bot.send(bot.t('FUTURES_ENTER_CONTRACT_SIZE'),
                     reply_markup=ReplyKeyboardHide())
        elif self.options.get('contract_size') is None:
            self.options['contract_size'] = bot.text
            bot.send(bot.t('FUTURES_CONTRACT_MATURITY'))
        elif self.options.get('contract_maturity') is None:
            self.options['contract_maturity'] = bot.text
            bot.send(bot.t('FUTURES_ENTER_DELIVERY_DATE'))
        elif self.options.get('delivery_date') is None:
            self.options['delivery_date'] = bot.text
            bot.send(bot.t('FUTURES_PRICE'))
        elif self.price is None:
            self.price = utils.to_int(bot.text, None)
            if self.price is None:
                return bot.send(bot.t('ENTER_NUMBER'))
            self._create_contract_send(bot)
        else:
            self._create_contract_process(bot)

    def _process_factoring(self, bot):
        if self.description is None:
            self.description = bot.text
            bot.send(bot.t('FACTORING_ENTER_ZIP'))
        elif self.zip_code is None:
            self.zip_code = bot.text
            bot.send(bot.t('FACTORING_ENTER_REG_SERVICE'))
        elif self.reg_service is None:
            self.reg_service = bot.text
            bot.send(bot.t('FACTORING_ENTER_LOAN_ID'))
        elif self.options.get('loan_id') is None:
            self.options['loan_id'] = bot.text
            bot.send(bot.t('FACTORING_PAY_REQS'),
                     reply_markup=ReplyKeyboard([
                         [bot.t('FACTORING_REGRESS')],
                         [bot.t('FACTORING_NO_REGRESS')],
                     ],
                                                one_time_keyboard=True))
        elif self.options.get('pay_reqs') is None:
            self.options['pay_reqs'] = bot.text
            bot.send(bot.t('FACTORING_TITLE_SUPPLIER'),
                     reply_markup=ReplyKeyboardHide())
        elif self.options.get('title_supplier') is None:
            self.options['title_supplier'] = bot.text
            bot.send(bot.t('FACTORING_SUM_REQS'))
        elif self.options.get('sum_reqs') is None:
            self.options['sum_reqs'] = bot.text
            bot.send(bot.t('FACTORING_DATE_REQS_PAY'))
        elif self.options.get('date_reqs_pay') is None:
            self.options['date_reqs_pay'] = bot.text
            bot.send(bot.t('FACTORING_PRICE'))
        elif self.price is None:
            self.price = utils.to_int(bot.text, None)
            if self.price is None:
                return bot.send(bot.t('ENTER_NUMBER'))
            self._create_contract_send(bot)
        else:
            self._create_contract_process(bot)

    @gen.coroutine
    def _create_contract_send(self, bot):
        try:
            bot.send(bot.t('GENERATE_PREVIEW_START'))

            yield self.generate_img(lang=bot.user.lang)

            path = self.get_image_path(bot.user.lang)
            if not path:
                raise Exception

            with open(path, 'rb') as f:
                yield bot.send_photo(files=(('photo', path, f.read()), ),
                                     caption=bot.t('GENERATE_PREVIEW_END'))
        except Exception as e:
            bot.send(bot.t('GENERATE_PREVIEW_FAIL'))
            traceback.print_exc()

        bot.send(bot.t('SAIL_YOU_CREATE_CONTRACT'),
                 reply_markup=ReplyKeyboard([[
                     bot.t('YES_APPROVE'),
                     bot.t('NO_FEAR'),
                     bot.t('WHAT_IS_BLOCKCHAIN')
                 ]],
                                            one_time_keyboard=False))

    @gen.coroutine
    def _create_contract_process(self, bot):
        if bot.match_command(bot.t('YES_APPROVE')):
            bot.send(bot.t('REGISTER_BC_BEGIN'),
                     reply_markup=ReplyKeyboardHide())

            # рега в реестре
            yield gen.sleep(1)
            self.bc_hash = PasswordHelper.get_hash(datetime.now().isoformat())

            # генерация картинок
            yield self.generate_img()

            self.salesman = self.salesman
            self.save()

            bot._on_start(welcome_text=bot.t('REGISTER_BC_END'),
                          keyboard=ReplyKeyboard([[bot.t('THNX_UNDERSTAND')]]))
        elif bot.match_command(bot.t('NO_FEAR')):
            bot._on_start(welcome_text=bot.t('FEAR_BLOCKCHAIN_WIKI'))
        elif bot.match_command(bot.t('WHAT_IS_BLOCKCHAIN')):
            bot.send(bot.t('WHAT_IS_BLOCKCHAIN_WIKI'))

    @gen.coroutine
    def generate_img(self, sync=False, *args, **kwargs):
        if sync:
            client = HTTPClient()
        else:
            client = AsyncHTTPClient()

        if kwargs.get('lang') in [None, Text.LANG_RU]:
            # генерация русской карточки
            req = HTTPRequest(
                get_screenshot_img_url(self.get_id(), Text.LANG_RU))
            if sync:
                res = client.fetch(req)
            else:
                res = yield client.fetch(req)

            ru_path = gen_path()
            mkdir(ru_path.get('folder'))
            with open(ru_path.get('fullname'), "wb") as f:
                f.write(res.body)

            self.ru_card = ru_path

        if kwargs.get('lang') in [None, Text.LANG_EN]:
            # генерация английской карточки
            req = HTTPRequest(
                get_screenshot_img_url(self.get_id(), Text.LANG_EN))
            if sync:
                res = client.fetch(req)
            else:
                res = yield client.fetch(req)

            en_path = gen_path()
            mkdir(en_path.get('folder'))
            with open(en_path.get('fullname'), "wb") as f:
                f.write(res.body)

            self.en_card = en_path

    def get_image_path(self, lang):
        path = None

        if lang == Text.LANG_RU:
            if self.ru_card is None:
                return False
            path = self.ru_card.get('relname')
        elif lang == Text.LANG_EN:
            if self.en_card is None:
                return False
            path = self.en_card.get('relname')

        if path is None:
            return False

        return '%s/%s' % (options.upload_path, path)

    def set_type(self, bot, offer_type):
        if offer_type == bot.t('SELL_CREDIT'):
            self.type = Offer.TYPE_CREDIT
        elif offer_type == bot.t('SELL_FUTURES'):
            self.type = Offer.TYPE_FUTURES
        elif offer_type == bot.t('SELL_FACTORING'):
            self.type = Offer.TYPE_FACTORING

    def save(self, *args, **kwargs):
        if not self.creation_date:
            self.creation_date = datetime.now(tz=timezone('UTC'))
        self.update_date = datetime.now(tz=timezone('UTC'))
        return super(Offer, self).save(*args, **kwargs)

    def to_dict_impl(self, **kwargs):
        return {
            'id': self.get_id(),
            'type': self.type,
            'description': self.description,
            'creation_date': self.creation_date,
            'zip_code': self.zip_code,
            'reg_service': self.reg_service,
            'options': self.options,
            'price': self.price,
            'salesman': self.salesman.to_dict() if self.salesman else None,
            'bc_hash': self.bc_hash
        }
Beispiel #12
0
class Account(Document, BaseModel):
    created_at = date_now()
    updated_at = DateTimeField()
    estado: Enum = EnumField(Estado, default=Estado.created)

    nombre = StringField()
    apellido_paterno = StringField()
    apellido_materno = StringField(required=False)
    cuenta = StringField(unique=True)
    rfc_curp = StringField()
    telefono = StringField()

    genero = EnumField(Genero, required=False)  # type: ignore
    fecha_nacimiento = DateTimeField(required=False)
    entidad_federativa = IntField(required=False)
    actividad_economica = IntField(required=False)
    calle = StringField(required=False)
    numero_exterior = StringField(required=False)
    numero_interior = StringField(required=False)
    colonia = StringField(required=False)
    alcaldia_municipio = StringField(required=False)
    cp = StringField(required=False)
    pais = IntField(required=False)
    email = StringField(required=False)
    id_identificacion = StringField(required=False)

    events = ListField(ReferenceField(Event))

    def create_account(self) -> CuentaFisica:
        self.estado = Estado.submitted
        self.save()

        optionals = dict(
            apellidoMaterno=self.apellido_materno,
            genero=self.genero,
            fechaNacimiento=self.fecha_nacimiento,
            entidadFederativa=self.entidad_federativa,
            actividadEconomica=self.actividad_economica,
            calle=self.calle,
            numeroExterior=self.numero_exterior,
            numeroInterior=self.numero_interior,
            colonia=self.colonia,
            alcaldiaMunicipio=self.alcaldia_municipio,
            cp=self.cp,
            pais=self.pais,
            email=self.email,
            idIdentificacion=self.id_identificacion,
        )

        # remove if value is None
        optionals = {key: val for key, val in optionals.items() if val}

        try:
            cuenta = stpmex_client.cuentas.alta(
                nombre=self.nombre,
                apellidoPaterno=self.apellido_paterno,
                cuenta=self.cuenta,
                rfcCurp=self.rfc_curp,
                telefono=self.telefono,
                **optionals,
            )
        except Exception as e:
            self.events.append(Event(type=EventType.error, metadata=str(e)))
            self.estado = Estado.error
            self.save()
            raise e
        else:
            self.estado = Estado.succeeded
            self.save()
            return cuenta

    def update_account(self, account: 'Account') -> None:
        optionals = dict(
            apellidoMaterno=account.apellido_materno,
            genero=account.genero,
            fechaNacimiento=account.fecha_nacimiento,
            entidadFederativa=account.entidad_federativa,
            actividadEconomica=account.actividad_economica,
            calle=account.calle,
            numeroExterior=account.numero_exterior,
            numeroInterior=account.numero_interior,
            colonia=account.colonia,
            alcaldiaMunicipio=account.alcaldia_municipio,
            cp=account.cp,
            pais=account.pais,
            email=account.email,
            idIdentificacion=account.id_identificacion,
        )

        # remove if value is None
        optionals = {key: val for key, val in optionals.items() if val}

        self.rfc_curp = account.rfc_curp
        self.nombre = account.nombre
        self.apellido_paterno = account.apellido_paterno
        self.apellido_materno = account.apellido_materno
        self.genero = account.genero
        self.fecha_nacimiento = account.fecha_nacimiento
        self.actividad_economica = account.actividad_economica
        self.calle = account.calle
        self.numero_exterior = account.numero_exterior
        self.numero_interior = account.numero_interior
        self.colonia = account.colonia
        self.alcaldia_municipio = account.alcaldia_municipio
        self.cp = account.cp
        self.pais = account.pais
        self.email = account.email
        self.id_identificacion = account.id_identificacion
        self.estado = Estado.succeeded
        self.save()
Beispiel #13
0
class ServicePort(Document):
    port = IntField(default=0)
    ip = StringField(default="")
    name = StringField(default="")
    cluster = ReferenceField(Cluster, reverse_delete_rule=CASCADE)
Beispiel #14
0
class Container(Document):
    id = StringField(default="", primary_key=True)
    name = StringField(default="")
    cluster = ReferenceField(Cluster, reverse_delete_rule=CASCADE)
Beispiel #15
0
class Habit(Document):

    name = StringField(max_length=120, required=True)
    description = StringField(max_length=5000)
    user = ReferenceField(User, reverse_delete_rule=CASCADE)
    num_Days = IntField(default=30)
    repeat = ListField(StringField(max_length=10))
    streak = 0
    is_public = StringField(requried=True, default="false", max_length=120)
    string_start = StringField(required=True,
                               default=datetime.datetime.strftime(
                                   datetime.datetime.now(), "%B %m, %Y"))
    start_Date = DateTimeField(required=True, default=datetime.datetime.now())
    curr_Date = DateTimeField(required=True, default=datetime.datetime.now())
    end_Date = DateTimeField(required=True, default=datetime.datetime.now())
    habit_data = ListField(ListField(max_length=3, required=True))
    complete = 0
    monthDict = {
        1: 31,
        2: 28,
        3: 31,
        4: 30,
        5: 31,
        6: 30,
        7: 31,
        8: 31,
        9: 30,
        10: 31,
        11: 30,
        12: 31
    }

    def Habit(self, name, days, end_Date):
        self.name = name
        self.days = days
        self.start_Date = [
            timer.strftime("%m"), timer.strftime("%d")
        ]  #A list with the month in mm format and day in the dd format
        self.start_Day = int(timer.day())
        self.curr_Day = int(timer.day())
        self.end_Date = end_Date

    # def habits(self, name, days, end_Date):
    #     self.name = name
    #     self.days = days
    #     self.start_Date = [timer.strftime("%m"), timer.strftime("%d")] #A list with the month in mm format and day in the dd format
    #     self.start_Day = int(timer.day())
    #     self.curr_Day = int(timer.day())
    #     self.end_Date = end_Date

    def setName(self, name):
        self.name = name

    def getName(self):
        return self.name

    def setDays(self, days):
        self.days = days

    def getDays(self):
        return self.days

    def setStreak(self, num):
        self.streak = num

    def getStreak(self):
        return self.streak

    def setStartDay(self, day):
        self.start_Day = day

    def getStartDay(self):
        return self.start_Day

    def setStartDate(self, date):
        self.start_Date = date

    def getStartDate(self):
        return self.start_Date

    def setEndDate(self, date):
        self.end_Date = date

    def getEndDate(self):
        return self.end_Date

    def calculate():
        day1 = start_Date[1]
        daycurr = curr_Date[1]
        day2 = end_Date[1]
        if (int(end_Date[1]) > int(start_Date[1])):
            day2 += monthDict[end_Date[1]]
        if (int(curr_Date[1]) > int(start_Date[1])):
            daycurr += monthDict[curr_Date[1]]
        fromStart = daycurr - day1
        totalDays = day2 - day1
        return str(round(fromStart / totalDays, 2))

    def streak():
        if (self.timer.day > self.curr_Date):
            if (self.complete == 1):
                self.streak += 1
                self.curr_Date += 1
                self.complete = 0
                return self.streak
            else:
                return 0
        else:
            return self.streak

    def to_public_json(self):
        entry = {
            "id": str(self.id),
            "user": {
                "id": str(self.user.id),
                "username": self.user.username
            },
            "name": self.name,
            "description": self.description,
            "num_Days": self.num_Days,
            "repeat": self.repeat,
            "string_start": self.string_start,
            "start_Date": self.start_Date,
            "curr_Date": self.curr_Date,
            "end_Date": self.end_Date,
            "is_public": self.is_public,
            "habit_data": self.habit_data
        }
        return entry
Beispiel #16
0
class Request(MongoModel, Document):
    brewtils_model = brewtils.models.Request

    # These fields are duplicated for job types, changes to this field
    # necessitate a change to the RequestTemplateSchema in brewtils.
    TEMPLATE_FIELDS = {
        "system": {"field": StringField, "kwargs": {"required": True}},
        "system_version": {"field": StringField, "kwargs": {"required": True}},
        "instance_name": {"field": StringField, "kwargs": {"required": True}},
        "namespace": {"field": StringField, "kwargs": {"required": False}},
        "command": {"field": StringField, "kwargs": {"required": True}},
        "command_type": {"field": StringField, "kwargs": {}},
        "parameters": {"field": DictField, "kwargs": {}},
        "comment": {"field": StringField, "kwargs": {"required": False}},
        "metadata": {"field": DictField, "kwargs": {}},
        "output_type": {"field": StringField, "kwargs": {}},
    }

    for field_name, field_info in TEMPLATE_FIELDS.items():
        locals()[field_name] = field_info["field"](**field_info["kwargs"])

    # Shared field with RequestTemplate, but it is required when saving Request
    namespace = StringField(required=True)

    parent = ReferenceField(
        "Request", dbref=True, required=False, reverse_delete_rule=CASCADE
    )
    children = DummyField(required=False)
    output = StringField()
    output_gridfs = FileField()
    output_type = StringField(choices=BrewtilsCommand.OUTPUT_TYPES)
    status = StringField(choices=BrewtilsRequest.STATUS_LIST, default="CREATED")
    command_type = StringField(choices=BrewtilsCommand.COMMAND_TYPES)
    created_at = DateTimeField(default=datetime.datetime.utcnow, required=True)
    updated_at = DateTimeField(default=None, required=True)
    error_class = StringField(required=False)
    has_parent = BooleanField(required=False)
    hidden = BooleanField(required=False)
    requester = StringField(required=False)
    parameters_gridfs = FileField()

    meta = {
        "auto_create_index": False,  # We need to manage this ourselves
        "index_background": True,
        "indexes": [
            # These are used for sorting all requests
            {"name": "command_index", "fields": ["command"]},
            {"name": "command_type_index", "fields": ["command_type"]},
            {"name": "system_index", "fields": ["system"]},
            {"name": "instance_name_index", "fields": ["instance_name"]},
            {"name": "namespace_index", "fields": ["namespace"]},
            {"name": "status_index", "fields": ["status"]},
            {"name": "created_at_index", "fields": ["created_at"]},
            {"name": "updated_at_index", "fields": ["updated_at"]},
            {"name": "comment_index", "fields": ["comment"]},
            {"name": "parent_ref_index", "fields": ["parent"]},
            {"name": "parent_index", "fields": ["has_parent"]},
            # These are for sorting parent requests
            {"name": "parent_command_index", "fields": ["has_parent", "command"]},
            {"name": "parent_system_index", "fields": ["has_parent", "system"]},
            {
                "name": "parent_instance_name_index",
                "fields": ["has_parent", "instance_name"],
            },
            {"name": "parent_status_index", "fields": ["has_parent", "status"]},
            {"name": "parent_created_at_index", "fields": ["has_parent", "created_at"]},
            {"name": "parent_comment_index", "fields": ["has_parent", "comment"]},
            # These are used for filtering all requests while sorting on created time
            {"name": "created_at_command_index", "fields": ["-created_at", "command"]},
            {"name": "created_at_system_index", "fields": ["-created_at", "system"]},
            {
                "name": "created_at_instance_name_index",
                "fields": ["-created_at", "instance_name"],
            },
            {"name": "created_at_status_index", "fields": ["-created_at", "status"]},
            # These are used for filtering parent while sorting on created time
            {
                "name": "parent_created_at_command_index",
                "fields": ["has_parent", "-created_at", "command"],
            },
            {
                "name": "parent_created_at_system_index",
                "fields": ["has_parent", "-created_at", "system"],
            },
            {
                "name": "parent_created_at_instance_name_index",
                "fields": ["has_parent", "-created_at", "instance_name"],
            },
            {
                "name": "parent_created_at_status_index",
                "fields": ["has_parent", "-created_at", "status"],
            },
            # These are used for filtering hidden while sorting on created time
            # I THINK this makes the set of indexes above superfluous, but I'm keeping
            # both as a safety measure
            {
                "name": "hidden_parent_created_at_command_index",
                "fields": ["hidden", "has_parent", "-created_at", "command"],
            },
            {
                "name": "hidden_parent_created_at_system_index",
                "fields": ["hidden", "has_parent", "-created_at", "system"],
            },
            {
                "name": "hidden_parent_created_at_instance_name_index",
                "fields": ["hidden", "has_parent", "-created_at", "instance_name"],
            },
            {
                "name": "hidden_parent_created_at_status_index",
                "fields": ["hidden", "has_parent", "-created_at", "status"],
            },
            # This is used for text searching
            {
                "name": "text_index",
                "fields": [
                    "$system",
                    "$command",
                    "$command_type",
                    "$comment",
                    "$status",
                    "$instance_name",
                ],
            },
        ],
    }

    logger = logging.getLogger(__name__)

    def pre_serialize(self):
        encoding = "utf-8"
        """If string output was over 16MB it was spilled over to the GridFS storage solution"""
        if self.output_gridfs:
            self.logger.debug("~~~Retrieving output from gridfs")
            self.output = self.output_gridfs.read().decode(encoding)
            self.output_gridfs = None

        if self.parameters_gridfs:
            self.logger.debug("~~~Retrieving parameters from gridfs")
            self.parameters = json.loads(self.parameters_gridfs.read().decode(encoding))
            self.parameters_gridfs = None

    def save(self, *args, **kwargs):
        self.updated_at = datetime.datetime.utcnow()
        max_size = 15 * 1_000_000
        encoding = "utf-8"
        parameter_size = sys.getsizeof(self.parameters)

        if self.output:
            parameter_size = sys.getsizeof(self.parameters)
            if parameter_size > max_size:
                self.logger.info("~~~Parameter size too big, storing in gridfs")
                self.parameters_gridfs.put(
                    json.dumps(self.parameters), encoding=encoding
                )
                self.parameters = None
                parameter_size = 0

            # If the output size is too large, we switch it over
            # Max size for Mongo is 16MB, switching over at 15MB to be safe
            if self.output and (sys.getsizeof(self.output) + parameter_size) > max_size:
                self.logger.info("~~~Output size too big, storing in gridfs")
                self.output_gridfs.put(self.output, encoding=encoding)
                self.output = None

        super(Request, self).save(*args, **kwargs)

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

        if self.status not in BrewtilsRequest.STATUS_LIST:
            raise ModelValidationError(
                f"Can not save Request {self}: Invalid status '{self.status}'"
            )

        if (
            self.command_type is not None
            and self.command_type not in BrewtilsRequest.COMMAND_TYPES
        ):
            raise ModelValidationError(
                f"Can not save Request {self}: Invalid command type '{self.command_type}'"
            )

        if (
            self.output_type is not None
            and self.output_type not in BrewtilsRequest.OUTPUT_TYPES
        ):
            raise ModelValidationError(
                f"Can not save Request {self}: Invalid output type '{self.output_type}'"
            )

    def clean_update(self):
        """Ensure that the update would not result in an illegal status transition"""
        # Get the original status
        old_status = Request.objects.get(id=self.id).status

        if self.status != old_status:
            if old_status in BrewtilsRequest.COMPLETED_STATUSES:
                raise RequestStatusTransitionError(
                    f"Status for a request cannot be updated once it has been "
                    f"completed. Current: {old_status}, Requested: {self.status}"
                )

            if (
                old_status == "IN_PROGRESS"
                and self.status not in BrewtilsRequest.COMPLETED_STATUSES
            ):
                raise RequestStatusTransitionError(
                    f"Request status can only transition from IN_PROGRESS to a "
                    f"completed status. Requested: {self.status}, completed statuses "
                    f"are {BrewtilsRequest.COMPLETED_STATUSES}."
                )
Beispiel #17
0
class Track(Document):
    """
    Track is a reference to a playable medium stored in an external,
    online source.

    It has common meta data (title, year, album name, artist name, ...)
    and is optionally linked to Musicbrainz DB via the track's MBID,
    artist's MBID, or/and album MBID.

    It is an "item" in FRBR's parlance.
    It is a "recording" in MB's parlance.

    http://en.wikipedia.org/wiki/FRBR
    http://musicbrainz.org/doc/MusicBrainz_Database/Schema

    It has `source` which determines where the medium is physically stored.

    """
    # User is optional as tracks can exist and be in no one's music collection.
    # However, as soon a user adds it to their collection, an editable copy
    # of the track with the user assigned is created.
    user = ReferenceField(User,
                          dbref=False,
                          required=False,
                          reverse_delete_rule=CASCADE)

    # All MBIDs are optional as they are filled only when possible.
    mbid = StringField()
    artist_mbid = StringField()
    album_mbid = StringField()

    title = StringField(required=True)

    # artist and album names
    artist = StringField()
    album = StringField()

    number = IntField()
    # set = IntField()
    year = IntField()

    # Source metadata.
    source = StringField(required=True, choices=['youtube', 'dropbox'])
    dropbox = EmbeddedDocumentField(DropboxTrack)
    youtube = EmbeddedDocumentField(YoutubeTrack)

    meta = {
        'collection': 'tracks',
        'allow_inheritance': False,
        'indexes': [
            'user',
            'mbid',
            'artist_mbid',
            'album_mbid',
        ]
    }

    def __unicode__(self):
        return self.title

    def to_json(self):

        data = {
            'id': str(self.pk),
            'title': self.title,
            'artist': self.artist or UNTITLED_ARTIST,
            'album': self.album or UNTITLED_ALBUM,
            'number': self.number,
            'source': self.source[0],
        }

        if self.source == 'youtube':
            data['sourceId'] = self.youtube.id

        return data
Beispiel #18
0
class SystemGardenMapping(MongoModel, Document):
    system = ReferenceField("System")
    garden = ReferenceField("Garden")
Beispiel #19
0
class OrderedProduct(EmbeddedDocument):
    quantity = IntField(required=True)
    product = ReferenceField(Product, required=True)
Beispiel #20
0
class EntityMixin(object):
    """A document mixin which attaches each document to an entity"""

    #: The entity that owns the document
    entity = ReferenceField('Entity', required=True, dbref=False)
Beispiel #21
0
class Room(Document):
    name = StringField(required=True, unique=True)
    members = ListField(ReferenceField(Client))
class ZabbixAgent(DynamicDocument):
    """ Zabbix Agent model representation
    """

    # Retrieve node hostname to set default value of 'hostname' field
    default_hostname = get_hostname()

    enabled = BooleanField(default=False,
                           verbose_name=_('Enable service'),
                           required=True,
                           help_text=_('Enable agent service'))

    servers = StringField(
        verbose_name=_('Server(s)'),
        required=True,
        default='127.0.0.1,::127.0.0.1,::ffff:127.0.0.1',
        help_text=
        _('List of comma delimited IP addresses (or hostnames) of Zabbix servers.'
          ))

    listeners = ListField(
        ReferenceField('Listener', reverse_delete_rule=PULL),
        verbose_name=_("Listen address(es)"),
        required=True,
        help_text=_('List of IP addresses that the agent should listen on.'))

    port = IntField(
        default=10050,
        required=True,
        min_value=1024,
        max_value=65535,
        verbose_name=_("Listen port"),
        help_text=_(
            'Agent will listen on this port for connections from the server.'))

    active_servers = StringField(
        verbose_name=_('Active Server(s)'),
        required=True,
        default="127.0.0.1:20051,zabbix.domain,[::1]:30051,::1,[12fc::1]",
        help_text=_(
            'List of comma delimited IP:port (or hostname:port) pairs of Zabbix'
            ' servers for active checks.'))

    hostname = StringField(
        verbose_name=_('Hostname'),
        required=True,
        default=default_hostname,
        help_text=_('Required for active checks and must match hostname'
                    ' as configured on the server.'))

    allow_root = BooleanField(verbose_name=_('Allow root'),
                              required=True,
                              default=False,
                              help_text=_('Allow the agent to run as "root".'))

    tls_accept = StringField(
        verbose_name=_('TLS Accept'),
        default='unencrypted',
        choices=ENCRYPTION_TYPE,
        required=True,
        help_text=_('What incoming connections to accept.'))

    tls_connect = StringField(
        verbose_name=_('TLS Connect'),
        required=True,
        default='unencrypted',
        choices=ENCRYPTION_TYPE,
        help_text=
        _('How the agent should connect to server or proxy. Used for active checks.'
          ))

    tls_cert = ReferenceField(
        'SSLCertificate',
        reverse_delete_rule=PULL,
        verbose_name=_('Agent certificate'),
        required=False,
        help_text=_('Certificate used by "TLS Accept" and/or "TLS Connect"'))

    tls_server_subject = StringField(
        verbose_name=_('Server certificate subject'),
        required=False,
        help_text=_('Allowed server certificate subject.'))

    tls_server_issuer = StringField(
        verbose_name=_('Server certificate issuer'),
        required=False,
        help_text=_('Allowed server certificate issuer.'))

    psk_identity = StringField(
        verbose_name=_('Agent PSK identity '),
        required=False,
        help_text=_(
            'Unique, case sensitive string used to identify the pre-shared key.'
        ))

    psk_key = StringField(
        verbose_name=_('Agent PSK string '),
        required=False,
        help_text=_('Pre-shared key used by agent to verify connection.'))

    enable_remote_commands = BooleanField(
        verbose_name=_('Enable remote commands'),
        required=True,
        default=False,
        help_text=_('Whether remote commands from Zabbix server are allowed.'))

    log_remote_commands = BooleanField(
        verbose_name=_('Log remote commands'),
        required=True,
        default=False,
        help_text=_('Enable logging of executed shell commands as warnings.'))

    start_agents = IntField(
        default=3,
        required=True,
        min_value=0,
        max_value=100,
        verbose_name=_("Start Agents"),
        help_text=
        _('Number of pre-forked instances of zabbix_agentd that process passive checks.'
          'If set to 0, disables passive checks and the agent will not listen on any TCP '
          'port.'))

    refresh_active_checks = IntField(
        default=120,
        required=True,
        min_value=60,
        max_value=3600,
        verbose_name=_("Refresh active checks"),
        help_text=_(
            'How often list of active checks is refreshed, in seconds.'))

    timeout_process = IntField(
        default=3,
        required=True,
        min_value=1,
        max_value=30,
        verbose_name=_("Timeout"),
        help_text=_('Spend no more than Timeout seconds on processing.'))

    buffer_send = IntField(
        default=5,
        required=True,
        min_value=1,
        max_value=3600,
        verbose_name=_("Buffer send"),
        help_text=_('Do not keep data longer than N seconds in buffer.'))

    buffer_size = IntField(
        default=100,
        required=True,
        min_value=2,
        max_value=65535,
        verbose_name=_("Buffer size"),
        help_text=_(
            'Maximum number of values in a memory buffer. The agent will send all '
            'collected data to Zabbix Server/Proxy if the buffer is full.'))

    def to_template(self):
        """ Dictionary used to create configuration file.

        :return: Dictionary of configuration parameters
        """
        # Convert self attributes into dict
        zabbix_settings = self.to_mongo()

        # Convert listeners list into string
        zabbix_settings['listeners'] = ','.join([l.ip for l in self.listeners])
        # Convert Boolean fields into int (0 or 1)
        bool_to_int = {
            True: 1,
            False: 0,
            # 'True': 1,
            # 'False': 0
        }
        attrs_to_convert = {
            'allow_root': self.allow_root,
            'enable_remote_commands': self.enable_remote_commands,
            'log_remote_commands': self.log_remote_commands,
        }
        for attr_name, attr in attrs_to_convert.items():
            zabbix_settings[attr_name] = bool_to_int[attr]
        return zabbix_settings

    def __str__(self):
        return "Zabbix-Agent Settings"
Beispiel #23
0
class Transaction(Document, BaseModel):
    created_at = date_now()
    updated_at = DateTimeField()
    stp_id = IntField()
    fecha_operacion = DateTimeField()
    institucion_ordenante = StringField()
    institucion_beneficiaria = StringField()
    clave_rastreo = StringField()
    monto = IntField()
    nombre_ordenante = StringField()
    tipo_cuenta_ordenante = IntField()
    cuenta_ordenante = StringField()
    rfc_curp_ordenante = StringField()
    nombre_beneficiario = StringField()
    tipo_cuenta_beneficiario = IntField()
    cuenta_beneficiario = StringField()
    rfc_curp_beneficiario = StringField()
    concepto_pago = StringField()
    referencia_numerica = IntField()
    empresa = StringField()
    estado: Enum = EnumField(Estado, default=Estado.created)
    version = IntField()
    speid_id = StringField()
    folio_origen = StringField()
    tipo_pago = IntField()
    email_beneficiario = StringField()
    tipo_cuenta_beneficiario2 = StringField()
    nombre_beneficiario2 = StringField()
    cuenta_beneficiario2 = StringField()
    rfc_curpBeneficiario2 = StringField()
    concepto_pago2 = StringField()
    clave_cat_usuario1 = StringField()
    clave_cat_usuario2 = StringField()
    clave_pago = StringField()
    referencia_cobranza = StringField()
    tipo_operacion = StringField()
    topologia = StringField()
    usuario = StringField()
    medio_entrega = IntField()
    prioridad = IntField()
    compound_key = StringField()

    events = ListField(ReferenceField(Event))

    meta = {
        'indexes': [
            '+stp_id',
            '+speid_id',
            '+clave_rastreo',
            # The Unique-Sparse index skips over any document that is missing
            # the indexed field (null values)
            {'fields': ['+compound_key'], 'unique': True, 'sparse': True},
        ]
    }

    def set_state(self, state: Estado):
        callback_helper.set_status_transaction(self.speid_id, state.value)
        self.estado = state

        self.events.append(Event(type=EventType.completed))

    def confirm_callback_transaction(self):
        response = ''
        self.events.append(Event(type=EventType.created))
        self.save()
        self.estado = Estado.succeeded
        callback_helper.send_transaction(self.to_dict())

        self.events.append(
            Event(type=EventType.completed, metadata=str(response))
        )

    def create_order(self) -> Orden:
        # Validate account has already been created
        if not SKIP_VALIDATION_PRIOR_SEND_ORDER:
            try:
                account = Account.objects.get(cuenta=self.cuenta_ordenante)
                assert account.estado is Estado.succeeded
            except (DoesNotExist, AssertionError):
                self.estado = Estado.error
                self.save()
                raise MalformedOrderException(
                    f'Account has not been registered: {self.cuenta_ordenante}'
                    f', stp_id: {self.stp_id}'
                )

        # Don't send if stp_id already exists
        if self.stp_id:
            return Orden(  # type: ignore
                id=self.stp_id,
                monto=self.monto / 100.0,
                conceptoPago=self.concepto_pago,
                nombreBeneficiario=self.nombre_beneficiario,
                cuentaBeneficiario=self.cuenta_beneficiario,
                institucionContraparte=self.institucion_beneficiaria,
                cuentaOrdenante=self.cuenta_ordenante,
            )

        optionals = dict(
            institucionOperante=self.institucion_ordenante,
            claveRastreo=self.clave_rastreo,
            referenciaNumerica=self.referencia_numerica,
            rfcCurpBeneficiario=self.rfc_curp_beneficiario,
            medioEntrega=self.medio_entrega,
            prioridad=self.prioridad,
            tipoPago=self.tipo_pago,
            topologia=self.topologia,
        )
        # remove if value is None
        remove = []
        for k, v in optionals.items():
            if v is None:
                remove.append(k)
        for k in remove:
            optionals.pop(k)

        try:
            order = stpmex_client.ordenes.registra(
                monto=self.monto / 100.0,
                conceptoPago=self.concepto_pago,
                nombreBeneficiario=self.nombre_beneficiario,
                cuentaBeneficiario=self.cuenta_beneficiario,
                institucionContraparte=self.institucion_beneficiaria,
                tipoCuentaBeneficiario=self.tipo_cuenta_beneficiario,
                nombreOrdenante=self.nombre_ordenante,
                cuentaOrdenante=self.cuenta_ordenante,
                rfcCurpOrdenante=self.rfc_curp_ordenante,
                **optionals,
            )
        except (Exception) as e:  # Anything can happen here
            self.events.append(Event(type=EventType.error, metadata=str(e)))
            self.estado = Estado.error
            self.save()
            raise e
        else:
            self.clave_rastreo = self.clave_rastreo or order.claveRastreo
            self.rfc_curp_beneficiario = (
                self.rfc_curp_beneficiario or order.rfcCurpBeneficiario
            )
            self.referencia_numerica = (
                self.referencia_numerica or order.referenciaNumerica
            )
            self.empresa = self.empresa or STP_EMPRESA
            self.stp_id = order.id

            self.events.append(
                Event(type=EventType.completed, metadata=str(order))
            )
            self.estado = Estado.submitted
            self.save()
            return order
Beispiel #24
0
class Post(Document):
    text = StringField()
    user = ReferenceField(User, required=True)
    comments = ListField(StringField())
    comments_by = ListField(ReferenceField("User"))
    keyword = ListField(StringField())
    reaction = ListField(StringField())
    hilarious = ListField(ReferenceField("User"))
    well_written = ListField(ReferenceField("User"))
    amazing_story = ListField(ReferenceField("User"))

    grammar_king = ReferenceField("User")

    group = IntField()
    assignment_id = ReferenceField("Assignment")
    submit = BooleanField(default=False)

    @staticmethod
    def create(text, assignment_id):

        post = Post(text=text,
                    user=current_user['id'],
                    assignment_id=ObjectId(assignment_id),
                    submit=True)
        post.save()
        return post

    @staticmethod
    def add_reaction(post_id, hilarious="", well_written="", amazing=""):

        post = Post.objects(pk=ObjectId(post_id), submit=True)
        if hilarious.lower() == 'true':
            post.update(add_to_set__hilarious=current_user['id'])
        if well_written.lower() == 'true':
            post.update(add_to_set__well_written=current_user['id'])
        if amazing.lower() == 'true':
            post.update(add_to_set__amazing_story=current_user['id'])

        return True

    @staticmethod
    def submit_assigment(text, assignment_id):

        assignment = Post.objects(user=current_user['id'],
                                  assignment_id=ObjectId(assignment_id))

        if assignment:
            assignment.update(text=text, submit=True)
            return assignment
        else:
            post = Post.create(text, assignment_id)
            post.save()
            return post

    def get_json(self):

        #print(self.user.pk)

        user_name = User.objects(pk=self.user.pk, name__exists=True)
        u_name = 'sample'
        if user_name:
            for u in user_name:
                u_name = u['name']

        dict_list = []
        for i in range(len(self.comments)):
            diction = {'comment': '', 'name': ''}
            diction['comment'] = self.comments[i]
            name = User.objects(pk=self.comments_by[i].pk, name__exists=True)
            another_name = ''
            if name:

                for n in name:
                    another_name = n['name']

            diction['name'] = another_name
            dict_list.append(diction)

        jstring = ''
        jstring = '{\n"text": ' + json.dumps(self.text) + ',\n' \
                  + '"name": ' + json.dumps(str(u_name)) + ',\n' \
                  + '"mongoid": ' + json.dumps(str(self.pk)) + ',\n' \
                  + '"comments": ' + json.dumps(dict_list) + ',\n' \
                  + '"hilarious": ' + json.dumps(str(len(self.hilarious))) + ',\n' \
                  + '"well_written": ' + json.dumps(str(len(self.well_written))) + ',\n' \
                  + '"amazing_story": ' + json.dumps(str(len(self.amazing_story))) + "}\n"

        return jstring

    @staticmethod
    def add_comments(post_id, comment):

        post = Post.objects(pk=ObjectId(post_id)).first()
        if (post):
            Post.objects(pk=ObjectId(post_id)).update_one(
                push__comments=comment, push__comments_by=current_user['id'])
            return post
Beispiel #25
0
class DepositRecord(Document):
    club = ReferenceField(Club)
    value = FloatField()
    handler = ReferenceField(Member)
    remark = StringField()
    meta = {'strict': False}
 class Home(Document):
     dad = ReferenceField(
         AbstractHuman)  # Referencing the abstract class
     address = StringField()
Beispiel #27
0
class Physician(Staff):
    name_initials = StringField(required=True)
    is_licenced = BooleanField()
    reference_number = StringField()
    main_speciality = ReferenceField(MedicalSpeciality, required=True)
    auxiliary_speciality = ListField(ReferenceField(MedicalSpeciality))
 class Human(Document):
     meta = {"abstract": True}
     creator = ReferenceField("self", dbref=True)
class Campaign(Document):
    name = StringField(max_length=100, required=True)
    slug = StringField(required=True, primary_key=True)
    description = StringField()
    creator = ReferenceField('Admin', required=True)
    subscribers = ListField(ReferenceField('Admin'))
    creation_time = DateTimeField(default=datetime.now)
    last_update = DateTimeField(default=datetime.now)
    wiki_page = StringField()
    tasks = ListField(ReferenceField('BaseTask'))
    # either a CSV Corpus or a corpus folder
    corpus: BaseCorpus = ReferenceField('BaseCorpus', required=True)
    # the audio file is being served in the starter zip
    serve_audio = BooleanField(default=False)
    # this object stores the campaign annotation checking scheme
    checking_scheme: TextGridCheckingScheme = ReferenceField(
        'TextGridCheckingScheme')
    # if this is false, textgrid aren't checked (except for the merge part)
    check_textgrids = BooleanField(default=True)
    # updated on trigger
    stats: CampaignStats = EmbeddedDocumentField(CampaignStats)

    def validate(self, clean=True):
        if isinstance(self.corpus, CSVCorpus) and self.serve_audio:
            raise ValidationError("Can't serve audio files with a csv corpus")
        super().validate(clean)

    def launch_gamma_update(self):
        """Launches a subprocess that computes the gamma statistics for
        that campaign. Does not wait for the subprocess to finish"""
        process = subprocess.Popen(["campaign-gamma", self.slug])
        self.stats.gamma_updating = True
        self.stats.can_update_gamma = False
        self.save()

    def update_stats(self, gamma_only=False):
        if self.stats is None:
            self.stats = CampaignStats()
        if gamma_only:
            self.stats.update_gamma_stats(self)
        else:
            self.stats.update_stats(self)
        self.save()

    @classmethod
    def post_delete_cleanup(cls, sender, document: 'Campaign', **kwargs):
        """Called upon a post_delete event. Takes care of cleaning up stuff, deleting the campaigns's
        child tasks and removing notifications related to that campaign"""
        for task in document.tasks:
            task.delete()
        from .users import Notification
        Notification.objects(
            Q(object_id=document.slug)
            & (Q(object_type="campaign") | Q(object_type="dashboard")))

    def tasks_for_file(self, audio_file: str):
        tasks = [task for task in self.tasks]
        return len([task for task in tasks if task.data_file == audio_file])

    @property
    def active_tasks(self):
        return BaseTask.objects(campaign=self.id, is_done=False)

    @property
    def annotators(self):
        return self.stats.annotators

    def gen_template_tg(self, filename: str) -> SingleAnnotatorTextGrid:
        """Generates the template textgrid (pregenerated tiers and tg length)
        for that campaign"""
        audio_file = self.corpus.get_file(filename)
        if self.checking_scheme is None:
            tg = TextGrid(name=filename, maxTime=audio_file.duration)
        else:
            tg = self.checking_scheme.gen_template_tg(audio_file.duration,
                                                      filename)
        return SingleAnnotatorTextGrid.from_textgrid(tg, [self.creator], None)

    def gen_summary_csv(self, only_gamma=False) -> str:
        str_io = StringIO()
        fields = [
            "task_file", "time_created", "time_completed", "time_started",
            "annotators"
        ]
        if self.stats is not None and self.stats.can_compute_gamma:
            write_gamma = True
            for tier_name in self.stats.tiers_gamma.keys():
                fields.append(f"gamma_{tier_name}")
        else:
            write_gamma = False

        csv_writer = csv.DictWriter(str_io, fields, delimiter="\t")
        csv_writer.writeheader()
        for task in self.tasks:
            task: BaseTask
            task_row = {
                "task_file":
                task.data_file,
                "time_created":
                task.creation_time,
                "time_completed":
                task.finish_time,
                "time_started":
                task.start_time,
                "annotators":
                ",".join(annotator.username for annotator in task.annotators)
            }
            if isinstance(task, DoubleAnnotatorTask) and write_gamma:
                for tier_name, gamma in task.tiers_gamma.items():
                    task_row[f"gamma_{tier_name}"] = gamma

            # if we only want the gamma rows, skipping single annotators tasks
            if only_gamma and not isinstance(task, DoubleAnnotatorTask):
                continue

            csv_writer.writerow(task_row)
        str_io.flush()
        return str_io.getvalue()

    def get_full_annots_archive(self) -> bytes:
        """Generates the full annotations zip archive for that campaign, to be
        then sent to the client"""
        buffer = BytesIO()
        # TODO: integrate the csv summary generator from above
        with zipfile.ZipFile(buffer, "w", zipfile.ZIP_STORED) as zfile:
            zip_folder: Path = Path(self.slug)

            # writing full summary
            summary_path = zip_folder / Path("summary.csv")
            zfile.writestr(str(summary_path), self.gen_summary_csv())

            # then writing tasks textgrids and per-task summary
            for task in self.tasks:
                task_annotators = "-".join(
                    [annotator.username for annotator in task.annotators])
                task_datafile = task.data_file.strip(
                    Path(task.data_file).suffix)
                task_folder = (zip_folder / Path(task_datafile) /
                               Path(task_annotators))
                for tg_name, tg_doc in task.textgrids.items():
                    if tg_doc is not None:
                        tg_archpath = task_folder / Path(tg_name + ".TextGrid")
                        zfile.writestr(str(tg_archpath), tg_doc.to_str())

        return buffer.getvalue()

    @property
    def short_profile(self):
        return {"slug": self.slug, "name": self.name}

    @property
    def status(self):
        if self.stats is None:
            self.update_stats()

        return {
            "slug":
            self.slug,
            "name":
            self.name,
            "description":
            self.description,
            "creator":
            self.creator.short_profile,
            "stats":
            self.stats.to_msg(),
            "corpus_path":
            self.corpus.name,
            "tiers_number":
            len(self.checking_scheme.tiers_specs)
            if self.checking_scheme is not None else None,
            "check_textgrids":
            self.check_textgrids,
            "annotators":
            [annotator.short_profile for annotator in self.annotators],
            "subscribers": [user.username for user in self.subscribers],
            "creation_time":
            self.creation_time,
            "last_update_time":
            self.last_update,
        }
Beispiel #30
0
class Rail(Document):
    _created_date = DateTimeField(default=datetime.datetime.now)
    _key_created_user = ObjectIdField()
    _last_modified_date = DateTimeField(default=datetime.datetime.now)
    _key_last_modified_user = ObjectIdField()
    _key_owner_user = ObjectIdField()

    analysis_date = DateTimeField(default=datetime.datetime.now)
    _key_organization = ReferenceField(Organization, required=True, reverse_delete_rule=2)
    _key_brand = ReferenceField(Brand, required=True, reverse_delete_rule=3)
    _key_kur_cinsi = ReferenceField(Currency, required=True, reverse_delete_rule=3)
    _key_iskonto = ReferenceField(Discount, required=True, reverse_delete_rule=3)

    _key_rail_size_1 = ReferenceField(RailSize, required=True, reverse_delete_rule=3)
    _key_rail_size_2 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_size_3 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_size_4 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_size_5 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_size_6 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_size_7 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_size_8 = ReferenceField(RailSize, reverse_delete_rule=1)

    _key_rail_flans_size_1 = ReferenceField(RailSize, required=True, reverse_delete_rule=3)
    _key_rail_flans_size_2 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_flans_size_3 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_flans_size_4 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_flans_size_5 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_flans_size_6 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_flans_size_7 = ReferenceField(RailSize, reverse_delete_rule=1)
    _key_rail_flans_size_8 = ReferenceField(RailSize, reverse_delete_rule=1)

    birim_fiyat = DecimalField(required=True)