Skip to content

Commit

Permalink
feat: update disable view count
Browse files Browse the repository at this point in the history
  • Loading branch information
florentcadot committed Jan 23, 2024
1 parent cfbe76f commit 7804a06
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
10 changes: 7 additions & 3 deletions c2corg_api/models/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from sqlalchemy.sql.schema import UniqueConstraint

UpdateType = enum.Enum(
'UpdateType', 'FIGURES LANG GEOM')
'UpdateType', 'FIGURES LANG GEOM DISABLE_VIEW_COUNT')

DOCUMENT_TYPE = document_types.DOCUMENT_TYPE

Expand Down Expand Up @@ -147,7 +147,8 @@ def get_versions(self):
'locales': {
locale.lang: locale.version for locale in self.locales
},
'geometry': self.geometry.version if self.geometry else None
'geometry': self.geometry.version if self.geometry else None,
'disable_view_count': self.disable_view_count
}

def get_update_type(self, old_versions):
Expand All @@ -162,7 +163,8 @@ def get_update_type(self, old_versions):
figures_equal = self.version == old_versions['document']
geom_equal = self.geometry.version == old_versions['geometry'] if \
self.geometry else old_versions['geometry'] is None

disable_view_count_equal = \
self.disable_view_count == old_versions['disable_view_count']
changed_langs = []
locale_versions = old_versions['locales']
for locale in self.locales:
Expand All @@ -179,6 +181,8 @@ def get_update_type(self, old_versions):
update_types.append(UpdateType.GEOM)
if changed_langs:
update_types.append(UpdateType.LANG)
if disable_view_count_equal:
update_types.append(UpdateType.DISABLE_VIEW_COUNT)

return (update_types, changed_langs)

Expand Down
32 changes: 20 additions & 12 deletions c2corg_api/views/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
update_cache_version_associations, get_cache_key
from c2corg_api.models.document import (
UpdateType, DocumentLocale, ArchiveDocumentLocale, ArchiveDocument,
ArchiveDocumentGeometry, set_available_langs, get_available_langs)
ArchiveDocumentGeometry, set_available_langs, get_available_langs,
Document)
from c2corg_api.models.document_history import HistoryMetaData, DocumentVersion
from c2corg_api.models.feed import update_feed_document_create, \
update_feed_document_update
Expand Down Expand Up @@ -132,14 +133,6 @@ def _get(self, document_config, schema, clazz_locale=None,

cache = cache_document_cooked if cook else cache_document_detail

claims = self.request.environ.get('jwtauth.claims', {})
is_bot = claims.get('robot', False)
user_agent = self.request.headers.get('User-Agent', '')
if not (is_bot or is_crawler(user_agent)):
publish(self.request.registry.documents_views_queue_config, id)

user_id = claims.get('sub', False)

def create_response():
return self._get_in_lang(
id, lang, document_config.clazz, schema, editing_view,
Expand All @@ -148,9 +141,11 @@ def create_response():
cook_locale=cook)

response = None
claims = self.request.environ.get('jwtauth.claims', {})
user_id = claims.get('sub', False)

if not editing_view:
increment_document_view_count(self.request, id)
increment_document_view_count(self.request, id, claims)

cache_key = get_cache_key(
id,
Expand Down Expand Up @@ -431,6 +426,12 @@ def update_document(
if document.type != MAP_TYPE and UpdateType.GEOM in update_types:
update_maps_for_document(document, reset=True)

if UpdateType.DISABLE_VIEW_COUNT in update_types:
update_disable_view_count_for_document(
document_in.document_id,
document_in.disable_view_count
)

if after_update:
after_update(document, update_types, user_id=user_id)

Expand Down Expand Up @@ -680,9 +681,16 @@ def f(request, **kwargs):
return f


def increment_document_view_count(request, doc_id):
claims = request.environ.get('jwtauth.claims', {})
def increment_document_view_count(request, doc_id, claims):
is_bot = claims.get('robot', False)
user_agent = request.headers.get('User-Agent', '')
if not (is_bot or is_crawler(user_agent)):
publish(request.registry.documents_views_queue_config, doc_id)


def update_disable_view_count_for_document(document_id, disable_view_count):
""" Update document disable_view_count value
"""
DBSession.query(Document). \
filter(Document.document_id == document_id). \
update({"disable_view_count": disable_view_count})

0 comments on commit 7804a06

Please sign in to comment.