Commit c315ba91 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

quotaholder: Store timestamps using DateTimeField

Add new fields issue_datetime in Commission and issue_datetime,
log_datetime in ProvisionLog. Migrate away from custom string-based
issue_time and log_time fields.
parent 5fd771da
......@@ -477,7 +477,7 @@ pending_commissions = [100, 200]
commission_description = {
"serial": 57,
"issue_time": "2013-04-08T10:19:15.0373",
"issue_time": "2013-04-08T10:19:15.0373+00:00",
"name": "a commission",
"provisions": [
{
......
......@@ -353,7 +353,7 @@ Status Description
{
"serial": 57,
"issue_time": "2013-04-08T10:19:15.0373",
"issue_time": "2013-04-08T10:19:15.0373+00:00",
"name": "an optional description",
"provisions": [
{
......
......@@ -31,6 +31,7 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from datetime import datetime
from django.db.models import F
from astakos.quotaholder_app.exception import (
QuotaholderError,
......@@ -43,7 +44,7 @@ from astakos.quotaholder_app.commission import (
Import, Release, Operations, finalize, undo)
from astakos.quotaholder_app.models import (
Holding, Commission, Provision, ProvisionLog, now)
Holding, Commission, Provision, ProvisionLog)
def get_quota(holders=None, sources=None, resources=None):
......@@ -165,7 +166,7 @@ def issue_commission(clientkey, provisions, name="", force=False):
commission = Commission.objects.create(clientkey=clientkey,
name=name,
issue_time=now())
issue_datetime=datetime.now())
for (holder, source, resource), quantity in provisions_to_create:
Provision.objects.create(serial=commission,
holder=holder,
......@@ -176,7 +177,7 @@ def issue_commission(clientkey, provisions, name="", force=False):
return commission.serial
def _log_provision(commission, provision, holding, log_time, reason):
def _log_provision(commission, provision, holding, log_datetime, reason):
kwargs = {
'serial': commission.serial,
......@@ -188,8 +189,8 @@ def _log_provision(commission, provision, holding, log_time, reason):
'usage_min': holding.usage_min,
'usage_max': holding.usage_max,
'delta_quantity': provision.quantity,
'issue_time': commission.issue_time,
'log_time': log_time,
'issue_datetime': commission.issue_datetime,
'log_datetime': log_datetime,
'reason': reason,
}
......@@ -240,7 +241,7 @@ def resolve_pending_commissions(clientkey, accept_set=None, reject_set=None,
holdings = _get_holdings_for_update(holding_keys)
provisions = _partition_by(lambda p: p.serial_id, ps)
log_time = now()
log_datetime = datetime.now()
accepted, rejected, notFound = [], [], []
for serial, accept in actions.iteritems():
......@@ -267,7 +268,7 @@ def resolve_pending_commissions(clientkey, accept_set=None, reject_set=None,
prefix = 'ACCEPT:' if accept else 'REJECT:'
comm_reason = prefix + reason[-121:]
_log_provision(commission, pv, h, log_time, comm_reason)
_log_provision(commission, pv, h, log_datetime, comm_reason)
pv.delete()
commission.delete()
return accepted, rejected, notFound, conflicting
......@@ -306,7 +307,7 @@ def get_commission(clientkey, serial):
response = {'serial': serial,
'provisions': ps,
'issue_time': commission.issue_time,
'issue_time': commission.issue_datetime,
'name': commission.name,
}
return response
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Adding field 'Commission.issue_datetime'
db.add_column('quotaholder_app_commission', 'issue_datetime', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime(2013, 1, 1, 0, 0, 0)), keep_default=False)
# Adding field 'ProvisionLog.issue_datetime'
db.add_column('quotaholder_app_provisionlog', 'issue_datetime', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime(2013, 1, 1, 0, 0, 0)), keep_default=False)
# Adding field 'ProvisionLog.log_datetime'
db.add_column('quotaholder_app_provisionlog', 'log_datetime', self.gf('django.db.models.fields.DateTimeField')(default=datetime.datetime(2013, 1, 1, 0, 0, 0)), keep_default=False)
def backwards(self, orm):
# Deleting field 'Commission.issue_datetime'
db.delete_column('quotaholder_app_commission', 'issue_datetime')
# Deleting field 'ProvisionLog.issue_datetime'
db.delete_column('quotaholder_app_provisionlog', 'issue_datetime')
# Deleting field 'ProvisionLog.log_datetime'
db.delete_column('quotaholder_app_provisionlog', 'log_datetime')
models = {
'quotaholder_app.commission': {
'Meta': {'object_name': 'Commission'},
'clientkey': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'issue_datetime': ('django.db.models.fields.DateTimeField', [], {}),
'issue_time': ('django.db.models.fields.CharField', [], {'max_length': '24'}),
'name': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '4096'}),
'serial': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
},
'quotaholder_app.holding': {
'Meta': {'unique_together': "(('holder', 'source', 'resource'),)", 'object_name': 'Holding'},
'holder': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'db_index': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'limit': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'resource': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'source': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'null': 'True'}),
'usage_max': ('snf_django.lib.db.fields.IntDecimalField', [], {'default': '0', 'max_digits': '38', 'decimal_places': '0'}),
'usage_min': ('snf_django.lib.db.fields.IntDecimalField', [], {'default': '0', 'max_digits': '38', 'decimal_places': '0'})
},
'quotaholder_app.provision': {
'Meta': {'object_name': 'Provision'},
'holder': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'db_index': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'quantity': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'resource': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'provisions'", 'to': "orm['quotaholder_app.Commission']"}),
'source': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'null': 'True'})
},
'quotaholder_app.provisionlog': {
'Meta': {'object_name': 'ProvisionLog'},
'delta_quantity': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'holder': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'issue_datetime': ('django.db.models.fields.DateTimeField', [], {}),
'issue_time': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'limit': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'log_datetime': ('django.db.models.fields.DateTimeField', [], {}),
'log_time': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'reason': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'resource': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'serial': ('django.db.models.fields.BigIntegerField', [], {}),
'source': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'null': 'True'}),
'usage_max': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'usage_min': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'})
}
}
complete_apps = ['quotaholder_app']
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import DataMigration
from django.db import models
class Migration(DataMigration):
def forwards(self, orm):
parse = lambda s: datetime.datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%f')
commissions = orm.Commission.objects.all()
for commission in commissions:
dt = parse(commission.issue_time)
commission.issue_datetime = dt
commission.save()
plogs = orm.ProvisionLog.objects.all()
for plog in plogs:
issue_dt = parse(plog.issue_time)
plog.issue_datetime = issue_dt
log_dt = parse(plog.log_time)
plog.log_datetime = log_dt
plog.save()
def backwards(self, orm):
"Write your backwards methods here."
models = {
'quotaholder_app.commission': {
'Meta': {'object_name': 'Commission'},
'clientkey': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'issue_datetime': ('django.db.models.fields.DateTimeField', [], {}),
'issue_time': ('django.db.models.fields.CharField', [], {'max_length': '24'}),
'name': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '4096'}),
'serial': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
},
'quotaholder_app.holding': {
'Meta': {'unique_together': "(('holder', 'source', 'resource'),)", 'object_name': 'Holding'},
'holder': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'db_index': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'limit': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'resource': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'source': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'null': 'True'}),
'usage_max': ('snf_django.lib.db.fields.IntDecimalField', [], {'default': '0', 'max_digits': '38', 'decimal_places': '0'}),
'usage_min': ('snf_django.lib.db.fields.IntDecimalField', [], {'default': '0', 'max_digits': '38', 'decimal_places': '0'})
},
'quotaholder_app.provision': {
'Meta': {'object_name': 'Provision'},
'holder': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'db_index': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'quantity': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'resource': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'provisions'", 'to': "orm['quotaholder_app.Commission']"}),
'source': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'null': 'True'})
},
'quotaholder_app.provisionlog': {
'Meta': {'object_name': 'ProvisionLog'},
'delta_quantity': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'holder': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'issue_datetime': ('django.db.models.fields.DateTimeField', [], {}),
'issue_time': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'limit': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'log_datetime': ('django.db.models.fields.DateTimeField', [], {}),
'log_time': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'reason': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'resource': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'serial': ('django.db.models.fields.BigIntegerField', [], {}),
'source': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'null': 'True'}),
'usage_max': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'usage_min': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'})
}
}
complete_apps = ['quotaholder_app']
# encoding: utf-8
import datetime
from south.db import db
from south.v2 import SchemaMigration
from django.db import models
class Migration(SchemaMigration):
def forwards(self, orm):
# Deleting field 'Commission.issue_time'
db.delete_column('quotaholder_app_commission', 'issue_time')
# Deleting field 'ProvisionLog.log_time'
db.delete_column('quotaholder_app_provisionlog', 'log_time')
# Deleting field 'ProvisionLog.issue_time'
db.delete_column('quotaholder_app_provisionlog', 'issue_time')
def backwards(self, orm):
# User chose to not deal with backwards NULL issues for 'Commission.issue_time'
raise RuntimeError("Cannot reverse this migration. 'Commission.issue_time' and its values cannot be restored.")
# User chose to not deal with backwards NULL issues for 'ProvisionLog.log_time'
raise RuntimeError("Cannot reverse this migration. 'ProvisionLog.log_time' and its values cannot be restored.")
# User chose to not deal with backwards NULL issues for 'ProvisionLog.issue_time'
raise RuntimeError("Cannot reverse this migration. 'ProvisionLog.issue_time' and its values cannot be restored.")
models = {
'quotaholder_app.commission': {
'Meta': {'object_name': 'Commission'},
'clientkey': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'issue_datetime': ('django.db.models.fields.DateTimeField', [], {}),
'name': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '4096'}),
'serial': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
},
'quotaholder_app.holding': {
'Meta': {'unique_together': "(('holder', 'source', 'resource'),)", 'object_name': 'Holding'},
'holder': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'db_index': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'limit': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'resource': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'source': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'null': 'True'}),
'usage_max': ('snf_django.lib.db.fields.IntDecimalField', [], {'default': '0', 'max_digits': '38', 'decimal_places': '0'}),
'usage_min': ('snf_django.lib.db.fields.IntDecimalField', [], {'default': '0', 'max_digits': '38', 'decimal_places': '0'})
},
'quotaholder_app.provision': {
'Meta': {'object_name': 'Provision'},
'holder': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'db_index': 'True'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'quantity': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'resource': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'serial': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'provisions'", 'to': "orm['quotaholder_app.Commission']"}),
'source': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'null': 'True'})
},
'quotaholder_app.provisionlog': {
'Meta': {'object_name': 'ProvisionLog'},
'delta_quantity': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'holder': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
'issue_datetime': ('django.db.models.fields.DateTimeField', [], {}),
'limit': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'log_datetime': ('django.db.models.fields.DateTimeField', [], {}),
'name': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'reason': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'resource': ('django.db.models.fields.CharField', [], {'max_length': '4096'}),
'serial': ('django.db.models.fields.BigIntegerField', [], {}),
'source': ('django.db.models.fields.CharField', [], {'max_length': '4096', 'null': 'True'}),
'usage_max': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'}),
'usage_min': ('snf_django.lib.db.fields.IntDecimalField', [], {'max_digits': '38', 'decimal_places': '0'})
}
}
complete_apps = ['quotaholder_app']
......@@ -31,10 +31,9 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from datetime import datetime
from snf_django.lib.db.fields import intDecimalField
from django.db.models import (Model, BigIntegerField, CharField,
from django.db.models import (Model, BigIntegerField, CharField, DateTimeField,
ForeignKey, AutoField)
from snf_django.lib.db.managers import ForUpdateManager
......@@ -55,16 +54,12 @@ class Holding(Model):
unique_together = (('holder', 'source', 'resource'),)
def now():
return datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:24]
class Commission(Model):
serial = AutoField(primary_key=True)
name = CharField(max_length=4096, default="")
clientkey = CharField(max_length=4096, null=False)
issue_time = CharField(max_length=24)
issue_datetime = DateTimeField()
objects = ForUpdateManager()
......@@ -97,8 +92,8 @@ class ProvisionLog(Model):
serial = BigIntegerField()
name = CharField(max_length=4096)
issue_time = CharField(max_length=4096)
log_time = CharField(max_length=4096)
issue_datetime = DateTimeField()
log_datetime = DateTimeField()
holder = CharField(max_length=4096)
source = CharField(max_length=4096, null=True)
resource = CharField(max_length=4096)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment