Completed
Pull Request — master (#44)
by Paolo
11:03 queued 04:44
created

biosample.models.Submission.__str__()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
from django.db import models
2
from django.contrib.auth.models import User
3
from django.contrib.contenttypes.fields import GenericForeignKey
4
from django.contrib.contenttypes.models import ContentType
5
6
from common.constants import STATUSES, NAME_STATUSES, WAITING, LOADED
7
from image_app.models import Submission as UIDSubmission
8
9
10
# Create your models here.
11
class Account(models.Model):
12
    user = models.OneToOneField(
13
        User,
14
        on_delete=models.CASCADE,
15
        related_name='biosample_account')
16
    name = models.SlugField()
17
    team = models.ForeignKey(
18
        'ManagedTeam',
19
        on_delete=models.CASCADE,
20
        help_text="Your AAP Team")
21
22
    def __str__(self):
23
        full_name = " ".join([self.user.first_name, self.user.last_name])
24
        return "%s (%s)" % (self.name, full_name)
25
26
27
class ManagedTeam(models.Model):
28
    name = models.CharField(max_length=255, unique=True)
29
30
    @classmethod
31
    def get_teams(cls):
32
        teams = cls.objects.all()
33
34
        return [team.name for team in teams]
35
36
    def __str__(self):
37
        return self.name
38
39
    class Meta:
40
        verbose_name = "managed team"
41
        verbose_name_plural = "managed teams"
42
43
44
class Submission(models.Model):
45
    uid_submission = models.ForeignKey(
46
        UIDSubmission,
47
        on_delete=models.CASCADE,
48
        related_name='usi_submissions')
49
50
    usi_submission_name = models.CharField(
51
        max_length=255,
52
        blank=True,
53
        null=True,
54
        db_index=True,
55
        unique=True,
56
        help_text='USI submission name')
57
58
    created_at = models.DateTimeField(auto_now_add=True)
59
60
    updated_at = models.DateTimeField(auto_now=True)
61
62
    # a field to track errors in UID loading. Should be blank if no errors
63
    # are found
64
    message = models.TextField(
65
        null=True,
66
        blank=True)
67
68
    # a column to track submission status
69
    # HINT: should I limit by biosample status?
70
    status = models.SmallIntegerField(
71
        choices=[x.value for x in STATUSES],
72
        help_text='example: Waiting',
73
        default=WAITING)
74
75
    def __str__(self):
76
        return "%s <%s> (%s): %s" % (
77
            self.id,
78
            self.usi_submission_name,
79
            self.uid_submission,
80
            self.get_status_display())
81
82
83
class SubmissionData(models.Model):
84
    submission = models.ForeignKey(
85
        Submission,
86
        on_delete=models.CASCADE,
87
        related_name='submission_data')
88
89
    # limit choices for contenttypes
90
    # https://axiacore.com/blog/how-use-genericforeignkey-django-531/
91
    name_limit = models.Q(app_label='image_app', model='animal') | \
92
        models.Q(app_label='image_app', model='sample')
93
94
    # Below the mandatory fields for generic relation
95
    # https://simpleisbetterthancomplex.com/tutorial/2016/10/13/how-to-use-generic-relations.html
96
    content_type = models.ForeignKey(
97
        ContentType,
98
        on_delete=models.CASCADE,
99
        limit_choices_to=name_limit)
100
101
    object_id = models.PositiveIntegerField()
102
    content_object = GenericForeignKey('content_type', 'object_id')
103
104
    def __str__(self):
105
        return "%s <%s>: %s" % (
106
            self.submission.id,
107
            self.submission.usi_submission_name,
108
            self.content_object.name)
109
110
    class Meta:
111
        verbose_name = "submission data"
112
        verbose_name_plural = "submission data"
113