From 2ca691d3b8ec2129a6d29f3b9b3203c2288216d1 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 15 May 2022 21:09:42 -0700 Subject: [PATCH] use created_date --- .../saved-view-widget.component.html | 2 +- .../document-detail.component.html | 2 +- .../document-detail.component.ts | 34 ++----------------- src-ui/src/app/data/paperless-document.ts | 4 +++ src-ui/src/app/pipes/custom-date.pipe.ts | 2 -- src-ui/src/app/utils/date.ts | 5 --- .../app/utils/ngb-iso-date-time-adapter.ts | 23 ++++++++----- src/documents/models.py | 4 +++ src/documents/serialisers.py | 15 ++++++++ 9 files changed, 43 insertions(+), 48 deletions(-) delete mode 100644 src-ui/src/app/utils/date.ts diff --git a/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.html b/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.html index 50d064c37..84ee1aabe 100644 --- a/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.html +++ b/src-ui/src/app/components/dashboard/widgets/saved-view-widget/saved-view-widget.component.html @@ -12,7 +12,7 @@ - {{doc.created | customDate}} + {{doc.created_date | customDate}} {{doc.title | documentTitle}} diff --git a/src-ui/src/app/components/document-detail/document-detail.component.html b/src-ui/src/app/components/document-detail/document-detail.component.html index a4203473f..ca4fb4275 100644 --- a/src-ui/src/app/components/document-detail/document-detail.component.html +++ b/src-ui/src/app/components/document-detail/document-detail.component.html @@ -68,7 +68,7 @@ - + { + .subscribe(() => { this.error = null - if (this.ogDate) { - try { - let newDate = new Date(normalizeDateStr(changes['created'])) - newDate.setHours( - this.ogDate.getHours(), - this.ogDate.getMinutes(), - this.ogDate.getSeconds(), - this.ogDate.getMilliseconds() - ) - this.documentForm.patchValue( - { created: newDate.toISOString() }, - { emitEvent: false } - ) - } catch (e) { - // catch this before we try to save and simulate an api error - this.error = { created: e.message } - } - } - Object.assign(this.document, this.documentForm.value) }) @@ -223,25 +203,17 @@ export class DocumentDetailComponent }, }) - this.ogDate = new Date(normalizeDateStr(doc.created.toString())) - // Initialize dirtyCheck this.store = new BehaviorSubject({ title: doc.title, content: doc.content, - created: this.ogDate.toISOString(), + created_date: doc.created_date, correspondent: doc.correspondent, document_type: doc.document_type, archive_serial_number: doc.archive_serial_number, tags: [...doc.tags], }) - // start with ISO8601 string - this.documentForm.patchValue( - { created: this.ogDate.toISOString() }, - { emitEvent: false } - ) - this.isDirty$ = dirtyCheck( this.documentForm, this.store.asObservable() diff --git a/src-ui/src/app/data/paperless-document.ts b/src-ui/src/app/data/paperless-document.ts index 92a5aee45..051fa0aaa 100644 --- a/src-ui/src/app/data/paperless-document.ts +++ b/src-ui/src/app/data/paperless-document.ts @@ -32,8 +32,12 @@ export interface PaperlessDocument extends ObjectWithId { checksum?: string + // UTC created?: Date + // localized date + created_date?: Date + modified?: Date added?: Date diff --git a/src-ui/src/app/pipes/custom-date.pipe.ts b/src-ui/src/app/pipes/custom-date.pipe.ts index bd4833d04..bf5ab1457 100644 --- a/src-ui/src/app/pipes/custom-date.pipe.ts +++ b/src-ui/src/app/pipes/custom-date.pipe.ts @@ -1,7 +1,6 @@ import { DatePipe } from '@angular/common' import { Inject, LOCALE_ID, Pipe, PipeTransform } from '@angular/core' import { SettingsService, SETTINGS_KEYS } from '../services/settings.service' -import { normalizeDateStr } from '../utils/date' const FORMAT_TO_ISO_FORMAT = { longDate: 'y-MM-dd', @@ -34,7 +33,6 @@ export class CustomDatePipe implements PipeTransform { this.settings.get(SETTINGS_KEYS.DATE_LOCALE) || this.defaultLocale let f = format || this.settings.get(SETTINGS_KEYS.DATE_FORMAT) - if (typeof value == 'string') value = normalizeDateStr(value) if (l == 'iso-8601') { return this.datePipe.transform(value, FORMAT_TO_ISO_FORMAT[f], timezone) } else { diff --git a/src-ui/src/app/utils/date.ts b/src-ui/src/app/utils/date.ts deleted file mode 100644 index b62ffc939..000000000 --- a/src-ui/src/app/utils/date.ts +++ /dev/null @@ -1,5 +0,0 @@ -// see https://github.com/dateutil/dateutil/issues/878 , JS Date does not -// seem to accept these strings as valid dates so we must normalize offset -export function normalizeDateStr(dateStr: string): string { - return dateStr.replace(/[\+-](\d\d):\d\d:\d\d/gm, `-$1:00`) -} diff --git a/src-ui/src/app/utils/ngb-iso-date-time-adapter.ts b/src-ui/src/app/utils/ngb-iso-date-time-adapter.ts index b9acc38ec..9d0ea4de6 100644 --- a/src-ui/src/app/utils/ngb-iso-date-time-adapter.ts +++ b/src-ui/src/app/utils/ngb-iso-date-time-adapter.ts @@ -5,11 +5,20 @@ import { NgbDateAdapter, NgbDateStruct } from '@ng-bootstrap/ng-bootstrap' export class ISODateTimeAdapter extends NgbDateAdapter { fromModel(value: string | null): NgbDateStruct | null { if (value) { - let date = new Date(value) - return { - day: date.getDate(), - month: date.getMonth() + 1, - year: date.getFullYear(), + if (value.match(/\d\d\d\d\-\d\d\-\d\d/g)) { + const segs = value.split('-') + return { + year: parseInt(segs[0]), + month: parseInt(segs[1]), + day: parseInt(segs[2]), + } + } else { + let date = new Date(value) + return { + day: date.getDate(), + month: date.getMonth() + 1, + year: date.getFullYear(), + } } } else { return null @@ -17,8 +26,6 @@ export class ISODateTimeAdapter extends NgbDateAdapter { } toModel(date: NgbDateStruct | null): string | null { - return date - ? new Date(date.year, date.month - 1, date.day).toISOString() - : null + return date ? [date.year, date.month, date.day].join('-') : null } } diff --git a/src/documents/models.py b/src/documents/models.py index 206df4e8a..cc4cd0613 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -279,6 +279,10 @@ class Document(models.Model): def thumbnail_file(self): return open(self.thumbnail_path, "rb") + @property + def created_date(self): + return timezone.localdate(self.created) + class Log(models.Model): diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 1bdcbab9e..564c0aae5 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -1,7 +1,10 @@ +import datetime import math import re import magic +from dateutil import tz +from django.conf import settings from django.utils.text import slugify from django.utils.translation import gettext as _ from rest_framework import serializers @@ -206,6 +209,7 @@ class DocumentSerializer(DynamicFieldsModelSerializer): original_file_name = SerializerMethodField() archived_file_name = SerializerMethodField() + created_date = serializers.DateField() def get_original_file_name(self, obj): return obj.get_public_filename() @@ -216,6 +220,16 @@ class DocumentSerializer(DynamicFieldsModelSerializer): else: return None + def update(self, instance, validated_data): + if "created_date" in validated_data and "created" not in validated_data: + new_datetime = datetime.datetime.combine( + validated_data.get("created_date"), + datetime.time(0, 0, 0, 0, tz.gettz(settings.TIME_ZONE)), + ) + instance.created = new_datetime + instance.save() + return instance + class Meta: model = Document depth = 1 @@ -227,6 +241,7 @@ class DocumentSerializer(DynamicFieldsModelSerializer): "content", "tags", "created", + "created_date", "modified", "added", "archive_serial_number",