1 3
import logging
2 3
import gzip
3 3
import base64
4 3
import json
5

6 3
from django.db import models
7 3
from elasticsearch import Elasticsearch
8

9 3
from cyborgbackup.api.versioning import reverse
10 3
from cyborgbackup.main.models.base import PrimordialModel
11

12 3
logger = logging.getLogger('cyborgbackup.models.Catalog')
13

14 3
__all__ = ['Catalog']
15

16

17 3
class Catalog(PrimordialModel):
18

19 3
    archive_name = models.CharField(
20
        max_length=1024,
21
    )
22

23 3
    mode = models.CharField(
24
        max_length=10
25
    )
26

27 3
    path = models.CharField(
28
        max_length=2048,
29
    )
30

31 3
    owner = models.CharField(
32
        max_length=1024
33
    )
34

35 3
    group = models.CharField(
36
        max_length=1024
37
    )
38

39 3
    type = models.CharField(
40
        max_length=1
41
    )
42

43 3
    healthy = models.BooleanField()
44

45 3
    size = models.PositiveIntegerField()
46

47 3
    mtime = models.DateTimeField()
48

49 3
    job = models.ForeignKey(
50
        'Job',
51
        related_name='catalogs',
52
        on_delete=models.CASCADE,
53
        null=False,
54
        editable=True,
55
    )
56

57 3
    def get_absolute_url(self, request=None):
58 0
        return reverse('api:catalog_detail', kwargs={'pk': self.pk}, request=request)
59

60 3
    def get_ui_url(self):
61 0
        return "/#/catalogs/{}".format(self.pk)
62

63 3
    @classmethod
64
    def create_from_data(self, **kwargs):
65 0
        pk = None
66 0
        for key in ('archive_name',):
67 0
            if key in kwargs:
68 0
                pk = key
69 0
        if pk is None:
70 0
            return
71

72 0
        archive_name = kwargs['archive_name']
73 0
        job = kwargs['job']
74 0
        catalog_data = kwargs['catalog']
75 0
        catalogs_entries_raw = gzip.decompress(base64.b64decode(catalog_data))
76 0
        catalog_entries = json.loads(catalogs_entries_raw.decode('utf-8'))
77 0
        created = []
78
        # es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
79 0
        for entry in catalog_entries:
80 0
            entry.update({'archive_name': archive_name, 'job_id': job})
81 0
            created.append(self.objects.create(**entry))
82
            # es.index(index='catalog', doc_type='entry', body={
83
            #     'path': entry['path'],
84
            #     'job': entry['job_id'],
85
            #     'archive_name': entry['archive_name'],
86
            #     'mode': entry['mode'],
87
            #     'owner': entry['user'],
88
            #     'group': entry['group'],
89
            #     'type': entry['type'],
90
            #     'size': entry['size'],
91
            #     'healthy': entry['healthy'],
92
            #     'mtime': entry['mtime']
93
            # });
94 0
        logger.info('Catalog data saved.', extra=dict(python_objects=dict(created=len(created))))
95 0
        return len(created)
96

97 3
    @classmethod
98
    def get_cache_key(self, key):
99 0
        return key
100

101 3
    @classmethod
102
    def get_cache_id_key(self, key):
103 0
        return '{}_ID'.format(key)
104

105 3
    def __str__(self):
106 0
        return 'catalog'

Read our documentation on viewing source code .

Loading