root / snf-astakos-app / astakos / quotaholder / models.py @ b78f2b29
History | View | Annotate | Download (4.4 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 Holding(Model): |
43 |
|
44 |
holder = CharField(max_length=4096, db_index=True) |
45 |
source = CharField(max_length=4096, null=True) |
46 |
resource = CharField(max_length=4096, null=False) |
47 |
|
48 |
limit = intDecimalField() |
49 |
flags = BigIntegerField(null=False, default=0) |
50 |
|
51 |
imported_min = intDecimalField(default=0)
|
52 |
imported_max = intDecimalField(default=0)
|
53 |
|
54 |
objects = ForUpdateManager() |
55 |
|
56 |
class Meta: |
57 |
unique_together = (('holder', 'source', 'resource'),) |
58 |
|
59 |
|
60 |
from datetime import datetime |
61 |
|
62 |
def now(): |
63 |
return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:24] |
64 |
|
65 |
|
66 |
class Commission(Model): |
67 |
|
68 |
serial = AutoField(primary_key=True)
|
69 |
name = CharField(max_length=4096, null=True) |
70 |
clientkey = CharField(max_length=4096, null=False) |
71 |
issue_time = CharField(max_length=24, default=now)
|
72 |
|
73 |
objects = ForUpdateManager() |
74 |
|
75 |
class Provision(Model): |
76 |
|
77 |
serial = ForeignKey( Commission, |
78 |
to_field='serial',
|
79 |
related_name='provisions' )
|
80 |
holding = ForeignKey(Holding, |
81 |
related_name='provisions')
|
82 |
quantity = intDecimalField() |
83 |
|
84 |
objects = ForUpdateManager() |
85 |
|
86 |
class ProvisionLog(Model): |
87 |
|
88 |
serial = BigIntegerField() |
89 |
name = CharField(max_length=4096, null=True) |
90 |
issue_time = CharField(max_length=4096)
|
91 |
log_time = CharField(max_length=4096)
|
92 |
holder = CharField(max_length=4096)
|
93 |
source = CharField(max_length=4096, null=True) |
94 |
resource = CharField(max_length=4096)
|
95 |
limit = intDecimalField() |
96 |
imported_min = intDecimalField() |
97 |
imported_max = intDecimalField() |
98 |
delta_quantity = intDecimalField() |
99 |
reason = CharField(max_length=4096)
|
100 |
|
101 |
objects = ForUpdateManager() |
102 |
|
103 |
|
104 |
def _get(*args, **kwargs): |
105 |
model = args[0]
|
106 |
args = args[1:]
|
107 |
o = model.objects |
108 |
|
109 |
for_update = kwargs.pop('for_update', False) |
110 |
f = o.get_for_update if for_update else o.get |
111 |
return f(*args, **kwargs)
|
112 |
|
113 |
|
114 |
def _filter(*args, **kwargs): |
115 |
model = args[0]
|
116 |
args = args[1:]
|
117 |
o = model.objects |
118 |
|
119 |
for_update = kwargs.pop('for_update', False) |
120 |
q = o.filter(*args, **kwargs) |
121 |
q = q.select_for_update() if for_update else q |
122 |
return q
|
123 |
|
124 |
|
125 |
def db_get_holding(*args, **kwargs): |
126 |
return _get(Holding, *args, **kwargs)
|
127 |
|
128 |
def db_get_commission(*args, **kwargs): |
129 |
return _get(Commission, *args, **kwargs)
|
130 |
|
131 |
def db_filter_provision(*args, **kwargs): |
132 |
return _filter(Provision, *args, **kwargs)
|