Пример #1
0
    def __init__(self, to, to_field=None, **kwargs):
        try:
            to_name = to._meta.object_name.lower()
        except AttributeError: # to._meta doesn't exist, so it must be RECURSIVE_RELATIONSHIP_CONSTANT
            assert isinstance(to, basestring), "OneToOneField(%r) is invalid. First parameter to OneToOneField must be either a model, a model name, or the string %r" % (to, RECURSIVE_RELATIONSHIP_CONSTANT)
        else:
            to_field = to_field or to._meta.pk.name
        kwargs['verbose_name'] = kwargs.get('verbose_name', '')

        if kwargs.has_key('edit_inline_type'):
            import warnings
            warnings.warn("edit_inline_type is deprecated. Use edit_inline instead.")
            kwargs['edit_inline'] = kwargs.pop('edit_inline_type')

        kwargs['rel'] = OneToOneRel(to, to_field,
            num_in_admin=kwargs.pop('num_in_admin', 0),
            edit_inline=kwargs.pop('edit_inline', False),
            related_name=kwargs.pop('related_name', None),
            limit_choices_to=kwargs.pop('limit_choices_to', None),
            lookup_overrides=kwargs.pop('lookup_overrides', None),
            raw_id_admin=kwargs.pop('raw_id_admin', False))
        kwargs['primary_key'] = True
        IntegerField.__init__(self, **kwargs)

        self.db_index = True
Пример #2
0
 def test_check_constraints(self):
     """
     Tests creating/deleting CHECK constraints
     """
     # Create the tables
     with connection.schema_editor() as editor:
         editor.create_model(Author)
     # Ensure the constraint exists
     constraints = connection.introspection.get_constraints(connection.cursor(), Author._meta.db_table)
     for name, details in constraints.items():
         if details["columns"] == ["height"] and details["check"]:
             break
     else:
         self.fail("No check constraint for height found")
     # Alter the column to remove it
     new_field = IntegerField(null=True, blank=True)
     new_field.set_attributes_from_name("height")
     with connection.schema_editor() as editor:
         editor.alter_field(Author, Author._meta.get_field_by_name("height")[0], new_field, strict=True)
     constraints = connection.introspection.get_constraints(connection.cursor(), Author._meta.db_table)
     for name, details in constraints.items():
         if details["columns"] == ["height"] and details["check"]:
             self.fail("Check constraint for height found")
     # Alter the column to re-add it
     with connection.schema_editor() as editor:
         editor.alter_field(Author, new_field, Author._meta.get_field_by_name("height")[0], strict=True)
     constraints = connection.introspection.get_constraints(connection.cursor(), Author._meta.db_table)
     for name, details in constraints.items():
         if details["columns"] == ["height"] and details["check"]:
             break
     else:
         self.fail("No check constraint for height found")
Пример #3
0
    def test_alter_implicit_id_to_explicit(self):
        """
        Should be able to convert an implicit "id" field to an explicit "id"
        primary key field.
        """
        with connection.schema_editor() as editor:
            editor.create_model(Author)

        new_field = IntegerField(primary_key=True)
        new_field.set_attributes_from_name("id")
        new_field.model = Author
        with connection.schema_editor() as editor:
            editor.alter_field(
                Author,
                Author._meta.get_field_by_name("id")[0],
                new_field,
                strict=True,
            )
Пример #4
0
 def test_add_field(self):
     """
     Tests adding fields to models
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(Author)
     # Ensure there's no age field
     columns = self.column_classes(Author)
     self.assertNotIn("age", columns)
     # Add the new field
     new_field = IntegerField(null=True)
     new_field.set_attributes_from_name("age")
     with connection.schema_editor() as editor:
         editor.add_field(Author, new_field)
     # Ensure the field is right afterwards
     columns = self.column_classes(Author)
     self.assertEqual(columns["age"][0], "IntegerField")
     self.assertEqual(columns["age"][1][6], True)
Пример #5
0
    def test_spanning_relations_with_custom_lookup_in_search_fields(self):
        hype = Group.objects.create(name='The Hype')
        concert = Concert.objects.create(name='Woodstock', group=hype)
        vox = Musician.objects.create(name='Vox', age=20)
        Membership.objects.create(music=vox, group=hype)
        # Register a custom lookup on IntegerField to ensure that field
        # traversing logic in ModelAdmin.get_search_results() works.
        IntegerField.register_lookup(Exact, 'exactly')
        try:
            m = ConcertAdmin(Concert, custom_site)
            m.search_fields = ['group__members__age__exactly']

            request = self.factory.get('/', data={SEARCH_VAR: '20'})
            cl = m.get_changelist_instance(request)
            self.assertCountEqual(cl.queryset, [concert])

            request = self.factory.get('/', data={SEARCH_VAR: '21'})
            cl = m.get_changelist_instance(request)
            self.assertCountEqual(cl.queryset, [])
        finally:
            IntegerField._unregister_lookup(Exact, 'exactly')
Пример #6
0
    def test_alter_implicit_id_to_explicit(self):
        """
        Should be able to convert an implicit "id" field to an explicit "id"
        primary key field.
        """
        with connection.schema_editor() as editor:
            editor.create_model(Author)

        new_field = IntegerField(primary_key=True)
        new_field.set_attributes_from_name("id")
        new_field.model = Author
        with connection.schema_editor() as editor:
            editor.alter_field(
                Author,
                Author._meta.get_field("id"),
                new_field,
                strict=True,
            )

        # This will fail if DROP DEFAULT is inadvertently executed on this
        # field which drops the id sequence, at least on PostgreSQL.
        Author.objects.create(name='Foo')
Пример #7
0
 def get_chat_sessions(self, request, enroll_code, courseUnit):
     return Chat.objects.filter(
         enroll_code=enroll_code,
         user=request.user,
         instructor=courseUnit.course.addedBy,
         is_live=False,
         is_test=self.tester_mode,
         is_preview=False
     ).annotate(
         not_finished=Case(
             When(state_id__isnull=True, then=0),
             When(state_id__isnull=False, then=1),
             default=0,
             output_field=IntegerField())
     ).order_by('-not_finished', '-last_modify_timestamp')
Пример #8
0
class StrIndex(Func):
    """
    Return a positive integer corresponding to the 1-indexed position of the
    first occurrence of a substring inside another string, or 0 if the
    substring is not found.
    """
    function = 'INSTR'
    arity = 2
    output_field = IntegerField()

    def as_postgresql(self, compiler, connection, **extra_context):
        return super().as_sql(compiler,
                              connection,
                              function='STRPOS',
                              **extra_context)
Пример #9
0
    def test_alter_field_nullability_change_raise_not_support_error(self):
        """
        Tests altering nullability of existing field in table
        """
        with DatabaseSchemaEditor(self.connection) as schema_editor:
            schema_editor.execute = mock.MagicMock()

            def constraint_names(*args, **kwargs):
                return ["num_unique"]

            schema_editor._constraint_names = constraint_names
            old_field = IntegerField(null=True)
            old_field.set_attributes_from_name("num")
            new_field = IntegerField()
            new_field.set_attributes_from_name("author_num")
            with self.assertRaises(NotSupportedError):
                schema_editor.alter_field(Author, old_field, new_field)
class Transcript(models.Model):
    episode_number = IntegerField(primary_key=True)
    date_published = CharField(max_length=10)
    link_to_mp3 = models.CharField(max_length=242)
    link_to_podcast = models.CharField(max_length=242)
    idd = models.CharField(max_length=240)
    status = models.CharField(max_length=245)
    text = models.TextField()
    words = models.JSONField()

    class Meta:
        verbose_name_plural = 'Transcripts'

    def __str__(self):
        return self.date_published
Пример #11
0
class Count(Aggregate):
    function = 'COUNT'
    name = 'Count'
    output_field = IntegerField()
    allow_distinct = True

    def __init__(self, expression, filter=None, **extra):
        if expression == '*':
            expression = Star()
        if isinstance(expression, Star) and filter is not None:
            raise ValueError('Star cannot be used with filter. Please specify a field.')
        super().__init__(expression, filter=filter, **extra)

    def convert_value(self, value, expression, connection):
        return 0 if value is None else value
Пример #12
0
class Card(Model):

    class Meta:
        ordering = ["card_no"]

    card_no = IntegerField(blank=True, null=True)
    card_set = ForeignKey(CardSet)
    name = CharField(max_length=255)
    url = URLField()
    card_type = ForeignKey(CardType)
    rarity = ForeignKey(Rarity)

    @property
    def instances(self):
        return self.cardinstance_set.all().order_by("variant")
Пример #13
0
    def test_alter_field_change_null_with_multiple_index_error(self):
        """
        Tests altering nullability of field with multiple index not supported
        """
        with DatabaseSchemaEditor(self.connection) as schema_editor:
            schema_editor.execute = mock.MagicMock()

            def constraint_names(*args, **kwargs):
                return ["num_unique", "dummy_index"]

            schema_editor._constraint_names = constraint_names
            old_field = IntegerField(null=True, db_index=True)
            old_field.set_attributes_from_name("num")
            new_field = IntegerField()
            new_field.set_attributes_from_name("author_num")
            with self.assertRaises(NotSupportedError):
                schema_editor.alter_field(Author, old_field, new_field)
Пример #14
0
 def test_convert_values_to_handle_null_value(self):
     database_operations = DatabaseOperations(connection)
     self.assertEqual(
         None,
         database_operations.convert_values(None,
                                            AutoField(primary_key=True)))
     self.assertEqual(None,
                      database_operations.convert_values(None, DateField()))
     self.assertEqual(
         None, database_operations.convert_values(None, DateTimeField()))
     self.assertEqual(
         None, database_operations.convert_values(None, DecimalField()))
     self.assertEqual(
         None, database_operations.convert_values(None, IntegerField()))
     self.assertEqual(None,
                      database_operations.convert_values(None, TimeField()))
Пример #15
0
class mergetask(models.Model):
    taskid = CharField(max_length=100, primary_key=True)
    createtime = DateTimeField()
    src1 = CharField(max_length=100)
    src2 = CharField(max_length=100)
    dst = CharField(max_length=100)
    # 1 Not transcoding, 2 transcoding ,3 transcoding sucess , 8 transcoding Failed
    status = IntegerField()

    def __unicode__(self):
        # 在Python3中使用 def __str__(self)
        return self.taskid

    def getTask(self):

        return self.src1
Пример #16
0
class Ord(Transform):
    function = "ASCII"
    lookup_name = "ord"
    output_field = IntegerField()

    def as_mysql(self, compiler, connection, **extra_context):
        return super().as_sql(compiler,
                              connection,
                              function="ORD",
                              **extra_context)

    def as_sqlite(self, compiler, connection, **extra_context):
        return super().as_sql(compiler,
                              connection,
                              function="UNICODE",
                              **extra_context)
Пример #17
0
class Ord(Transform):
    function = 'ASCII'
    lookup_name = 'ord'
    output_field = IntegerField()

    def as_mysql(self, compiler, connection, **extra_context):
        return super().as_sql(compiler,
                              connection,
                              function='ORD',
                              **extra_context)

    def as_sqlite(self, compiler, connection, **extra_context):
        return super().as_sql(compiler,
                              connection,
                              function='UNICODE',
                              **extra_context)
Пример #18
0
class Source(models.Model):        
    TYPE_PDF            = 1
    TYPE_YOUTUBE        = 2
    TYPE_HTML5VIDEO     = 3
    TYPE_HTML5          = 4
    TYPES               = ((TYPE_PDF, "PDF"), (TYPE_YOUTUBE, "YOUTUBE"), (TYPE_HTML5VIDEO, "HTML5VIDEO"), (TYPE_HTML5, "HTML5"))     
    title               = CharField(max_length=255, default="untitled")         # old: title text
    submittedby         = ForeignKey(User, blank=True, null=True)               # old: submittedby integer
    numpages            = IntegerField(default=0)
    w                   = IntegerField(default=0)                               # old: ncols integer
    h                   = IntegerField(default=0)                               # old: nrows integer
    rotation            = IntegerField(default=0)                               # new
    version             = IntegerField(default=0)                               #incremented when adding src
    type                = IntegerField(choices=TYPES, default=TYPE_PDF)
    x0                  = IntegerField(default=0)                               #x-coordinate of lower-left corner of trimbox 
    y0                  = IntegerField(default=0)                               #y-coordinate of lower-left corner of trimbox 
    def __unicode__(self):
        return "%s %s: %s" % (self.__class__.__name__,self.id,  self.title)
Пример #19
0
class Count(abc.ABC, Func):
    """
    Custom SQL `COUNT` function without `GROUP BY`.

    This SQL `COUNT` directly inherits from Func() instead of Aggregate
    like django's default COUNT function. This way the `GROUP BY` order
    isn't added.
    """

    name = "Count"
    function = "COUNT"

    output_field = IntegerField()
    allow_distinct = True

    def __init__(self, expression, **extra):
        """
        Overridden to check for filtering star expressions.

        :param expression: The SQL function expression
        :type expression: django.db.models.expressions.Expression | str

        :param extra: Additional keyword arguments to pass on
        :type extra: any
        """
        expression = Star() if expression == "*" else expression
        has_filter = "filter" in extra and extra["filter"] is not None

        if isinstance(expression, Star) and has_filter:
            raise ValueError("Star cannot be used with filter.")

        Func.__init__(self, expression, filter=filter, **extra)

    def convert_value(self, value, expression, connection):
        """
        Sanitize NULL value's.

        :param value: The value to sanitize
        :type value: int | None

        :param expression: The currently used function expression
        :type expression: django.db.models.expressions.Expression

        :param connection: The current connection proxy
        :type connection: django.db.DefaultConnectionProxy
        """
        return 0 if value is None else value
Пример #20
0
    def list(self, request, *args, **kwargs):
        if request.query_params.get('offline') == 'true':

            queryset = self.filter_queryset(Asset.objects.all().annotate(
                rack_letter=Cast('asset_number', CharField())).annotate(
                    numstr_in_rack=Cast('asset_number', CharField())).annotate(
                        number_in_racknum=Cast('asset_number', IntegerField(
                        ))).exclude(datacenter__is_offline=False))
            # Asset.objects.all() \
            #     .annotate(rack_letter=Substr('rack__rack_number', 1, 1)) \
            #     .annotate(numstr_in_rack=Substr('rack__rack_number', 2))
            # queryset = queryset.annotate(number_in_racknum=Cast('numstr_in_rack', IntegerField()))
            # self.filter_queryset(self.get_queryset())
            if request.query_params.get('export') == 'true':
                if request.query_params.get('np') == 'true':
                    return export_network_ports(queryset)
                return export_assets(queryset)

            page = self.paginate_queryset(queryset)
            if page is not None:
                serializer = self.get_serializer(page, many=True)
                return self.get_paginated_response(serializer.data)
            serializer = self.get_serializer(queryset, many=True)
            return Response(serializer.data)

        if not request.query_params.get('offline') == 'true':
            queryset = self.filter_queryset(
                self.get_queryset()).exclude(datacenter__is_offline=True)
            if request.query_params.get('export') == 'true':
                queryset = self.filter_queryset(self.get_queryset())
                if request.query_params.get('np') == 'true':
                    return export_network_ports(queryset)
                return export_assets(queryset)
            page = self.paginate_queryset(queryset)
            if page is not None:
                serializer = self.get_serializer(page, many=True)
                return self.get_paginated_response(serializer.data)
            serializer = self.get_serializer(queryset, many=True)
            return Response(serializer.data)
        if request.query_params.get('export') == 'true':
            queryset = self.filter_queryset(self.get_queryset())
            if request.query_params.get('np') == 'true':
                return export_network_ports(queryset)
            return export_assets(queryset)

        return super().list(self, request, *args, **kwargs)
Пример #21
0
class Answer(models.Model):
    id = CharField(max_length=20, primary_key=True, db_column='id')
    question_id = ForeignKey(Question,
                             db_column='QUESTID',
                             default='',
                             on_delete=models.CASCADE)
    assignment_id = ForeignKey(Assignment,
                               db_column='ASNID',
                               default='',
                               on_delete=models.CASCADE)
    answer = IntegerField(db_column='ANSWER',
                          default=1,
                          validators=[validate_choice])

    class Meta:
        db_table = "ANSWER"
        unique_together = (("question_id", "assignment_id"), )
Пример #22
0
class Extract(TimezoneMixin, Transform):
    lookup_name = None
    output_field = IntegerField()

    def __init__(self, expression, lookup_name=None, tzinfo=None, **extra):
        if self.lookup_name is None:
            self.lookup_name = lookup_name
        if self.lookup_name is None:
            raise ValueError('lookup_name must be provided')
        self.tzinfo = tzinfo
        super().__init__(expression, **extra)

    def as_sql(self, compiler, connection):
        sql, params = compiler.compile(self.lhs)
        lhs_output_field = self.lhs.output_field
        if isinstance(lhs_output_field, DateTimeField):
            tzname = self.get_tzname()
            sql = connection.ops.datetime_extract_sql(self.lookup_name, sql, tzname)
        elif isinstance(lhs_output_field, DateField):
            sql = connection.ops.date_extract_sql(self.lookup_name, sql)
        elif isinstance(lhs_output_field, TimeField):
            sql = connection.ops.time_extract_sql(self.lookup_name, sql)
        elif isinstance(lhs_output_field, DurationField):
            if not connection.features.has_native_duration_field:
                raise ValueError('Extract requires native DurationField database support.')
            sql = connection.ops.time_extract_sql(self.lookup_name, sql)
        else:
            # resolve_expression has already validated the output_field so this
            # assert should never be hit.
            assert False, "Tried to Extract from an invalid type."
        return sql, params

    def resolve_expression(self, query=None, allow_joins=True, reuse=None, summarize=False, for_save=False):
        copy = super().resolve_expression(query, allow_joins, reuse, summarize, for_save)
        field = copy.lhs.output_field
        if not isinstance(field, (DateField, DateTimeField, TimeField, DurationField)):
            raise ValueError(
                'Extract input expression must be DateField, DateTimeField, '
                'TimeField, or DurationField.'
            )
        # Passing dates to functions expecting datetimes is most likely a mistake.
        if type(field) == DateField and copy.lookup_name in ('hour', 'minute', 'second'):
            raise ValueError(
                "Cannot extract time component '%s' from DateField '%s'. " % (copy.lookup_name, field.name)
            )
        return copy
Пример #23
0
class Img(Model):
    id = BigAutoField(verbose_name='ID', primary_key=True)
    md5 = CharField(verbose_name='MD5', max_length=32, null=False)
    src = CharField(
        verbose_name='路径',
        max_length=255,
        null=False,
    )
    is_deleted = BooleanField(verbose_name='已经删除',
                              null=False,
                              default=False,
                              blank=True)
    size = IntegerField(verbose_name='体积(Byte)', null=False)
    updater = BigIntegerField(null=False, verbose_name='作者账号')  # user外键
    create_time = DateTimeField(null=False,
                                verbose_name='创建时间',
                                auto_now_add=True)
Пример #24
0
class Location(models.Model):                                                   # old: nb2_location
    source              = ForeignKey(Source)                                    # old: id_source integer
    version             = IntegerField(default=1)
    ensemble            = ForeignKey(Ensemble)                                  # old: id_ensemble integer
    section             = ForeignKey(Section, null=True)    
    x                   = IntegerField()
    y                   = IntegerField()
    w                   = IntegerField()
    h                   = IntegerField()
    page                = IntegerField()
    def __unicode__(self):
        return "%s %s: on source %s - page %s " % (self.__class__.__name__,self.id,  self.source_id, self.page)
Пример #25
0
class About(models.Model):

    title = CharField(max_length=1000, blank=True, default="")
    subtitle = CharField(max_length=1000, blank=True, default="")
    info = TextField(blank=True, default="")

    index = IntegerField(default=0)

    externURL = URLField(verbose_name="Extern link", default="", blank=True)

    image = ImageField(upload_to="about_images", null=True)

    largeImage = ImageSpecField(source="image",
                                processors=[ResizeToFill(1920, 1080)],
                                format='JPEG',
                                options={
                                    'quality': 75,
                                    'progressive': True
                                })

    mediumImage = ImageSpecField(source="image",
                                 processors=[ResizeToFill(1280, 720)],
                                 format='JPEG',
                                 options={
                                     'quality': 75,
                                     'progressive': True
                                 })

    smallImage = ImageSpecField(source="image",
                                processors=[ResizeToFill(640, 360)],
                                format='JPEG',
                                options={
                                    'quality': 75,
                                    'progressive': True
                                })

    def getShortDescription(self):
        return str(self.title + " - " + self.subtitle[:50] + "...")

    def toHTML(self):
        return self.info.replace("<p", "<p class=\"rl-document__paragraph\"")\
            .replace("<span", "<span class=\"rl-document__title\"")

    def __unicode__(self):
        return self.title + " - " + self.subtitle
Пример #26
0
    def test_convert_values_to_handle_null_value(self):
        from django.db.backends.sqlite3.base import DatabaseOperations

        database_operations = DatabaseOperations(connection)
        self.assertEqual(
            None,
            database_operations.convert_values(None,
                                               AutoField(primary_key=True)))
        self.assertEqual(None,
                         database_operations.convert_values(None, DateField()))
        self.assertEqual(
            None, database_operations.convert_values(None, DateTimeField()))
        self.assertEqual(
            None, database_operations.convert_values(None, DecimalField()))
        self.assertEqual(
            None, database_operations.convert_values(None, IntegerField()))
        self.assertEqual(None,
                         database_operations.convert_values(None, TimeField()))
Пример #27
0
class Profile(QuicksellModel):
	"""User profile info."""

	user = OneToOneField(
		User, related_name='_profile', primary_key=True,
		editable=False, on_delete=CASCADE
	)
	uuid = UUIDField(default=uuid.uuid4, unique=True, editable=False)
	date_created = DateField(default=date.today, editable=False)
	full_name = CharField(max_length=100, blank=True)
	about = TextField(blank=True)
	online = BooleanField(default=True)
	rating = IntegerField(default=0)
	avatar = ImageField(null=True, blank=True, upload_to='images/avatars')
	location = ForeignKey(**location_fk_kwargs)

	def __str__(self):
		return str(self.user) + "'s profile."
Пример #28
0
class Enlace(models.Model):
    titulo = models.CharField(max_length=140)
    enlace = models.URLField()
    votos = IntegerField(default=0)
    categorias = models.ForeignKey(Categoria)
    usuario = models.ForeignKey(User)
    timestamp = models.DateTimeField(auto_now_add=True)

    def __unicode__(self):
        return "%s - %s" % (self.titulo, self.enlace)

    def mis_votos_en_imagen_rosada(self):
        return 'http://placehold.it/200x100/E8117F/ffffff/&text=%d+votos' % self.votos

    def es_popular(self):
        return self.votos > 1

    es_popular.boolean = True
Пример #29
0
class Ensemble(models.Model):  # old: ensemble
    SECTION_ASSGT_NULL = 1
    SECTION_ASSGT_RAND = 2
    SECTION_ASSGT_TYPES = ((SECTION_ASSGT_NULL, "NULL"), (SECTION_ASSGT_RAND,
                                                          "RANDOM"))
    name = CharField(max_length=255)  # old: name text
    description = CharField(max_length=255, default="No description available")
    allow_staffonly = BooleanField(
        default=True,
        verbose_name="Allow users to write 'staff-only' comments")
    allow_anonymous = BooleanField(
        default=True, verbose_name="Allow users to write anonymous comments")
    allow_tag_private = BooleanField(
        default=True,
        verbose_name="Allow users to make comments private to tagged users only"
    )
    allow_guest = BooleanField(
        default=False,
        verbose_name="Allow guests (i.e. non-members) to access the site")
    invitekey = CharField(max_length=63, blank=True, null=True)  # new
    use_invitekey = BooleanField(
        default=True,
        verbose_name=
        "Allow users who have the 'subscribe link' to register by themselves")
    allow_download = BooleanField(
        default=True, verbose_name="Allow users to download the PDFs")
    allow_ondemand = BooleanField(
        default=False,
        verbose_name=
        "Allow users to add any PDF accessible on the internet by pointing to its URL"
    )
    default_pause = BooleanField(
        default=False, verbose_name="Pause on staff Video comments by default")
    section_assignment = IntegerField(choices=SECTION_ASSGT_TYPES,
                                      default=SECTION_ASSGT_NULL,
                                      null=True)
    metadata = TextField(null=True,
                         blank=True)  #data in json format to help processing.

    def __unicode__(self):
        return "%s %s: %s" % (self.__class__.__name__, self.id, self.name)

    class Meta:
        ordering = ["id"]
Пример #30
0
class Reminder(models.Model):
    project_id = CharField(null=False, blank=False, max_length=264)
    day = IntegerField(null=True, blank=True)
    message = CharField(max_length=160)
    reminder_mode = CharField(null=False, blank=False, max_length=20, default=ReminderMode.BEFORE_DEADLINE)
    organization = ForeignKey(Organization)

    def to_dict(self):
        return {'day': self.day, 'message': self.message, 'reminder_mode': self.reminder_mode, 'id': self.id}

    def void(self, void=True):
        self.voided = void
        self.save()

    def should_be_send_on(self, deadline, on_date):
        assert isinstance(on_date, date)
        deadline_date = self._get_applicapable_deadline_date(deadline, on_date)
        return on_date == deadline_date + timedelta(days=self._delta())

    def get_sender_list(self, project, on_date, dbm):
        if not project.reminder_and_deadline['should_send_reminder_to_all_ds']:
            deadline_date = self._get_applicapable_deadline_date(project.deadline(), on_date)
            return project.get_data_senders_without_submissions_for(deadline_date, dbm)
        return project.get_data_senders(dbm)

    def _delta(self):
        if self.reminder_mode == ReminderMode.ON_DEADLINE:
            return 0
        if self.reminder_mode == ReminderMode.BEFORE_DEADLINE:
            return -self.day
        if self.reminder_mode == ReminderMode.AFTER_DEADLINE:
            return self.day

    def _get_applicapable_deadline_date(self, deadline, on_date):
        if self.reminder_mode == ReminderMode.BEFORE_DEADLINE:
            return deadline.next_deadline(on_date)
        else:
            return deadline.current_deadline(on_date)

    def log(self, dbm, project_id, date, to_number, sent_status='sent', number_of_sms=0):
        log = ReminderLog(dbm=dbm, reminder=self, project_id=project_id, date=date, sent_status=sent_status,
                          number_of_sms=number_of_sms, to_number=to_number)
        log.save()
        return log
Пример #31
0
def get_next_id(queryset: QuerySet, id_field: Field, max_len: int):
    """
    Fetch the next sequential ID value by incrementing the maximum ID value in a
    queryset.

    :param queryset QuerySet: The queryset to get the next sequential ID from
    :param id_field Field: The ID field to consider
    :param max_len int: The maximum length of an ID value
    """
    if not queryset:
        return "1".zfill(max_len)

    return (queryset.annotate(next_id=Func(
        Cast(F(id_field.name), IntegerField()) + 1,
        Value(f"FM{'0' * max_len}"),
        function="TO_CHAR",
        output_field=id_field,
    ), ).exclude(next_id__in=queryset.values(id_field.name), ).order_by(
        id_field.name).first().next_id)
Пример #32
0
def xml_to_field(field_type, **kwargs):
    if field_type == 'int':
        #TODO fix max_length
        field = IntegerField(max_length=MAX_LENGHT_INT, **kwargs)
    if field_type == 'char':
        #TODO fix max_length
        field = CharField(max_length=MAX_LENGHT_CHAR, **kwargs)
    if field_type == 'text':
        field = TextField(**kwargs)
    if field_type == 'boolean':
        field = BooleanField(**kwargs)
    if field_type == 'date':
        field = DateField(**kwargs)
    if field_type == 'datetime':
        field = DateTimeField(**kwargs)
    if field_type == 'float':
        field = FloatField(**kwargs)

    return field
Пример #33
0
class RBACSync(Model):
    """A row in this table denotes a change that requires information RBAC
    micro-service to be updated.

    Typically this will be populated by a trigger within the database. A
    listeners in regiond will be notified and consult the un-synced records
    in this table. This way we can consistently publish RBAC information to the
    RBAC service in an HA environment.
    """

    class Meta(DefaultMeta):
        """Default meta."""

    objects = RBACSyncManager()

    action = CharField(
        editable=False, max_length=6, null=False, blank=True,
        choices=RBAC_ACTION_CHOICES, default=RBAC_ACTION.FULL,
        help_text="Action that should occur on the RBAC service.")

    # An '' string is used when action is 'full'.
    resource_type = CharField(
        editable=False, max_length=255, null=False, blank=True,
        help_text="Resource type that as been added/updated/removed.")

    # A `None` is used when action is 'full'.
    resource_id = IntegerField(
        editable=False, null=True, blank=True,
        help_text="Resource ID that has been added/updated/removed.")

    # A '' string is used when action is 'full'.
    resource_name = CharField(
        editable=False, max_length=255, null=False, blank=True,
        help_text="Resource name that has been added/updated/removed.")

    # This field is informational.
    created = DateTimeField(
        editable=False, null=False, auto_now=False, auto_now_add=True)

    # This field is informational.
    source = CharField(
        editable=False, max_length=255, null=False, blank=True,
        help_text="A brief explanation what changed.")
Пример #34
0
class Comment(models.Model):                                                    # old: nb2_comment
    TYPES               = ((1, "Private"), (2, "Staff"), (3, "Class"))     
    location            = ForeignKey(Location)                                  # old: id_location integer
    parent              = ForeignKey('self', null=True)                        # old: id_parent integer
    author              = ForeignKey(User)                                      # old: id_author integer,
    ctime               = DateTimeField(default=datetime.now)                   # old: ctime timestamp 
    body                = TextField(blank=True, null=True)
    type                = IntegerField(choices=TYPES)
    signed              = BooleanField(default=True)                            # old: signed integer DEFAULT 0,
    deleted             = BooleanField(default=False)                           # old: vis_status integer DEFAULT 0
    moderated           = BooleanField(default=False)
    def __unicode__(self):
        return "%s %s: %s " % (self.__class__.__name__,self.id,  self.body[:50])
   
    @property
    def created(self):
        if (timezone.is_naive(self.ctime)):
            return str(calendar.timegm(pytz.utc.localize(self.ctime).timetuple()))
        else:
            return str(calendar.timegm(self.ctime.astimezone(pytz.utc).timetuple()))
Пример #35
0
class DomainName(Model):
    """Domain Names."""

    ip_address = GenericIPAddressField(protocol='IPv4', verbose_name='IP Address')
    domain_name = CharField(max_length=100, verbose_name='Domain Name')
    sr_number = IntegerField(null=True, verbose_name='SR Number', db_column='ticket_id')

    def __str__(self):
        return 'DNS Record: ' + str(self.ip_address)

    @property
    def domain_name_request(self):
        if self.sr_number:
            # TODO: Update to use updated srsconnector
            # return DomainNameRequest.objects.get(ticket_id=self.sr_number)
            return None
        return None

    class Meta:
        verbose_name = 'Domain Name'
Пример #36
0
def filter_products_by_stock_availability(qs, stock_availability,
                                          channel_slug):
    allocations = (Allocation.objects.values("stock_id").filter(
        quantity_allocated__gt=0,
        stock_id=OuterRef("pk")).values_list(Sum("quantity_allocated")))
    allocated_subquery = Subquery(queryset=allocations,
                                  output_field=IntegerField())

    stocks = (Stock.objects.for_channel(channel_slug).filter(
        quantity__gt=Coalesce(allocated_subquery, 0)).values(
            "product_variant_id"))
    variants = ProductVariant.objects.filter(
        Exists(stocks.filter(
            product_variant_id=OuterRef("pk")))).values("product_id")

    if stock_availability == StockAvailability.IN_STOCK:
        qs = qs.filter(Exists(variants.filter(product_id=OuterRef("pk"))))
    if stock_availability == StockAvailability.OUT_OF_STOCK:
        qs = qs.filter(~Exists(variants.filter(product_id=OuterRef("pk"))))
    return qs
Пример #37
0
    def __new__(cls, name, bases, attrs):
        super_new = super(ModelBase, cls).__new__
        parents = [b for b in bases if isinstance(b, ModelBase)]
        if not parents:
            # If this isn't a subclass of Model, don't do anything special.
            return super_new(cls, name, bases, attrs)

        # Create the class.
        module = attrs.pop('__module__')
        new_class = super_new(cls, name, bases, {'__module__': module})
        attr_meta = attrs.pop('Meta', None)
        abstract = getattr(attr_meta, 'abstract', False)
        mat_view = getattr(attr_meta, 'materialized_view', False)
        leaf = False
        if mat_view and abstract:
            raise TypeError("A model cannot be 'abstract' and 'materialized_view' at the same time.")
        if not attr_meta:
            meta = getattr(new_class, 'Meta', None)
        else:
            meta = attr_meta
        base_meta = getattr(new_class, '_meta', None)

        if getattr(meta, 'app_label', None) is None:
            # Figure out the app_label by looking one level up.
            # For 'django.contrib.sites.models', this would be 'sites'.
            model_module = sys.modules[new_class.__module__]
            kwargs = {"app_label": model_module.__name__.split('.')[-2]}
        else:
            kwargs = {}

        new_class.add_to_class('_meta', Options(meta, **kwargs))

        if not abstract:
            new_class.add_to_class('DoesNotExist', subclass_exception('DoesNotExist',
                    tuple(x.DoesNotExist
                            for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
                                    or (ObjectDoesNotExist,), module))
            new_class.add_to_class('MultipleObjectsReturned', subclass_exception('MultipleObjectsReturned',
                    tuple(x.MultipleObjectsReturned
                            for x in parents if hasattr(x, '_meta') and not x._meta.abstract)
                                    or (MultipleObjectsReturned,), module))
            if base_meta:
                if not base_meta.abstract:
                    # Non-abstract child classes inherit some attributes from their
                    # non-abstract parent (unless an ABC comes before it in the
                    # method resolution order).
                    if not hasattr(meta, 'ordering'):
                        new_class._meta.ordering = base_meta.ordering
                    if not hasattr(meta, 'get_latest_by'):
                        new_class._meta.get_latest_by = base_meta.get_latest_by
                if base_meta.materialized_view or base_meta.leaf or (
                        base_meta.db_view and base_meta.intermediate):
                    if mat_view :
                        raise TypeError('A materialized view cannot inherit from another')
                    new_class._meta.leaf = True
                    leaf = True
                    # register the materialized_view base
                    new_class._meta.mat_view_base = base_meta.concrete_model
                if base_meta.leaf:
                    mat_view_base = base_meta.mat_view_base
                    new_class._meta.mat_view_base = mat_view_base
                    # turn the base into an intermediate view
                    base_meta.db_view = True
                    if base_meta._auto_db_table:
                        base_meta.db_table = '%s_%s' % (base_meta.db_table, 'view')
                    base_meta.intermediate = True
                    base_meta.leaf = False
                    base_meta.abstract = True
                    if base_meta.concrete:
                        base_model = mat_view_base._meta.leaf_ids[base_meta.leaf_id]
                        # concrete views know about themselves...
                        base_meta.leaves = {base_model: base_meta.leaf_id}
                        base_meta.leaf_ids = {base_meta.leaf_id: base_model}
                    else:
                        base_meta.leaves = {}
                        base_meta.leaf_ids = {}
                    ctype_field = IntegerField(db_index=True)
                    ctype_field.acquired = True
                    base_meta.concrete_model.add_to_class('pgd_child_type', ctype_field)
                if base_meta.intermediate:
                    new_class._meta.mat_view_base = base_meta.mat_view_base

        is_proxy = new_class._meta.proxy

        if getattr(new_class, '_default_manager', None):
            if not is_proxy:
                # Multi-table inheritance doesn't inherit default manager from
                # parents.
                new_class._default_manager = None
                new_class._base_manager = None
            else:
                # Proxy classes do inherit parent's default manager, if none is
                # set explicitly.
                new_class._default_manager = new_class._default_manager._copy_to_model(new_class)
                new_class._base_manager = new_class._base_manager._copy_to_model(new_class)

        # Bail out early if we have already created this class.
        m = get_model(new_class._meta.app_label, name,
                      seed_cache=False, only_installed=False)
        if m is not None:
            return m

        # Add all attributes to the class.
        for obj_name, obj in attrs.items():
            new_class.add_to_class(obj_name, obj)

        if mat_view:
            # add a pgd_child_type field to identify the model of each view row
            if 'pgd_child_type' in attrs:
                # pfff... bloody clever users... It's 2:06 AM now, I won't fix it
                pass
            ctype_field = IntegerField(db_index=True)
            ctype_field.acquired = True
            new_class.add_to_class('pgd_child_type', ctype_field)
            # add caches of the children
            new_class._meta.leaf_ids = {}
            new_class._meta.leaves = {}

        # All the fields of any type declared on this model
        new_fields = new_class._meta.local_fields + \
                     new_class._meta.local_many_to_many + \
                     new_class._meta.virtual_fields
        field_names = set([f.name for f in new_fields])

        # Basic setup for proxy models.
        if is_proxy:
            base = None
            for parent in [cls for cls in parents if hasattr(cls, '_meta')]:
                if parent._meta.abstract:
                    if parent._meta.fields:
                        raise TypeError("Abstract base class containing model fields not permitted for proxy model '%s'." % name)
                    else:
                        continue
                if base is not None:
                    raise TypeError("Proxy model '%s' has more than one non-abstract model base class." % name)
                else:
                    base = parent
            if base is None:
                    raise TypeError("Proxy model '%s' has no non-abstract model base class." % name)
            if (new_class._meta.local_fields or
                    new_class._meta.local_many_to_many):
                raise FieldError("Proxy model '%s' contains model fields." % name)
            new_class._meta.setup_proxy(base)
            new_class._meta.concrete_model = base._meta.concrete_model
        else:
            new_class._meta.concrete_model = new_class

        # Do the appropriate setup for any model parents.
        o2o_map = dict([(f.rel.to, f) for f in new_class._meta.local_fields
                if isinstance(f, OneToOneField)])

        if leaf:
            # Add all child fields on its materalized and intermediate views and mark them
            # acquired
            child_fields = new_class._meta.local_fields + new_class._meta.local_many_to_many
            for base in [c for c in new_class.__mro__ if hasattr(c, '_meta') and (
                         c._meta.intermediate or c._meta.materialized_view) ]:
                inserted = False
                for field in child_fields:
                    if not getattr(field, 'acquired', False):
                        inserted = True
                        parent_field = copy.deepcopy(field)
                        parent_field.acquired = True
                        if base._meta.intermediate:
                            parent_field.child_field = True
                        base.add_to_class(field.name, parent_field)
                if inserted:
                    # move pgd_child_type at last position on the parent
                    ctype_field = base._meta.get_field_by_name('pgd_child_type')[0]
                    base._meta.local_fields.remove(ctype_field)
                    base._meta.local_fields.append(ctype_field)
                    # invalidate field caches
                    if hasattr(base._meta, '_field_cache'):
                        del base._meta._field_cache
                        del base._meta._field_name_cache
                    if hasattr(base._meta, '_name_map'):
                        del base._meta._name_map


        for base in parents:
            original_base = base
            if not hasattr(base, '_meta'):
                # Things without _meta aren't functional models, so they're
                # uninteresting parents.
                continue

            parent_fields = base._meta.local_fields + base._meta.local_many_to_many
            # Check for clashes between locally declared fields and those
            # on the base classes (we cannot handle shadowed fields at the
            # moment, except in mat. view inheritance).
            if not leaf:
                for field in parent_fields:
                    if field.name in field_names:
                        raise FieldError('Local field %r in class %r clashes '
                                        'with field of similar name from '
                                        'base class %r' %
                                            (field.name, name, base.__name__))

            if not base._meta.abstract:
                # Concrete classes...
                base = base._meta.concrete_model
                if base in o2o_map:
                    field = o2o_map[base]
                elif not is_proxy:
                    attr_name = '%s_ptr' % base._meta.module_name
                    field = OneToOneField(base, name=attr_name,
                            auto_created=True, parent_link=True)
                    new_class.add_to_class(attr_name, field)
                else:
                    field = None
                new_class._meta.parents[base] = field
            else:
                # .. and abstract ones.
                for field in parent_fields:
                    if not getattr(field, 'acquired', False):
                        new_class.add_to_class(field.name, copy.deepcopy(field))

                # Pass any non-abstract parent classes onto child.
                new_class._meta.parents.update(base._meta.parents)

            # Inherit managers from the abstract base classes.
            new_class.copy_managers(base._meta.abstract_managers)

            # Proxy models inherit the non-abstract managers from their base,
            # unless they have redefined any of them.
            if is_proxy:
                new_class.copy_managers(original_base._meta.concrete_managers)

            # Inherit virtual fields (like GenericForeignKey) from the parent
            # class
            for field in base._meta.virtual_fields:
                if base._meta.abstract and field.name in field_names:
                    raise FieldError('Local field %r in class %r clashes '\
                                     'with field of similar name from '\
                                     'abstract base class %r' % \
                                        (field.name, name, base.__name__))
                new_class.add_to_class(field.name, copy.deepcopy(field))

        if abstract:
            # Abstract base models can't be instantiated and don't appear in
            # the list of models for an app. We do the final setup for them a
            # little differently from normal models.
            attr_meta.abstract = False
            new_class.Meta = attr_meta
            return new_class

        new_class._prepare()

        if mat_view:
            # now we know the table name, we can set the sequence name
            tname = new_class._meta.db_table
            new_class._meta.pk.sequence = tname + '_id_seq'

        register_models(new_class._meta.app_label, new_class)

        # Because of the way imports happen (recursively), we may or may not be
        # the first time this model tries to register with the framework. There
        # should only be one class for each model, so we always return the
        # registered version.
        new_class = get_model(new_class._meta.app_label, name,
                         seed_cache=False, only_installed=False)

        if mat_view:
            # turn it into an abstract model, so children won't try to create
            # joins
            new_class._meta.abstract = True

            # Now, we override new_class.__new__ on non-concrete intermediate and
            # materialized views to choose the class of the new object returned
            # when the user (or a QuerySet) tries to instantiate it.
            # It is chosen amongst leaves
            def new_matview_new(klass,*args,**kwargs):
                # leaf classes are not patched
                if not (klass._meta.materialized_view or
                            klass._meta.intermediate):
                    return Model.__new__(klass, *args, **kwargs)
                if len(kwargs) and klass._meta.concrete:
                    # probably a user instanciation
                    return Model.__new__(klass, *args, **kwargs)
                parent_fnames = [f.column for f in klass._meta.fields]
                idx = parent_fnames.index('pgd_child_type')
                if len(args) <= idx and 'pgd_child_type' not in kwargs:
                    # I wouldn't do that if I were you. Seems like a smart (or
                    # crazy) user is trying to do something clever (or foolish)
                    # and I don't want to refrain one's creativity. This model
                    # cannot be saved anyway.
                    return Model.__new__(klass,*args,**kwargs)
                # Here is the magic: if you try to instanciate a m10d view
                # you get its subbclass corresponding to its pgd_child_type
                # That how mixed queryset work.
                # pgd_child_type should be the last field. Just to be sure
                leaf = klass._meta.leaf_ids.get(args[idx],None)
                # TODO PG: add an exception here if None

                # hook the __init__ arguments
                leaf_fnames = [f.name for f in leaf._meta.fields]
                leaf_args = [None]*len(leaf_fnames)
                for i in range(len(parent_fnames)):
                    fname = parent_fnames[i]
                    if fname in leaf_fnames:
                        leaf_args[leaf_fnames.index(fname)] = args[i]
                inst = Model.__new__(leaf,*leaf_args,**kwargs)
                # The modified arg list is stored on the instance itself.
                # A hook in __init__ will send it to the original __init__
                inst.__hooked_args__ = leaf_args
                return inst

            new_matview_new.__patched__ = True
            if not getattr(new_class.__new__, '__patched__', False):
                new_class.__new__ = staticmethod(new_matview_new)

        if leaf or (new_class._meta.intermediate and new_class._meta.concrete):
            # register the leaf on its materialized_view base model
            mvbase = new_class._meta.mat_view_base
            mvbase._meta.register_leaf(new_class)

        return new_class
Пример #38
0

class IntegerFieldFloatRounding(object):
    """
    Allow floats to work as query values for IntegerField. Without this, the
    decimal portion of the float would always be discarded.
    """
    def get_prep_lookup(self):
        if isinstance(self.rhs, float):
            self.rhs = math.ceil(self.rhs)
        return super(IntegerFieldFloatRounding, self).get_prep_lookup()


class IntegerGreaterThanOrEqual(IntegerFieldFloatRounding, GreaterThanOrEqual):
    pass
IntegerField.register_lookup(IntegerGreaterThanOrEqual)


class IntegerLessThan(IntegerFieldFloatRounding, LessThan):
    pass
IntegerField.register_lookup(IntegerLessThan)


class In(FieldGetDbPrepValueIterableMixin, BuiltinLookup):
    lookup_name = 'in'

    def process_rhs(self, compiler, connection):
        db_rhs = getattr(self.rhs, '_db', None)
        if db_rhs is not None and db_rhs != connection.alias:
            raise ValueError(
                "Subqueries aren't allowed across different databases. Force "
Пример #39
0
 def __init__(self, with_respect_to=[], **kwargs):
     self.with_respect_to = with_respect_to
     kwargs['null'] = False
     kwargs['editable'] = False
     IntegerField.__init__(self, **kwargs )