Statistics
| Branch: | Tag: | Revision:

root / commissioning / servers / quotaholder / django_backend / models.py @ fac1de87

History | View | Annotate | Download (2.8 kB)

1

    
2
from commissioning import ( Callpoint, Controller,
3
                            Physical, CommissionException,
4
                            CorruptedError, InvalidDataError,
5
                            InvalidKeyError, NoEntityError,
6
                            NoQuantityError, NoCapacityError    )
7

    
8

    
9
from django.db.models import Model, BigIntegerField, CharField, ForeignKey
10
from django.db import transaction
11

    
12

    
13
class Holder(Model):
14

    
15
    attribute   =   CharField(max_length=72, primary_key=True)
16
    intval      =   BigIntegerField()
17
    strval      =   CharField(max_length=72)
18

    
19

    
20
def alloc_serial(nr=1):
21
    if nr < 0:
22
        m = "Can only receive a positive argument, not %d" % (nr,)
23
        raise CorruptedError(m)
24

    
25
    try:
26
        holder = Holder.objects.get(attribute='serial')
27
    except Holder.DoesNotExist:
28
        holder = Holder(attribute='serial', inval=1)
29

    
30
    serial = holder.serial
31
    holder.serial += nr
32
    holder.save()
33

    
34
    return serial
35

    
36

    
37
class Entity(Model):
38

    
39
    entity      =   CharField(max_length=72, primary_key=True)
40
    owner       =   ForeignKey('self', to_field='entity',
41
                               related_name='entities')
42
    key         =   CharField(max_length=72, null=False)
43

    
44

    
45
class Policy(Model):
46

    
47
    policy          =   CharField(max_length=72, primary_key=True)
48
    quantity        =   BigIntegerField(null=False, default=0)
49
    capacity        =   BigIntegerField(null=False, default=0)
50
    import_limit    =   BigIntegerField(null=False, default=0)
51
    export_limit    =   BigIntegerField(null=False, default=0)
52

    
53

    
54
class Holding(Model):
55

    
56
    entity      =   ForeignKey(Entity, to_field='entity')
57
    resource    =   CharField(max_length=72, null=False)
58

    
59
    policy      =   ForeignKey(Policy, to_field='policy')
60
    flags       =   BigIntegerField(null=False, default=0)
61

    
62
    imported    =   BigIntegerField(null=False, default=0)
63
    importing   =   BigIntegerField(null=False, default=0)
64
    exported    =   BigIntegerField(null=False, default=0)
65
    exporting   =   BigIntegerField(null=False, default=0)
66

    
67
    class Meta:
68
        unique_together = (('entity', 'resource'),)
69

    
70

    
71
from datetime import datetime
72

    
73
def now():
74
    return datetime.now().strftime('%Y-%m-%dT:%H:%M:%S.%f')[:24]
75

    
76

    
77
class Commission(Model):
78

    
79
    serial      =   BigIntegerField(primary_key=True, default=alloc_serial)
80
    entity      =   ForeignKey(Entity, to_field='entity')
81
    clientkey   =   CharField(max_length=72, null=False)
82
    issue_time  =   CharField(max_length=24, default=now)
83

    
84

    
85
class Provision(Model):
86

    
87
    serial      =   ForeignKey( Commission,
88
                                to_field='serial',
89
                                related_name='provisions'   )
90

    
91
    entity      =   ForeignKey(Entity, to_field='entity')
92
    resource    =   CharField(max_length=72, null=False)
93
    quantity    =   BigIntegerField(null=False)
94

    
95