1
# Python
2 6
import json
3

4
# Django
5 6
from django.db import models
6

7
# CyBorgBackup
8 6
from cyborgbackup.api.versioning import reverse
9 6
from cyborgbackup.main.models.base import CreatedModifiedModel
10 6
from cyborgbackup.main.utils.encryption import encrypt_field, decrypt_field
11

12 6
__all__ = ['Setting']
13

14

15 6
class Setting(CreatedModifiedModel):
16

17 6
    SETTING_TYPE_CHOICES = [
18
        ('boolean', 'Boolean'),            # Job was started manually by a user.
19
        ('integer', 'Integer'),        # Job was started via relaunch.
20
        ('string', 'String'),        # Job was started via host callback.
21
        ('privatekey', 'Scheduled'),      # Job was started from a schedule.
22
        ('password', 'Dependency'),    # Job was started as a dependency of another job.
23
        ('workflow', 'Workflow'),        # Job was started from a workflow job.
24
    ]
25

26 6
    key = models.CharField(
27
        max_length=255,
28
    )
29 6
    value = models.TextField(
30
        null=True,
31
    )
32 6
    setting_type = models.CharField(
33
        max_length=20,
34
        choices=SETTING_TYPE_CHOICES,
35
        default='manual',
36
        editable=False,
37
    )
38

39 6
    def get_absolute_url(self, request=None):
40 6
        return reverse('api:setting_detail', kwargs={'pk': self.pk}, request=request)
41

42 6
    def get_ui_url(self):
43 0
        return None
44

45 6
    def __unicode__(self):
46 0
        try:
47 0
            json_value = json.dumps(self.value)
48 0
        except ValueError:
49
            # In the rare case the DB value is invalid JSON.
50 0
            json_value = u'<Invalid JSON>'
51 0
        if self.user:
52 0
            return u'{} ({}) = {}'.format(self.key, self.user, json_value)
53
        else:
54 0
            return u'{} = {}'.format(self.key, json_value)
55

56 6
    def is_setting_encrypted(self):
57 0
        if self.setting_type in ['password', 'privatekey']:
58 0
            return True
59
        else:
60 0
            return False
61

62 6
    def save(self, *args, **kwargs):
63 0
        encrypted = self.is_setting_encrypted()
64
        # If update_fields has been specified, add our field names to it,
65
        # if it hasn't been specified, then we're just doing a normal save.
66 0
        update_fields = kwargs.get('update_fields', [])
67
        # When first saving to the database, don't store any encrypted field
68
        # value, but instead save it until after the instance is created.
69
        # Otherwise, store encrypted value to the database.
70 0
        if encrypted:
71 0
            self.value = encrypt_field(self, 'value')
72 0
            if 'value' not in update_fields:
73 0
                update_fields.append('value')
74 0
        super(Setting, self).save(*args, **kwargs)
75

76 6
    @classmethod
77
    def get_cache_key(self, key):
78 0
        return key
79

80 6
    @classmethod
81
    def get_cache_id_key(self, key):
82 0
        return '{}_ID'.format(key)
83

84 6
    @classmethod
85
    def get_value(self, name):
86 0
        objs = self.objects.filter(key=name)
87 0
        if len(objs) == 1:
88 0
            setting = objs[0]
89 0
            return decrypt_field(setting, 'value')
90
        else:
91 0
            return None

Read our documentation on viewing source code .

Loading