~caio1982/capomastro/releasedocs

« back to all changes in this revision

Viewing changes to archives/models.py

"automatic merge of lp:~caio1982/capomastro/missingcreds-1433354/ by tarmac [r=roadmr,jamesj,codersquid][bug=1433354][author=caio1982]"

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
import urlparse
3
3
from collections import OrderedDict
4
4
 
 
5
from django.conf import settings
 
6
from django.core.exceptions import ValidationError
5
7
from django.db import models
6
8
from django.utils.encoding import python_2_unicode_compatible
7
 
from django.conf import settings
8
9
 
9
10
from jenkins.models import Artifact, Build
10
11
from credentials.models import SshKeyPair, SwiftCredential
49
50
    default = models.BooleanField(default=False)
50
51
    base_url = models.CharField(max_length=200, blank=True, default="")
51
52
 
 
53
    def clean(self):
 
54
        """
 
55
        Overrides the model validation after each field had been verified,
 
56
        as this is the only place where we can run multi-field checks.
 
57
 
 
58
        The downside of these validations is that if we eventually
 
59
        create more network transports in the model we'll have to
 
60
        manually add them to these checks, but I can't see a better
 
61
        way to validate them at the moment directly in the model.
 
62
        """
 
63
        if ("local" not in self.transport
 
64
                and self.ssh_credentials is None
 
65
                and self.swift_credentials is None):
 
66
            alert = ("Credentials are required if the selected "
 
67
                     "transport is not local.")
 
68
            raise ValidationError(alert)
 
69
 
 
70
        if "swift" in self.transport and self.swift_credentials is None:
 
71
            alert = ("Missing set of Swift credentials, "
 
72
                     "please create/select one.")
 
73
            raise ValidationError(alert)
 
74
 
 
75
        if "ssh" in self.transport and self.ssh_credentials is None:
 
76
            alert = ("Missing set of SSH credentials, "
 
77
                     "please create/select one.")
 
78
            raise ValidationError(alert)
 
79
 
52
80
    def __str__(self):
53
81
        return "{name} default: {default}".format(name=self.name,
54
82
                                                  default=self.default)