예제 #1
0
class WeightModel(Model):
    name = TextField(default='unknown')
    classificator_provider = TextField(blank=False)
    shape = BinaryField(blank=True)
    body = BinaryField()
    accuracy = FloatField(default=-1)
    lambda_value = FloatField(default=-1)
    learn_rate = FloatField(default=-1)
예제 #2
0
class TestData(Resource, TitleMixin):
    """
    测试数据。
    评测机在评测提交时将使用此表中的数据。
    """
    # 编号
    id = BigAutoField(primary_key=True)
    # 所属题元
    meta_problem = ForeignKey(MetaProblem,
                              related_name='test_data',
                              to_field='id')

    # 测试输入的大小
    in_size = IntegerField()
    # 测试输出的大小
    out_size = IntegerField()

    # 测试输入(二进制数据)
    test_in = BinaryField(null=True)
    # 测试输出(二进制数据)
    test_out = BinaryField(null=True)

    # 有多少题目使用了此测试数据
    number_problem = IntegerField(default=0)

    def __str__(self):
        return '<TestData ' + str(self.id) + ' in MetaProblem ' + str(
            self.meta_problem_id) + '>'

    def get_test_in(self):
        """
        读取测试输入,将二进制数据转换为字符串。
        :return: 字符串,测试输入。
        """
        if self.test_in is None:
            return None
        else:
            return bytes(self.test_in).decode('utf-8', 'ignore')[:_INPUT_MAX]

    def get_test_out(self):
        """
        读取测试输出,将二进制数据转换为字符串。
        :return: 字符串,测试输出。
        """
        if self.test_out is None:
            return None
        else:
            return bytes(self.test_out).decode('utf-8', 'ignore')[:_INPUT_MAX]
예제 #3
0
class Test(Model):
    name = CharField(max_length=20)
    owner = ForeignKey(settings.AUTH_USER_MODEL,
                       null=True,
                       blank=True,
                       on_delete=CASCADE)
    public = BooleanField(default=False)
    date = DateField(null=True, blank=True)
    datetime = DateTimeField(null=True, blank=True)
    permission = ForeignKey('auth.Permission',
                            null=True,
                            blank=True,
                            on_delete=CASCADE)

    # We can’t use the exact names `float` or `decimal` as database column name
    # since it fails on MySQL.
    a_float = FloatField(null=True, blank=True)
    a_decimal = DecimalField(null=True,
                             blank=True,
                             max_digits=5,
                             decimal_places=2)
    bin = BinaryField(null=True, blank=True)
    ip = GenericIPAddressField(null=True, blank=True)
    duration = DurationField(null=True, blank=True)
    uuid = UUIDField(null=True, blank=True)

    class Meta(object):
        ordering = ('name', )
예제 #4
0
class Test(Model):
    name = CharField(max_length=20)
    owner = ForeignKey(settings.AUTH_USER_MODEL,
                       null=True,
                       blank=True,
                       on_delete=SET_NULL)
    public = BooleanField(default=False)
    date = DateField(null=True, blank=True)
    datetime = DateTimeField(null=True, blank=True)
    permission = ForeignKey('auth.Permission',
                            null=True,
                            blank=True,
                            on_delete=PROTECT)

    # We can’t use the exact names `float` or `decimal` as database column name
    # since it fails on MySQL.
    a_float = FloatField(null=True, blank=True)
    a_decimal = DecimalField(null=True,
                             blank=True,
                             max_digits=5,
                             decimal_places=2)
    bin = BinaryField(null=True, blank=True)
    ip = GenericIPAddressField(null=True, blank=True)
    duration = DurationField(null=True, blank=True)
    uuid = UUIDField(null=True, blank=True)

    try:
        from django.db.models import JSONField
        json = JSONField(null=True, blank=True)
    except ImportError:
        pass

    class Meta:
        ordering = ('name', )
예제 #5
0
파일: models.py 프로젝트: polz113/bober
class Resource(Model):
    def __str__(self):
        return u"{}: {}".format(self.relative_url, self.file)
    question = ForeignKey('Question', on_delete=CASCADE)
    relative_url = CharField(max_length=255)
    file = FileField(null=True, upload_to='resources')
    resource_type = CharField(max_length=255)
    mimetype = CharField(max_length=255)
    data = BinaryField(null=True)
    part_of_solution = BooleanField(default=False)

    def url(self):
        return self.file.url

    def as_bytes(self):
        s = bytes()
        if self.file:
            self.file.open()
            s = self.file.read()
            self.file.close()
        elif self.data is not None:
            fname = os.path.join('resources', self.question.identifier,
                                 self.relative_url)
            self.file.name = fname
            ensure_dir_exists(self.file.path)
            with open(self.file.path, 'wb') as f:
                f.write(self.data)
            s = self.data
        return s

    def as_base64(self):
        return base64.b64encode(self.as_bytes())
예제 #6
0
class SpecialJudge(Resource, TitleMixin):
    """
    特殊评测。
    由于废弃字段等原因,一道题目下可以出现多个特殊评测代码,未被废弃的编号最大的特殊评测将作为题目的特殊评测,
    若题目下无特殊评测或特殊评测均未废弃状态,则题目被设定为不存在特殊评测。
    """
    # 所属题目
    problem = ForeignKey(Problem, related_name='special_judge', to_field='id')
    # 特殊评测代码是属于哪个编程环境的(HUSTOJ仅支持C/C++的代码)
    environment = ForeignKey('Environment',
                             related_name='special_judge',
                             to_field='id')

    # 特殊评测代码(二进制数据)
    code = BinaryField()

    def __str__(self):
        return '<Special Judge %s of Problem %s>' % (self.id, self.problem.id)

    def get_code(self):
        """
        获取特殊评测代码,将二进制数据转换为字符串。
        :return: 字符串,特殊评测代码。
        """
        return bytes(self.code).decode('utf-8', 'ignore')
예제 #7
0
class AccessToken(Model):
    class ParseError(Exception):
        pass

    # project = models.ForeignKey(Project, on_delete=models.CASCADE)
    project_id = TextField()
    token = BinaryField(max_length=encryption.TOKEN_SIZE)
    valid_until = DateTimeField()

    def is_valid(self, now=None) -> bool:
        if now is None:
            now = now_utc()

        return self.valid_until > now

    @staticmethod
    def _token_by_proj_id(project_id: str):
        return AccessToken.objects.filter(project_id=project_id)

    @staticmethod
    def _purge_old_tokens(project_id: str):
        now = now_utc()

        for token in AccessToken._token_by_proj_id(project_id):
            if not token.is_valid(now):
                token.delete()

    @staticmethod
    def get_project_token(project_id: str) -> Optional["AccessToken"]:
        AccessToken._purge_old_tokens(project_id)
        return AccessToken._token_by_proj_id(project_id).first()

    @staticmethod
    def create_new(project_id: str) -> "AccessToken":
        tok, valid_until = generate_token()
        return AccessToken.objects.create(project_id=project_id,
                                          token=tok,
                                          valid_until=valid_until)

    @staticmethod
    def get_from_base64(b64_token):
        try:
            tok = b64decode(b64_token)
        except (binascii.Error, ValueError):
            # could not parse base64 string
            raise AccessToken.ParseError()

        token = AccessToken.objects.get(token=tok)
        if token is None or not token.is_valid():
            return None

        return token

    def as_base64(self):
        """
        get this token as base64 encoded string
        """
        return b64encode(self.token).decode()
예제 #8
0
class Resource(Model):
    id = UUIDField(primary_key=True, default=uuid4, editable=False)

    name = CharField(max_length=255)
    extension = CharField(max_length=255, blank=True)

    content = BinaryField()
    content_type = CharField(max_length=255)

    created_at = DateTimeField(auto_now_add=True)
예제 #9
0
파일: models.py 프로젝트: PaweuB/concent
class StoredMessage(Model):
    type        = PositiveSmallIntegerField()
    timestamp   = DateTimeField()
    data        = BinaryField()
    task_id     = CharField(max_length = MESSAGE_TASK_ID_MAX_LENGTH, null = True, blank = True)
    subtask_id  = CharField(max_length = MESSAGE_TASK_ID_MAX_LENGTH, null = True, blank = True)
    created_at = DateTimeField(auto_now_add=True)

    def __str__(self):
        return 'StoredMessage #{}, type:{}, {}'.format(self.id, self.type, self.timestamp)
예제 #10
0
파일: models.py 프로젝트: PaweuB/concent
class PendingEthereumTransaction(Model):
    """
    Represents pending Ethereum transaction state.
    """

    nonce = DecimalField(max_digits=78, decimal_places=0)

    gasprice = DecimalField(max_digits=78, decimal_places=0)
    startgas = DecimalField(max_digits=78, decimal_places=0)
    value = DecimalField(max_digits=78, decimal_places=0)

    to = BinaryField(max_length=20)
    data = BinaryField()

    v = IntegerField()
    r = DecimalField(max_digits=78, decimal_places=0)
    s = DecimalField(max_digits=78, decimal_places=0)

    created_at = DateTimeField(auto_now_add=True)
예제 #11
0
class PaymentInfo(Model):
    """
    Stores the information needed to construt a PaymentCommitted message
    """
    class RecipientType(ChoiceEnum):
        Provider = 'provider'
        Requestor = 'requestor'

    payment_ts = DateTimeField()
    task_owner_key = BinaryField()
    provider_eth_account = CharField(max_length=ETHEREUM_ADDRESS_LENGTH)
    amount_paid = IntegerField()
    recipient_type = CharField(max_length=32, choices=RecipientType.choices())
    amount_pending = IntegerField()
    pending_response = ForeignKey(PendingResponse, related_name='payments')

    def clean(self):
        if self.task_owner_key == self.provider_eth_account:
            raise ValidationError({
                'provider_eth_account':
                'Provider ethereum account address must be diffrent than task owner key'
            })

        if not isinstance(self.amount_pending,
                          int) or self.amount_pending <= 0:
            raise ValidationError({
                'amount_pending':
                'Amount pending must be an integer and bigger than 0'
            })

        if not isinstance(self.amount_paid, int) or self.amount_paid < 0:
            raise ValidationError({
                'amount_paid':
                'Amount paid must be an integer and bigger than or equal 0'
            })

        if not isinstance(self.task_owner_key, bytes) or not len(
                self.task_owner_key) == TASK_OWNER_KEY_LENGTH:
            raise ValidationError({
                'task_owner_key':
                f'Task owner key must be a bytes string with {TASK_OWNER_KEY_LENGTH} characters'
            })

        if not isinstance(self.provider_eth_account, str) or not len(
                self.provider_eth_account) == ETHEREUM_ADDRESS_LENGTH:
            raise ValidationError({
                'provider_eth_account':
                f'Provider ethereum account address must be a string with {ETHEREUM_ADDRESS_LENGTH} characters'
            })

        if not isinstance(self.pending_response, PendingResponse):
            raise ValidationError({
                'pending_response':
                'PaymentInfo should be related with Pending Response'
            })
예제 #12
0
def get_destinations(application_id, user_type=None):
    """
    Get destinations for an open application. For gov users they are ordered based on flag priority and alphabetized by name.
    """
    if user_type == UserType.EXPORTER:
        countries_on_application = (CountryOnApplication.objects.
                                    select_related("country").prefetch_related(
                                        "flags", "country__flags").filter(
                                            application=application_id))
    else:
        countries_on_application = (
            CountryOnApplication.objects.select_related(
                "country").prefetch_related(
                    "country__flags",
                    "flags").filter(application=application_id).annotate(
                        highest_flag_priority=Min("country__flags__priority"),
                        contains_flags=Case(When(country__flags__isnull=True,
                                                 then=0),
                                            default=1,
                                            output_field=BinaryField()),
                    ).order_by("-contains_flags", "highest_flag_priority",
                               "country__name"))

    destinations = []

    for coa in countries_on_application:
        destinations.append({
            "id":
            coa.id,
            "country": {
                "id":
                coa.country.id,
                "name":
                coa.country.name,
                "flags": [{
                    "colour": f.colour,
                    "name": f.name,
                    "label": f.label,
                    "id": f.id
                } for f in coa.country.flags.all()
                          if f.status == FlagStatuses.ACTIVE],
            },
            "flags": [{
                "id": f.id,
                "name": f.name
            } for f in coa.flags.all()],
            "contract_types":
            coa.contract_types,
            "other_contract_type_text":
            coa.other_contract_type_text,
        })

    return {"type": "countries", "data": destinations}
예제 #13
0
    def filter_queryset(self, queryset):
        users_team_first = self.request.GET.get("users_team_first", False)
        name = self.request.GET.get("name")

        if name:
            queryset = queryset.filter(name__icontains=name)
        if str_to_bool(users_team_first):
            queryset = queryset.annotate(users_team=(
                Case(When(team=self.request.user.govuser.team, then=1),
                     default=0,
                     output_field=BinaryField()))).order_by("-users_team")

        return queryset
예제 #14
0
class WaitingAction(CremeModel):
    action = CharField(
        _(u'Action'), max_length=100
    )  # Action (i.e: create, update...) # TODO: int instead ??
    # TODO: split into 2 CharFields 'fetcher' & 'input' ?
    # NB: - If default backend (subject="*"): fetcher_name.
    #     - If not  'fetcher_name - input_name'  (i.e: email - raw, email - infopath, sms - raw...).
    source = CharField(_(u'Source'), max_length=100)
    # raw_data = TextField(blank=True, null=True)  # Pickled data
    raw_data = BinaryField(blank=True, null=True)  # Pickled data
    ct = CTypeForeignKey(verbose_name=_(
        u'Type of resource'))  # Redundant, but faster bd recovery
    subject = CharField(_(u'Subject'), max_length=100)
    user = CremeUserForeignKey(verbose_name=_(u'Owner'),
                               blank=True,
                               null=True,
                               default=None)  # If sandbox per user

    class Meta:
        app_label = 'crudity'
        verbose_name = _(u'Waiting action')
        verbose_name_plural = _(u'Waiting actions')

    # def get_data(self):
    @property
    def data(self):
        # data = loads(self.raw_data.encode('utf-8'))
        data = loads(self.raw_data)

        # if isinstance(data, dict):
        #     data = {
        #         k: v.decode('utf8') if isinstance(v, str) else v
        #             for k, v in data.items()
        #     }

        return data

    # def set_data(self, data):
    @data.setter
    def data(self, data):
        self.raw_data = dumps(data)

    def can_validate_or_delete(self, user):
        """self.user not None means that sandbox is by user"""
        if self.user is not None and self.user != user and not user.is_superuser:
            return False, ugettext(
                u'You are not allowed to validate/delete the waiting action <{}>'
            ).format(self.id)

        return True, ugettext(u'OK')
예제 #15
0
파일: actions.py 프로젝트: mrjmad/creme_crm
class WaitingAction(CremeModel):
    # Action (i.e: create, update...) # TODO: int instead ??
    action = CharField(_('Action'), max_length=100)

    # TODO: split into 2 CharFields 'fetcher' & 'input' ?
    # NB: - If default backend (subject="*"): fetcher_name.
    #     - If not  'fetcher_name - input_name'  (eg: email - raw, email - infopath, sms - raw...).
    source = CharField(_('Source'), max_length=100)

    raw_data = BinaryField(blank=True, null=True)  # Pickled data

    # Redundant, but faster bd recovery
    ct = creme_fields.CTypeForeignKey(verbose_name=_('Type of resource'))

    subject = CharField(_('Subject'), max_length=100)

    # If sandbox per user
    user = creme_fields.CremeUserForeignKey(
        verbose_name=_('Owner'),
        blank=True,
        null=True,
        default=None,
    )

    class Meta:
        app_label = 'crudity'
        verbose_name = _('Waiting action')
        verbose_name_plural = _('Waiting actions')

    @property
    def data(self) -> dict:
        return loads(self.raw_data)

    @data.setter
    def data(self, data: dict):
        self.raw_data = dumps(data)

    def can_validate_or_delete(self, user) -> Tuple[bool, str]:
        """self.user not None means that sandbox is by user"""
        if self.user is not None and self.user != user and not user.is_superuser:
            return (
                False,
                gettext(
                    'You are not allowed to validate/delete the waiting action <{}>'
                ).format(self.id))

        return True, gettext('OK')
예제 #16
0
class InstantContent(Model):
    """
    The content that can be easily previewed and transmitted:

        contentType: IntegerField
            1: 'text & image'([pure text] OR [pure image] OR [text + image])
            2: 'location'
            3: 'voice'

        content: BinaryField
            binary version of the content, it should contain all the information of the InstantContent

        compressionMethod: CharField
            1: 'gzip'

        textVersionContent: TextField
            text version of the content, the value should be:
                if the type is pure text:
                    the original pure text
                if the type is pure image:
                    blank
                if the type is text + image:
                    only the text content
                if the type is location:
                    GPS: (xxx.xxx,yyy.yyy,zzz.zzz)
                    Location: provided by the user or Google API
                if the type is voice:
                    the converted text content
    """
    class ContentType(IntegerChoices):
        TYPE_TEXT_IMAGE = 1, _("Text & Image")
        TYPE_LOCATION = 2, _("Location")
        TYPE_VOICE = 3, _("Voice")

    class CompressionMethod(IntegerChoices):
        TYPE_GZIP = 1, _("gzip")

    user = ForeignKey(to=RecordedUser, on_delete=CASCADE, null=True)
    contentType = IntegerField(choices=ContentType.choices, null=True)
    content = BinaryField(null=True)
    compressionMethod = IntegerField(choices=CompressionMethod.choices,
                                     null=True)
    textVersionContent = TextField(null=True)

    def _str_(self):
        return self.title
예제 #17
0
class RequestProfile(Model):
    started_at = DateTimeField()
    created_at = DateTimeField()
    request_user = ForeignKey(User, on_delete=SET_NULL, null=True)
    request_path = TextField(blank=True)
    response_code = IntegerField()
    time_real = FloatField()
    time_user = FloatField()
    time_sys = FloatField()
    allocated_vm = BigIntegerField()
    peak_rss_use = BigIntegerField()
    data = BinaryField(null=True)
    data_json = BooleanField()
    data_path = TextField(blank=True)
    size_base = BigIntegerField(null=True)
    size_gzip = BigIntegerField(null=True)
    size_json = BigIntegerField(null=True)
예제 #18
0
class HubUser(AbstractUser):
    """
    The HubUser is basically the standard Django user, but has an additional encrypted shared TOTP secret
    """

    totp_secret = BinaryField(
        _('TOTP Secret'),
        max_length=1024, null=True, blank=False,
        default=create_encrypted_random_totp_secret,
        help_text=_('Encrypted TOTP Secret')
    )

    def set_totp_secret(self, secret: bytes):
        """
        Set the TOTP Secret

        The TOTP Secret will be encrypted before it is saved to the database.

        :param bytes secret: The TOTP secret to be encrypted and saved to the database
        """
        if len(secret) < 32:
            raise ValueError(_('Secret must be at least 32 bytes long'))
        if len(secret) > 96:
            raise ValueError(_('Secret must not be larger than 96 bytes'))
        self.totp_secret = SymmetricCrypt().encrypt(secret)

    def get_totp_secret(self) -> Optional[bytes]:
        """
        Get the TOTP Secret

        This method gives you the unencrypted (decrypted) TOTP secret

        :rtype: Optional[bytes]
        :returns: The unencrypted secret or None if no secret is set
        """
        if self.totp_secret is None:
            return None
        return SymmetricCrypt().decrypt(self.totp_secret)

    def __str__(self):
        return '{}'.format(self.username)
예제 #19
0
class Activity(Model):

    ap_id = TextField()
    payload = BinaryField()
    created_at = DateField(auto_now_add=True)
    person = ForeignKey(Person, related_name='activities', on_delete=CASCADE)
    remote = BooleanField(default=False)

    @property
    def uris(self):
        if self.remote:
            ap_id = self.ap_id
        else:
            ap_id = uri("activity", self.person.username, self.id)
        return URIs(id=ap_id)

    def to_activitystream(self):
        payload = self.payload.decode("utf-8")
        data = json.loads(payload)
        data.update({"id": self.uris.id})
        return data
예제 #20
0
class Event(Model):
    ctime = DateTimeField(auto_now_add=True, null=True)

    # Identify the object in the database and its version
    xref = CharField(_(u'External Ref'), max_length=XREF_LENGTH)
    xbus_message_correlation_id = CharField(_(u'Message correlation id'),
                                            max_length=36)

    # Event type
    event_type = CharField(_(u'Event type'), max_length=80)
    event_id = CharField(_(u'Event id'), max_length=80, null=True, blank=True)

    # Direction : incoming or outgoing
    DIRECTION_CHOICES = (
        ('in', _(u'Incoming')),
        ('out', _(u'Outgoing')),
        ('immediate-out', _(u'Immediate-Out')),
    )
    direction = CharField(_(u'Direction'),
                          max_length=25,
                          choices=DIRECTION_CHOICES)

    # State
    STATE_CHOICES = (
        ('pending', _(u'Pending')),
        ('done', u'Done'),
        ('error', _(u'Error')),
    )
    state = CharField(_(u'State'), max_length=20, choices=STATE_CHOICES)

    comment = TextField(_(u'Comment'), blank=True)

    # Binary in msgpack format
    item = BinaryField(_(u'Event item'))

    admin_url = CharField(max_length=250,
                          default="",
                          editable=False,
                          null=True)
예제 #21
0
class PaymentInfo(Model):
    """
    Stores the information needed to construt a PaymentCommitted message
    """
    class RecipientType(ChoiceEnum):
        Provider = 'provider'
        Requestor = 'requestor'

    payment_ts = DateTimeField()
    task_owner_key = BinaryField(validators=[
        MinLengthValidator(TASK_OWNER_KEY_LENGTH),
        MaxLengthValidator(TASK_OWNER_KEY_LENGTH),
    ])
    provider_eth_account = CharField(
        max_length=ETHEREUM_ADDRESS_LENGTH,
        validators=[MinLengthValidator(ETHEREUM_ADDRESS_LENGTH)])
    amount_paid = DecimalField(max_digits=BIG_ENDIAN_INT_MAX_DIGITS,
                               decimal_places=0,
                               validators=[MinValueValidator(0)])
    recipient_type = CharField(max_length=32, choices=RecipientType.choices())
    amount_pending = DecimalField(max_digits=BIG_ENDIAN_INT_MAX_DIGITS,
                                  decimal_places=0,
                                  validators=[MinValueValidator(0)])

    @property
    def amount_paid_as_int(self) -> int:
        return int(self.amount_paid)

    @property
    def amount_pending_as_int(self) -> int:
        return int(self.amount_pending)

    def clean(self) -> None:
        if self.task_owner_key == self.provider_eth_account:
            raise ValidationError({
                'provider_eth_account':
                'Provider Ethereum account address must be different than task owner key.'
            })
예제 #22
0
class WebhookData(ConcurrentTransitionMixin, Model):
    """Abstract base class for webhook data."""
    class Meta:
        app_label = APP_LABEL
        abstract = True

    NEW = STATE.NEW
    PROCESSING = STATE.PROCESSING
    PROCESSED = STATE.PROCESSED
    ERROR = STATE.ERROR

    CHOICES = STATE.CHOICES

    # We know we always want to record the webhook source, and the
    # date we received it.
    status = FSMIntegerField(choices=CHOICES, default=NEW, protected=True)
    source = GenericIPAddressField(null=True)
    received = DateTimeField(default=timezone.now)
    headers = JSONField()
    # This is for storing the webhook payload exactly as received
    # (i.e. from request.body), which comes in handy for signature
    # verification.
    body = BinaryField()

    @transition(field=status, source=NEW, target=PROCESSING, on_error=ERROR)
    def start_processing(self):
        logger.debug('Processing webhook %s' % self.id)

    @transition(field=status,
                source=PROCESSING,
                target=PROCESSED,
                on_error=ERROR)
    def finish_processing(self):
        logger.debug('Finishing webhook %s' % self.id)

    @transition(field=status, source=PROCESSING, target=ERROR)
    def fail(self):
        logger.debug('Failed to process webhook %s' % self.id)
예제 #23
0
class RootKey(TimestampedModel):
    """A root key for signing macaroons."""

    id = BigAutoField(primary_key=True, verbose_name='ID')
    material = BinaryField()
    expiration = DateTimeField()
예제 #24
0
class OToolkitDevice(Device):
    """
    Abstract model for a :mod:`oath_toolkit`-based django-otp_ ``Device``.

    .. _django-otp: https://pypi.python.org/pypi/django-otp

    .. attribute:: secret

        Binary-encoded secret data. In
        :class:`django_otp.plugins.otp_totp.models.TOTPDevice`, this field is
        named ``key``, but this is a suboptimal choice, as ``key`` is a
        SQL keyword.

        Defaults to 40 random bytes.

        :type: :class:`django.db.models.BinaryField`

    .. attribute:: window

        The number of OTPs before and after the start OTP to test. In
        :class:`django_otp.plugins.otp_totp.models.TOTPDevice`, this field is
        named ``tolerance``.


        Defaults to ``1``.

        :type: :class:`django.db.models.PositiveSmallIntegerField`

    .. attribute:: digits

        The number of digits in the token.

        Defaults to ``6``.

        :type: :class:`django.db.models.PositiveSmallIntegerField`
    """

    secret = BinaryField(max_length=SECRET_SIZE, default=_random_data)
    window = PositiveSmallIntegerField(default=1)
    digits = PositiveSmallIntegerField(default=6, choices=[(6, 6), (8, 8)])

    class Meta:
        abstract = True

    def secret_qrcode(self, request):
        """
        QR code image based on the secret.

        :rtype: :class:`qrcode.image.base.BaseImage`
        """
        site = get_current_site(request)
        return qrcode.generate(self.oath_type,
                               self.secret,
                               self.user.username,
                               site.name,
                               border=2,
                               box_size=4)

    @property
    def secret_base32(self):
        """
        The secret, in a human-readable Base32-encoded string.

        :type: bytes
        """
        return self.oath.base32_encode(self.secret, human_readable=True)

    @secret_base32.setter
    def secret_base32(self, value):
        self.secret = self.oath.base32_decode(value)

    @property
    def secret_hex(self):
        """
        The secret, in a hex-encoded string.

        :type: bytes
        """
        return hexlify(self.secret)

    @secret_hex.setter
    def secret_hex(self, value):
        self.secret = unhexlify(value)

    @property
    def oath(self):
        if not hasattr(self, '_oath'):
            self._oath = OATH()
        return self._oath

    def _do_verify_token(self, token, validator_func, *args):
        token = bytes(token)
        if len(token) != self.digits:
            token = token.rjust(self.digits, b'0')
        args += (self.window, token)
        try:
            return validator_func(bytes(self.secret), *args)
        except OATHError:
            return False
예제 #25
0
class File(models.Model):

    objects = FileManager()

    name = models.CharField(
        max_length=255, unique=True, blank=False, null=False, db_index=True
    )

    size = models.PositiveIntegerField(db_index=True, blank=False, null=False)

    content = BinaryField(blank=False, null=False)

    created_datetime = models.DateTimeField(
        db_index=True, default=timezone.now, verbose_name="Created datetime"
    )

    _content_hash = models.CharField(
        db_column="content_hash", db_index=True, max_length=128, blank=True, null=True
    )

    class Meta:
        db_table = "binary_database_files_file"

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):

        # Check for and clear old content hash.
        if self.id:
            old = File.objects.get(id=self.id)
            if old.content != self.content:
                self._content_hash = None

        # Recalculate new content hash.
        self.content_hash

        return super(File, self).save(*args, **kwargs)

    @property
    def content_hash(self):
        if not self._content_hash and self.content:
            self._content_hash = utils.get_text_hash(self.content)
        return self._content_hash

    def dump(self, check_hash=False):
        """
        Writes the file content to the filesystem.

        If check_hash is true, clears the stored file hash and recalculates.
        """
        if is_fresh(self.name, self._content_hash):
            return
        write_file(self.name, self.content, overwrite=True)
        if check_hash:
            self._content_hash = None
        self.save()

    @classmethod
    def dump_files(cls, debug=True, verbose=False):
        """
        Writes all files to the filesystem.
        """
        if debug:
            tmp_debug = settings.DEBUG
            settings.DEBUG = False
        try:
            q = cls.objects.only("id", "name", "_content_hash").values_list(
                "id", "name", "_content_hash"
            )
            total = q.count()
            if verbose:
                print("Checking %i total files..." % (total,))
            i = 0
            for (file_id, name, content_hash) in q.iterator():
                i += 1
                if verbose and not i % 100:
                    print("%i of %i" % (i, total))
                if not is_fresh(name=name, content_hash=content_hash):
                    if verbose:
                        print(
                            ("File %i-%s is stale. Writing to local file " "system...")
                            % (file_id, name)
                        )
                    f = File.objects.get(id=file_id)
                    write_file(f.name, f.content, overwrite=True)
                    f._content_hash = None
                    f.save()
        finally:
            if debug:
                settings.DEBUG = tmp_debug
예제 #26
0
    def __init__(self, *args, **kwargs):
        self.profile = kwargs.pop('profile')
        self.read_only = kwargs.pop('read_only', False)
        self.modified_venue = kwargs.pop('dirty', None)
        super().__init__(*args, **kwargs)

        PLACE, FAMILY_MEMBERS, PHONE, PUBLIC_EMAIL = (vis.type() for vis in [
            VisibilitySettingsForPlace,
            VisibilitySettingsForFamilyMembers,
            VisibilitySettingsForPhone,
            VisibilitySettingsForPublicEmail,
        ])
        # Gathering all data items linked to the profile.
        what = Q()
        owned_places = self.profile.owned_places.exclude(
            deleted=True).prefetch_related('family_members')
        what |= Q(model_type=PLACE, place__in=owned_places)
        what |= Q(model_type=FAMILY_MEMBERS,
                  family_members__in=[
                      place.pk for place in owned_places
                      if len(place.family_members_cache()) != 0
                      and not place.family_is_anonymous
                  ])
        what |= Q(model_type=PHONE,
                  phone__in=self.profile.phones.exclude(deleted=True))
        what |= Q(model_type=PUBLIC_EMAIL,
                  profile=self.profile) if self.profile.email else Q()
        qs = VisibilitySettings.objects.filter(what)
        # Forcing a specific sort order: places (with their corresponding family members),
        # then phones, then a public email if present.
        qs = qs.annotate(
            level_primary=Case(
                When(Q(model_type=PLACE) | Q(model_type=FAMILY_MEMBERS),
                     then=1),
                When(model_type=PHONE, then=2),
                When(model_type=PUBLIC_EMAIL, then=3),
                default=9,
                output_field=BinaryField(),
            ),
            level_secondary=Case(
                When(model_type=PLACE, then=10),
                When(model_type=FAMILY_MEMBERS, then=11),
                output_field=BinaryField(),
            ),
        )
        # Preparing presentation settings.
        qs = qs.annotate(
            hint=Case(
                When(model_type=PLACE, then=Value(str(_("A place in")))),
                When(model_type=PHONE, then=Value(str(_("Phone number")))),
                When(model_type=PUBLIC_EMAIL,
                     then=Value(str(_("Email address")))),
                output_field=CharField(),
            ),
            indent=Case(
                When(model_type=FAMILY_MEMBERS, then=True),
                default=False,
                output_field=BooleanField(),
            ),
        )
        self.queryset = qs.order_by('level_primary', 'model_id',
                                    'level_secondary')
예제 #27
0
class AsWKB(GeoFunc):
    output_field = BinaryField()
    arity = 1
예제 #28
0
 def test_BinaryField(self):
     lazy_func = lazy(lambda: b"", bytes)
     self.assertIsInstance(BinaryField().get_prep_value(lazy_func()), bytes)
예제 #29
0
    def search(  # noqa
        self,
        queue_id=None,
        is_work_queue=None,
        user=None,
        status=None,
        case_type=None,
        assigned_user=None,
        case_officer=None,
        include_hidden=None,
        organisation_name=None,
        case_reference=None,  # gov case number
        exporter_application_reference=None,
        exporter_site_name=None,
        exporter_site_address=None,
        control_list_entry=None,
        flags=None,
        country=None,
        team_advice_type=None,
        final_advice_type=None,
        min_sla_days_remaining=None,
        max_sla_days_remaining=None,
        submitted_from=None,
        submitted_to=None,
        finalised_from=None,
        finalised_to=None,
        party_name=None,
        party_address=None,
        goods_related_description=None,
        sla_days_elapsed_sort_order=None,
        sla_days_elapsed=None,
        **kwargs,
    ):
        """
        Search for a user's available cases given a set of search parameters.
        """
        case_qs = (self.submitted().select_related(
            "status", "case_type").prefetch_related(
                "case_assignments",
                "case_assignments__user",
                "case_assignments__queue",
            ))

        if not include_hidden and user:
            EcjuQuery = get_model("cases", "ecjuquery")
            CaseReviewDate = get_model("cases", "casereviewdate")

            case_qs = case_qs.exclude(id__in=EcjuQuery.objects.filter(
                raised_by_user__team_id=user.team.id,
                responded_at__isnull=True).values("case_id").distinct())

            # We hide cases that have a next review date that is set in the future (for your team)
            case_qs = case_qs.exclude(id__in=CaseReviewDate.objects.filter(
                team_id=user.team.id,
                next_review_date__gt=timezone.localtime().date()).values(
                    "case_id"))

        if queue_id and user:
            case_qs = case_qs.filter_based_on_queue(queue_id=queue_id,
                                                    team_id=user.team.id,
                                                    user=user)

        if status:
            case_qs = case_qs.has_status(status=status)

        if case_type:
            case_type = CaseTypeEnum.reference_to_id(case_type)
            case_qs = case_qs.is_type(case_type=case_type)

        if assigned_user:
            if assigned_user == self.NOT_ASSIGNED:
                case_qs = case_qs.not_assigned_to_any_user()
            else:
                case_qs = case_qs.assigned_to_user(user=assigned_user)

        if case_officer:
            if case_officer == self.NOT_ASSIGNED:
                case_officer = None
            case_qs = case_qs.assigned_as_case_officer(user=case_officer)

        if case_reference:
            case_qs = case_qs.with_case_reference_code(case_reference)

        if exporter_application_reference:
            case_qs = case_qs.with_exporter_application_reference(
                exporter_application_reference)

        if organisation_name:
            case_qs = case_qs.with_organisation(organisation_name)

        if exporter_site_name:
            case_qs = case_qs.with_exporter_site_name(exporter_site_name)

        if exporter_site_address:
            case_qs = case_qs.with_exporter_site_address(exporter_site_address)

        if control_list_entry:
            case_qs = case_qs.with_control_list_entry(control_list_entry)

        if flags:
            case_qs = case_qs.with_flags(flags)

        if country:
            case_qs = case_qs.with_country(country)

        if team_advice_type:
            case_qs = case_qs.with_advice(team_advice_type, AdviceLevel.TEAM)

        if final_advice_type:
            case_qs = case_qs.with_advice(final_advice_type, AdviceLevel.FINAL)

        if min_sla_days_remaining or max_sla_days_remaining:
            case_qs = case_qs.with_sla_days_range(
                min_sla=min_sla_days_remaining, max_sla=max_sla_days_remaining)

        if sla_days_elapsed:
            case_qs = case_qs.with_sla_days_elapsed(sla_days_elapsed)

        if submitted_from or submitted_to:
            case_qs = case_qs.with_submitted_range(
                submitted_from=submitted_from, submitted_to=submitted_to)

        if finalised_from or finalised_to:
            case_qs = case_qs.with_finalised_range(
                finalised_from=finalised_from, finalised_to=finalised_to)

        if party_name:
            case_qs = case_qs.with_party_name(party_name)

        if party_address:
            case_qs = case_qs.with_party_address(party_address)

        if goods_related_description:
            case_qs = case_qs.with_goods_related_description(
                goods_related_description)

        if is_work_queue:
            case_qs = case_qs.annotate(case_order=Case(
                When(baseapplication__hmrcquery__have_goods_departed=False,
                     then=0),
                default=1,
                output_field=BinaryField(),
            ))

            case_qs = case_qs.order_by("case_order", "submitted_at")
        else:
            case_qs = case_qs.order_by_date()

        if sla_days_elapsed_sort_order:
            if sla_days_elapsed_sort_order == SortOrder.ASCENDING:
                case_qs = case_qs.order_by("sla_days")
            else:
                case_qs = case_qs.order_by("-sla_days")

        return case_qs.distinct()
예제 #30
0
    def get_ordered_parties(self, instance, party_type):
        """
        Order the parties based on destination flag priority and where the party has
        no flag, by destination (party/country depending on standard/open application) name.

        """
        parties_on_application = (
            instance.all_parties()
            .filter(party__type=party_type, deleted_at__isnull=True)
            .annotate(
                highest_flag_priority=Min("party__flags__priority"),
                contains_flags=Case(When(party__flags__isnull=True, then=0), default=1, output_field=BinaryField()),
            )
            .order_by("-contains_flags", "highest_flag_priority", "party__name")
        )

        parties = [PartySerializer(poa.party).data for poa in parties_on_application]

        return parties