Skip to content

Commit

Permalink
Add transcription count to user ES index, Refs #900
Browse files Browse the repository at this point in the history
  • Loading branch information
rstorey committed Jun 26, 2019
1 parent 3c7fe0c commit de85e7c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
31 changes: 22 additions & 9 deletions concordia/documents.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from django.contrib.auth.models import User
from django_elasticsearch_dsl import DocType, Index, fields

from .models import Asset, SiteReport, Transcription, UserAssetTagCollection
from .models import (
Asset,
SiteReport,
Transcription,
UserAssetTagCollection,
UserProfile,
)

user = Index("users")
user.settings(number_of_shards=1, number_of_replicas=0)
user_profile = Index("user_profiles")
user_profile.settings(number_of_shards=1, number_of_replicas=0)

tag_collection = Index("tags")
tag_collection.settings(number_of_shards=1, number_of_replicas=0)
Expand All @@ -19,12 +24,20 @@
asset.settings(number_of_shards=1, number_of_replicas=0)


@user.doc_type
class UserDocument(DocType):
class Meta:
model = User
@user_profile.doc_type
class UserProfileDocument(DocType):
transcription_count = fields.IntegerField(attr="transcription_count")
user = fields.ObjectField(
properties={
"last_login": fields.DateField(),
"date_joined": fields.DateField(),
"username": fields.KeywordField(),
"is_active": fields.BooleanField(),
}
)

fields = ["last_login", "date_joined", "username", "is_active"]
class Meta:
model = UserProfile


@site_report.doc_type
Expand Down
5 changes: 5 additions & 0 deletions concordia/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
class UserProfile(MetricsModelMixin("userprofile"), models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)

def transcription_count(self):
user_qs = User.get(self.user.id)
user_qs = user_qs.annotate(Count("transcription"))
return user_qs.transcription_count


class TranscriptionStatus(object):
"""
Expand Down

0 comments on commit de85e7c

Please sign in to comment.