diff --git a/bd_api/apps/api/v1/admin.py b/bd_api/apps/api/v1/admin.py index e69ca2c6..07d81471 100644 --- a/bd_api/apps/api/v1/admin.py +++ b/bd_api/apps/api/v1/admin.py @@ -764,11 +764,23 @@ def queryset(self, request, queryset): class DateTimeRangeAdmin(admin.ModelAdmin): - readonly_fields = ["id"] list_display = ["__str__", "coverage"] + readonly_fields = ["id"] autocomplete_fields = [ "coverage", ] + exclude = [ + "start_quarter", + "start_semester", + "start_hour", + "start_minute", + "start_second", + "end_quarter", + "end_semester", + "end_hour", + "end_minute", + "end_second", + ] class CoverageAdmin(admin.ModelAdmin): diff --git a/bd_api/apps/api/v1/models.py b/bd_api/apps/api/v1/models.py index a6afeae9..4f867b9d 100644 --- a/bd_api/apps/api/v1/models.py +++ b/bd_api/apps/api/v1/models.py @@ -1677,23 +1677,7 @@ class DateTimeRange(BaseModel): graphql_nested_filter_fields_whitelist = ["id"] def __str__(self): - start_year = self.start_year or "" - start_month = f"-{self.start_month}" if self.start_month else "" - start_day = f"-{self.start_day}" if self.start_day else "" - start_hour = f" {self.start_hour}" if self.start_hour else "" - start_minute = f":{self.start_minute}" if self.start_minute else "" - start_second = f":{self.start_second}" if self.start_second else "" - end_year = self.end_year or "" - end_month = f"-{self.end_month}" if self.end_month else "" - end_day = f"-{self.end_day}" if self.end_day else "" - end_hour = f" {self.end_hour}" if self.end_hour else "" - end_minute = f":{self.end_minute}" if self.end_minute else "" - end_second = f":{self.end_second}" if self.end_second else "" - interval = f"({self.interval})" if self.interval else "" - return ( - f"{start_year}{start_month}{start_day}{start_hour}{start_minute}{start_second}" - f"{interval}{end_year}{end_month}{end_day}{end_hour}{end_minute}{end_second}" - ) + return f"{self.since_str}({self.interval_str}){self.until_str}" class Meta: """Meta definition for DateTimeRange.""" @@ -1715,6 +1699,16 @@ def since(self): self.start_second or 0, ) + @property + def since_str(self): + if self.start_year and self.start_month and self.start_day: + return self.since.strftime("%Y-%m-%d") + if self.start_year and self.start_month: + return self.since.strftime("%Y-%m") + if self.start_year: + return self.since.strftime("%Y") + return "" + @property def until(self): if self.end_year: @@ -1727,6 +1721,22 @@ def until(self): self.end_second or 0, ) + @property + def until_str(self): + if self.end_year and self.end_month and self.end_day: + return self.until.strftime("%Y-%m-%d") + if self.end_year and self.end_month: + return self.until.strftime("%Y-%m") + if self.end_year: + return self.until.strftime("%Y") + return "" + + @property + def interval_str(self): + if self.interval: + return str(self.interval) + return "0" + def get_similarity_of_datetime(self, other: "DateTimeRange"): if not self.since: return 0