Skip to content
Snippets Groups Projects
Commit 72ac8dd5 authored by jan.bednarik's avatar jan.bednarik
Browse files

ReportDoc improvements and model tests for Report.

parent 502e98d1
No related branches found
No related tags found
No related merge requests found
from django.conf import settings from django.conf import settings
from django_elasticsearch_dsl import DocType, Index, fields from django_elasticsearch_dsl import DocType, Index, fields
import json
from .models import Report from .models import Report
...@@ -16,7 +17,14 @@ class ReportDoc(DocType): ...@@ -16,7 +17,14 @@ class ReportDoc(DocType):
provided_benefit = fields.TextField(analyzer=settings.ES_TEXT_ANALYZER) provided_benefit = fields.TextField(analyzer=settings.ES_TEXT_ANALYZER)
our_participants = fields.TextField(analyzer=settings.ES_TEXT_ANALYZER) our_participants = fields.TextField(analyzer=settings.ES_TEXT_ANALYZER)
other_participants = fields.TextField(analyzer=settings.ES_TEXT_ANALYZER) other_participants = fields.TextField(analyzer=settings.ES_TEXT_ANALYZER)
extra = fields.ObjectField() # there is no support for JSONField now, so we serialize it to text
extra = fields.TextField()
def prepare_extra(self, instance):
return json.dumps(instance.extra)
def get_extra(self):
return json.loads(self.extra)
class Meta: class Meta:
model = Report model = Report
......
...@@ -2,6 +2,7 @@ from django.conf import settings ...@@ -2,6 +2,7 @@ from django.conf import settings
from django.db import models from django.db import models
from django.contrib.auth.models import AbstractUser from django.contrib.auth.models import AbstractUser
from django.contrib.postgres.fields import JSONField from django.contrib.postgres.fields import JSONField
from django.utils import timezone
class User(AbstractUser): class User(AbstractUser):
...@@ -39,7 +40,7 @@ class LoginAttempt(models.Model): ...@@ -39,7 +40,7 @@ class LoginAttempt(models.Model):
class Report(models.Model): class Report(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT) author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
date = models.DateTimeField() date = models.DateTimeField()
published = models.DateTimeField() published = models.DateTimeField(default=timezone.now)
title = models.TextField(null=True, blank=True) title = models.TextField(null=True, blank=True)
body = models.TextField() body = models.TextField()
received_benefit = models.TextField(null=True, blank=True) received_benefit = models.TextField(null=True, blank=True)
...@@ -47,3 +48,7 @@ class Report(models.Model): ...@@ -47,3 +48,7 @@ class Report(models.Model):
our_participants = models.TextField(null=True, blank=True) our_participants = models.TextField(null=True, blank=True)
other_participants = models.TextField(null=True, blank=True) other_participants = models.TextField(null=True, blank=True)
extra = JSONField(null=True, blank=True) extra = JSONField(null=True, blank=True)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
User.objects.filter(id=self.author.id).update(is_author=True)
# -*- coding: utf-8 -*-
# snapshottest: v1 - https://goo.gl/zC4yUc
from __future__ import unicode_literals
from snapshottest import Snapshot, GenericRepr
snapshots = Snapshot()
snapshots['test_report_is_saved_in_elasticsearch 1'] = [
GenericRepr("ReportDoc(index='report_ded_test', doc_type='report_doc', id='2')")
]
import pytest
import arrow
from openlobby.core.models import Report, User
from openlobby.core.documents import ReportDoc
pytestmark = [pytest.mark.django_db, pytest.mark.usefixtures('django_es')]
def test_report_marks_user_as_author_on_save():
author = User.objects.create(id=1, is_author=False)
date = arrow.get(2018, 1, 1).datetime
Report.objects.create(author=author, date=date, body='Lorem ipsum.')
user = User.objects.get(id=1)
assert user.is_author
def test_report_is_saved_in_elasticsearch():
author = User.objects.create(id=6)
date = arrow.get(2018, 1, 1).datetime
published = arrow.get(2018, 1, 2).datetime
Report.objects.create(
id=3,
author=author,
date=date,
published=published,
title='It happened',
body='Lorem ipsum.',
received_benefit='coffee',
provided_benefit='tea',
our_participants='me',
other_participants='them',
extra={'a': 3},
)
docs = list(ReportDoc.search())
assert len(docs) == 1
doc = docs[0]
assert doc.meta.id == '3'
assert doc.author_id == 6
assert doc.date == date
assert doc.published == published
assert doc.title == 'It happened'
assert doc.body == 'Lorem ipsum.'
assert doc.received_benefit == 'coffee'
assert doc.provided_benefit == 'tea'
assert doc.our_participants == 'me'
assert doc.other_participants == 'them'
assert doc.get_extra() == {'a': 3}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment