Skip to content

Commit

Permalink
Introduce separate create_user_field and create_timestamp_field for l…
Browse files Browse the repository at this point in the history
…ogging record creations
  • Loading branch information
manisandro committed May 21, 2024
1 parent e2c6466 commit b2d2920
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
8 changes: 8 additions & 0 deletions schemas/qwc-data-service.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,18 @@
"description": "Field suffix where to log the username who last changed an upload field, i.e. <fieldname>__<suffix> for the field <fieldname>. If empty, username is not logged.",
"type": "string"
},
"create_timestamp_field": {
"description": "If specified, the timestamp of the creation of the record will be logged to the <create_timestamp_field> field of the record.",
"type": "string"
},
"edit_timestamp_field": {
"description": "If specified, the timestamp of the last mutation to a record will be logged to the <edit_timestamp_field> field of the record.",
"type": "string"
},
"create_user_field": {
"description": "If specified, the username of the user who created a record with be logged to the <create_user_field> field of the record.",
"type": "string"
},
"edit_user_field": {
"description": "If specified, the username of the last user who performed a mutation to a record with be logged to the <edit_user_field> field of the record.",
"type": "string"
Expand Down
29 changes: 21 additions & 8 deletions src/data_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def create(self, identity, translator, dataset, feature, files={}):
if save_errors:
return self.error_response(translator.tr("error.feature_commit_failed"), save_errors)

self.add_logging_fields(feature, identity)
self.add_create_logging_fields(feature, identity)

# create new feature
try:
Expand Down Expand Up @@ -313,7 +313,7 @@ def update(self, identity, translator, dataset, id, feature, files={}):
upload_user_field = key + "__" + upload_user_field_suffix
feature["properties"][upload_user_field] = get_username(identity)

self.add_logging_fields(feature, identity)
self.add_update_logging_fields(feature, identity)

# update feature
try:
Expand Down Expand Up @@ -683,7 +683,25 @@ def resolve_attachment(self, identity, translator, dataset, slug):
'file': attachment
}

def add_logging_fields(self, feature, identity):
def add_create_logging_fields(self, feature, identity):
"""Adds logging fields to the feature
:param dict feature: Feature object
:param str|obj identity: User identity
"""
create_user_field = self.config.get("create_user_field", None)
create_timestamp_field = self.config.get("create_timestamp_field", None)

if create_user_field:
feature["properties"][create_user_field] = get_username(identity)
if create_user_field in feature.get("defaultedProperties", []):
feature["defaultedProperties"].remove(create_user_field)
if create_timestamp_field:
feature["properties"][create_timestamp_field] = str(datetime.now())
if create_timestamp_field in feature.get("defaultedProperties", []):
feature["defaultedProperties"].remove(create_timestamp_field)

def add_update_logging_fields(self, feature, identity):
"""Adds logging fields to the feature
:param dict feature: Feature object
Expand All @@ -694,13 +712,8 @@ def add_logging_fields(self, feature, identity):

if edit_user_field:
feature["properties"][edit_user_field] = get_username(identity)
if edit_user_field in feature.get("defaultedProperties", []):
feature["defaultedProperties"].remove(edit_user_field)
if edit_timestamp_field:
feature["properties"][edit_timestamp_field] = str(datetime.now())
if edit_timestamp_field in feature.get("defaultedProperties", []):
feature["defaultedProperties"].remove(edit_timestamp_field)


def error_response(self, error, details):
self.logger.error("%s: %s", error, details)
Expand Down

0 comments on commit b2d2920

Please sign in to comment.