Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ciur committed Oct 9, 2024
1 parent c0728a9 commit f016859
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 32 deletions.
23 changes: 19 additions & 4 deletions papermerge/core/db/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
DocumentVersion,
Page,
)
from papermerge.core.exceptions import InvalidDateFormat

from .common import get_ancestors

Expand All @@ -30,6 +31,22 @@
}


def str2date(value: str) -> datetime.date:
"""Convert incoming user string to datetime.date"""
# 10 = 4 Y chars + 1 "-" char + 2 M chars + 1 "-" char + 2 D chars
DATE_LEN = 10
stripped_value = value.strip()
if len(stripped_value) < DATE_LEN:
raise InvalidDateFormat(
f"{stripped_value} expected to have at least {DATE_LEN} characters"
)

return datetime.strptime(
value[:DATE_LEN],
INCOMING_DATE_FORMAT,
).date()


def get_doc(
session: Session,
id: UUID,
Expand Down Expand Up @@ -135,9 +152,7 @@ def update_document_custom_field_values(
)
if attr_name:
if attr_name == "date":
_dic[f"value_{attr_name}"] = datetime.strptime(
incoming_cf.value, INCOMING_DATE_FORMAT
)
_dic[f"value_{attr_name}"] = str2date(incoming_cf.value)
else:
_dic[f"value_{attr_name}"] = incoming_cf.value

Expand Down Expand Up @@ -216,7 +231,7 @@ def add_document_custom_field_values(
value = ""
if attr_name:
if attr_name == "date":
value = datetime.strptime(incoming_cf.value, INCOMING_DATE_FORMAT)
value = str2date(incoming_cf.value)
else:
value = incoming_cf.value
_dic[f"value_{attr_name}"] = value
Expand Down
7 changes: 7 additions & 0 deletions papermerge/core/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Global Papermerge exception and warning classes.
"""

from rest_framework.exceptions import APIException


Expand All @@ -13,9 +14,15 @@ class SuperuserDoesNotExist(Exception):
Raised when superuser was not found.
Papermerge must have at least one superuser.
"""

pass


class FileTypeNotSupported(Exception):
"""File type not supported"""

pass


class InvalidDateFormat(Exception):
pass
66 changes: 66 additions & 0 deletions tests/core/models/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import pytest
from django.db import transaction
from django.db.utils import IntegrityError
from django.utils.datetime_safe import datetime
from sqlalchemy.orm import Session

from papermerge.core import db, schemas
from papermerge.core.db.doc import str2date
from papermerge.core.models import Document, User
from papermerge.core.storage import abs_path
from papermerge.test import TestCase
Expand Down Expand Up @@ -300,6 +302,65 @@ def test_document_add_custom_field_value_of_type_date(
assert len(total_cfv_after) == 1


@pytest.mark.django_db(transaction=True)
def test_document_update_custom_field_of_type_date1(
db_session: Session,
document: Document,
document_type_with_one_date_cf: schemas.DocumentType,
):
dtype = document_type_with_one_date_cf
cf_add = {
"document_type_id": dtype.id,
"custom_fields": [
# date is expected to be in:
# papermerge.core.constants.INCOMING_DATE_FORMAT
{"custom_field_id": dtype.custom_fields[0].id, "value": "2024-10-28"},
],
}
custom_fields_add = schemas.DocumentCustomFieldsAdd(**cf_add)
db.add_document_custom_field_values(
db_session,
id=document.id,
custom_fields_add=custom_fields_add,
user_id=document.user.id,
)

total_cfv_after1: list[schemas.CustomFieldValue] = (
db.get_document_custom_field_values(
db_session, id=document.id, user_id=document.user.id
)
)
assert len(total_cfv_after1) == 1

cf_update = {
"document_type_id": dtype.id,
"custom_fields": [
{
"custom_field_value_id": total_cfv_after1[0].id,
"value": "2024-02-28 00:00:00",
},
],
}
custom_fields_update = schemas.DocumentCustomFieldsUpdate(**cf_update)

db.update_document_custom_field_values(
db_session,
id=document.id,
custom_fields_update=custom_fields_update,
user_id=document.user.id,
)

total_cfv_after2 = db.get_document_custom_field_values(
db_session, id=document.id, user_id=document.user.id
)
# even though we update multiple times - only the value is
# updated - and number of custom fields associated with
# the document is the same i.e. - one
assert len(total_cfv_after2) == 1
# and the value is - last one
assert total_cfv_after2[0].value == "2024-02-28 00:00:00"


@pytest.mark.django_db(transaction=True)
def test_document_update_same_custom_field_value_multiple_times1(
db_session: Session,
Expand Down Expand Up @@ -582,3 +643,8 @@ def test_document_set_document_type_to_none(
)

assert len(total_cfv_after) == 0


def test_str2date():
assert str2date("2024-10-30") == datetime(2024, 10, 30).date()
assert str2date("2024-10-30 00:00:00") == datetime(2024, 10, 30).date()
2 changes: 1 addition & 1 deletion ui2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"react-dom": "^18.3.1",
"react-redux": "^9.1.2",
"react-router": "^6.26.2",
"react-router-dom": "^6.23.1"
"react-router-dom": "^6.26.2"
},
"devDependencies": {
"@types/js-cookie": "^3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ export default function EditGroupModal({
const [updateCustomField, {isLoading: isLoadingGroupUpdate}] =
useEditCustomFieldMutation()
const [name, setName] = useState<string>("")
const [dataType, setDataType] = useState<CustomFieldDataType>("string")
const [dataType, setDataType] = useState<CustomFieldDataType>(
data?.data_type || "string"
)

useEffect(() => {
formReset()
Expand Down
Empty file removed ui2/src/pages/CustomFields.tsx
Empty file.
34 changes: 8 additions & 26 deletions ui2/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -687,13 +687,6 @@ __metadata:
languageName: node
linkType: hard

"@remix-run/router@npm:1.16.1":
version: 1.16.1
resolution: "@remix-run/router@npm:1.16.1"
checksum: 10c0/5f1b0aef4924830eeab9c86dcaa5af8157066e5de65b449e7fdf406532b2384828a46a447c31b0735fd713a06938dd88bfd4e566d9989be70c770457dda16c92
languageName: node
linkType: hard

"@remix-run/router@npm:1.19.2":
version: 1.19.2
resolution: "@remix-run/router@npm:1.19.2"
Expand Down Expand Up @@ -2234,31 +2227,20 @@ __metadata:
languageName: node
linkType: hard

"react-router-dom@npm:^6.23.1":
version: 6.23.1
resolution: "react-router-dom@npm:6.23.1"
"react-router-dom@npm:^6.26.2":
version: 6.26.2
resolution: "react-router-dom@npm:6.26.2"
dependencies:
"@remix-run/router": "npm:1.16.1"
react-router: "npm:6.23.1"
"@remix-run/router": "npm:1.19.2"
react-router: "npm:6.26.2"
peerDependencies:
react: ">=16.8"
react-dom: ">=16.8"
checksum: 10c0/01b954d7d0ff4c53bb2edbc816458f3fad1ce9ee49a4dfdc5c866065c23026c9cce429b46b754cbaebb83b22cfe5f605bbf441acf515e3c377cbdf021b0bec4c
languageName: node
linkType: hard

"react-router@npm:6.23.1":
version: 6.23.1
resolution: "react-router@npm:6.23.1"
dependencies:
"@remix-run/router": "npm:1.16.1"
peerDependencies:
react: ">=16.8"
checksum: 10c0/091949805745136350ab049b2a96281bf38742c9d3651019fb48ea79c5eafbfb0379f1d3e636602dd56b0ef278389e8fd25be983dc2c0ffd1103d06dfa8019f3
checksum: 10c0/7515128a98eef0a6b2bf354ef9dfefad03556a06be00fa9220eda6526aaada8a42f294911083473d7ced6d7128c3088bd193218bbb3d62593f9f4f7053781c23
languageName: node
linkType: hard

"react-router@npm:^6.26.2":
"react-router@npm:6.26.2, react-router@npm:^6.26.2":
version: 6.26.2
resolution: "react-router@npm:6.26.2"
dependencies:
Expand Down Expand Up @@ -2704,7 +2686,7 @@ __metadata:
react-dom: "npm:^18.3.1"
react-redux: "npm:^9.1.2"
react-router: "npm:^6.26.2"
react-router-dom: "npm:^6.23.1"
react-router-dom: "npm:^6.26.2"
sass: "npm:^1.77.8"
typescript: "npm:^5.4.5"
vite: "npm:^5.4.8"
Expand Down

0 comments on commit f016859

Please sign in to comment.