root / snf-astakos-app / astakos / quotaholder / models.py @ d03796c2
History | View | Annotate | Download (5.1 kB)
1 |
# Copyright 2012, 2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
|
35 |
from synnefo.lib.db.intdecimalfield import intDecimalField |
36 |
|
37 |
from django.db.models import (Model, BigIntegerField, CharField, |
38 |
ForeignKey, AutoField) |
39 |
from django.db import transaction |
40 |
from synnefo.lib.db.managers import ForUpdateManager |
41 |
|
42 |
class Policy(Model): |
43 |
|
44 |
policy = CharField(max_length=4096, primary_key=True) |
45 |
capacity = intDecimalField() |
46 |
|
47 |
objects = ForUpdateManager() |
48 |
|
49 |
class Holding(Model): |
50 |
|
51 |
holder = CharField(max_length=4096, db_index=True) |
52 |
resource = CharField(max_length=4096, null=False) |
53 |
|
54 |
policy = ForeignKey(Policy, to_field='policy')
|
55 |
flags = BigIntegerField(null=False, default=0) |
56 |
|
57 |
imported_min = intDecimalField(default=0)
|
58 |
imported_max = intDecimalField(default=0)
|
59 |
stock_min = intDecimalField(default=0)
|
60 |
stock_max = intDecimalField(default=0)
|
61 |
|
62 |
objects = ForUpdateManager() |
63 |
|
64 |
class Meta: |
65 |
unique_together = (('holder', 'resource'),) |
66 |
|
67 |
|
68 |
from datetime import datetime |
69 |
|
70 |
def now(): |
71 |
return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:24] |
72 |
|
73 |
|
74 |
class Commission(Model): |
75 |
|
76 |
serial = AutoField(primary_key=True)
|
77 |
holder = CharField(max_length=4096, db_index=True) |
78 |
name = CharField(max_length=4096, null=True) |
79 |
clientkey = CharField(max_length=4096, null=False) |
80 |
issue_time = CharField(max_length=24, default=now)
|
81 |
|
82 |
objects = ForUpdateManager() |
83 |
|
84 |
class Provision(Model): |
85 |
|
86 |
serial = ForeignKey( Commission, |
87 |
to_field='serial',
|
88 |
related_name='provisions' )
|
89 |
|
90 |
holder = CharField(max_length=4096, db_index=True) |
91 |
resource = CharField(max_length=4096, null=False) |
92 |
quantity = intDecimalField() |
93 |
|
94 |
objects = ForUpdateManager() |
95 |
|
96 |
class ProvisionLog(Model): |
97 |
|
98 |
serial = BigIntegerField() |
99 |
source = CharField(max_length=4096)
|
100 |
target = CharField(max_length=4096)
|
101 |
name = CharField(max_length=4096)
|
102 |
issue_time = CharField(max_length=4096)
|
103 |
log_time = CharField(max_length=4096)
|
104 |
resource = CharField(max_length=4096)
|
105 |
source_capacity = intDecimalField() |
106 |
source_imported_min = intDecimalField() |
107 |
source_imported_max = intDecimalField() |
108 |
source_stock_min = intDecimalField() |
109 |
source_stock_max = intDecimalField() |
110 |
target_capacity = intDecimalField() |
111 |
target_imported_min = intDecimalField() |
112 |
target_imported_max = intDecimalField() |
113 |
target_stock_min = intDecimalField() |
114 |
target_stock_max = intDecimalField() |
115 |
delta_quantity = intDecimalField() |
116 |
reason = CharField(max_length=4096)
|
117 |
|
118 |
objects = ForUpdateManager() |
119 |
|
120 |
|
121 |
def _get(*args, **kwargs): |
122 |
model = args[0]
|
123 |
args = args[1:]
|
124 |
o = model.objects |
125 |
|
126 |
for_update = kwargs.pop('for_update', False) |
127 |
f = o.get_for_update if for_update else o.get |
128 |
return f(*args, **kwargs)
|
129 |
|
130 |
|
131 |
def _filter(*args, **kwargs): |
132 |
model = args[0]
|
133 |
args = args[1:]
|
134 |
o = model.objects |
135 |
|
136 |
for_update = kwargs.pop('for_update', False) |
137 |
q = o.filter(*args, **kwargs) |
138 |
q = q.select_for_update() if for_update else q |
139 |
return q
|
140 |
|
141 |
|
142 |
def db_get_holding(*args, **kwargs): |
143 |
return _get(Holding, *args, **kwargs)
|
144 |
|
145 |
def db_get_policy(*args, **kwargs): |
146 |
return _get(Policy, *args, **kwargs)
|
147 |
|
148 |
def db_get_commission(*args, **kwargs): |
149 |
return _get(Commission, *args, **kwargs)
|
150 |
|
151 |
def db_filter_provision(*args, **kwargs): |
152 |
return _filter(Provision, *args, **kwargs)
|