Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / quotaholder / models.py @ 003909e3

History | View | Annotate | Download (4.8 kB)

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