• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openhealthcare / elcid-rfh / 2225

pending completion
2225

Pull #528

travis-ci

web-flow
Merge upsream v0.3.7-api refactor
Pull Request #528: V0.3.7 api refactor

57 of 205 branches covered (27.8%)

785 of 785 new or added lines in 25 files covered. (100.0%)

2211 of 2886 relevant lines covered (76.61%)

1.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.88
/intrahospital_api/models.py
1
from django.db import models
1✔
2
from django.utils import timezone
1✔
3
from elcid.utils import model_method_logging
1✔
4
import opal.models as omodels
1✔
5
from opal.models import PatientSubrecord
1✔
6
from opal.core.fields import ForeignKeyOrFreeText
1✔
7
from intrahospital_api import exceptions
1✔
8

9

10
class ExternalDemographics(PatientSubrecord):
1✔
11
    _is_singleton = True
1✔
12
    _icon = "fa fa-handshake-o"
1✔
13
    _advanced_searchable = False
1✔
14
    _exclude_from_extract = True
1✔
15

16
    hospital_number = models.CharField(max_length=255, blank=True)
1✔
17
    nhs_number = models.CharField(max_length=255, blank=True, null=True)
1✔
18
    surname = models.CharField(max_length=255, blank=True)
1✔
19
    first_name = models.CharField(max_length=255, blank=True)
1✔
20
    middle_name = models.CharField(max_length=255, blank=True, null=True)
1✔
21
    title = ForeignKeyOrFreeText(omodels.Title)
1✔
22
    date_of_birth = models.DateField(null=True, blank=True)
1✔
23
    sex = ForeignKeyOrFreeText(omodels.Gender)
1✔
24
    ethnicity = ForeignKeyOrFreeText(omodels.Ethnicity)
1✔
25

26

27
class PatientLoad(models.Model):
1✔
28
    RUNNING = "running"
1✔
29
    FAILURE = "failure"
1✔
30
    SUCCESS = "success"
1✔
31

32
    state = models.CharField(
1✔
33
        max_length=255,
34
        blank=True,
35
        null=True
36
    )
37

38
    started = models.DateTimeField()
1✔
39
    stopped = models.DateTimeField(
1✔
40
        blank=True, null=True
41
    )
42
    count = models.IntegerField(
1✔
43
        blank=True, null=True
44
    )
45

46
    @property
1✔
47
    def verbose_name(self):
48
        return self.__class__._meta.verbose_name
×
49

50
    @model_method_logging
1✔
51
    def start(self):
52
        self.started = timezone.now()
1✔
53
        self.state = self.RUNNING
1✔
54
        self.save()
1✔
55

56
    @property
1✔
57
    def duration(self):
58
        if not self.stopped:
1✔
59
            raise ValueError(
×
60
                "%s has not finished yet" % self
61
            )
62
        return self.stopped - self.started
1✔
63

64
    @model_method_logging
1✔
65
    def complete(self):
66
        self.stopped = timezone.now()
1✔
67
        self.state = self.SUCCESS
1✔
68
        self.save()
1✔
69

70
    @model_method_logging
1✔
71
    def failed(self):
72
        self.stopped = timezone.now()
1✔
73
        self.state = self.FAILURE
1✔
74
        self.save()
1✔
75

76
    class Meta:
1✔
77
        abstract = True
1✔
78
        ordering = ('started',)
1✔
79

80

81
class InitialPatientLoad(PatientLoad, PatientSubrecord):
1✔
82
    """
83
    This model is the initial load of a patient
84
    future loads are done by the cron batch load
85
    """
86
    _advanced_searchable = False
1✔
87
    _exclude_from_extract = True
1✔
88

89
    def __unicode__(self):
90
        hospital_number = self.patient.demographics_set.first().hospital_number
91
        if self.stopped:
92
            return "{} {} {} {}".format(
93
                hospital_number,
94
                self.state,
95
                self.started,
96
                self.duration
97
            )
98
        else:
99
            return "{} {} {}".format(
100
                hospital_number,
101
                self.state,
102
                self.started
103
            )
104

105
    def update_from_dict(self, data, *args, **kwargs):
1✔
106
        """
107
        For the purposes of the front end this model is read only.
108
        """
109
        pass
1✔
110

111

112
class BatchPatientLoad(PatientLoad):
1✔
113
    """
114
    This is a load that runs at a regular interval for a particular service.
115
    """
116

117
    service_name = models.CharField(
1✔
118
        max_length=255, blank=True, default=""
119
    )
120

121
    def __unicode__(self):
122
        if self.stopped:
123
            return "{} {} {} {} {}".format(
124
                self.service_name,
125
                self.state,
126
                self.started,
127
                self.count,
128
                self.duration
129
            )
130
        else:
131
            return "{} {} {} {}".format(
132
                self.service_name,
133
                self.state,
134
                self.started,
135
                self.count
136
            )
137

138
    def save(self, *args, **kwargs):
1✔
139
        if self.state == self.RUNNING:
1✔
140
            existing_batch_load = self.__class__.objects.filter(
1✔
141
                service_name=self.service_name,
142
                state=self.RUNNING
143
            ).first()
144
            if existing_batch_load:
1✔
145
                err = "Trying to start a batch for {} when {} is still \
1✔
146
running"
147
                raise exceptions.BatchLoadError(err.format(
1✔
148
                    self.service_name, existing_batch_load.id)
149
                )
150

151
        super(BatchPatientLoad, self).save(*args, **kwargs)
1✔
152

153

154

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc