Commit 9c01d9d1 authored by Sofia Papagiannaki's avatar Sofia Papagiannaki
Browse files

integration with aquarium progress I

Refs: #1824 
parent 9cbf88dc
...@@ -41,7 +41,10 @@ from base64 import b64encode ...@@ -41,7 +41,10 @@ from base64 import b64encode
from django.db import models from django.db import models
from django.contrib.auth.models import User, UserManager from django.contrib.auth.models import User, UserManager
from astakos.im.settings import DEFAULT_USER_LEVEL, INVITATIONS_PER_LEVEL, AUTH_TOKEN_DURATION from astakos.im.settings import DEFAULT_USER_LEVEL, INVITATIONS_PER_LEVEL, AUTH_TOKEN_DURATION, BILLING_FIELDS, QUEUE_CONNECTION
from synnefo.lib.queue import exchange_connect, exchange_send, exchange_close, Receipt
QUEUE_CLIENT_ID = 3 # Astakos.
class AstakosUser(User): class AstakosUser(User):
""" """
...@@ -92,18 +95,20 @@ class AstakosUser(User): ...@@ -92,18 +95,20 @@ class AstakosUser(User):
def save(self, update_timestamps=True, **kwargs): def save(self, update_timestamps=True, **kwargs):
if update_timestamps: if update_timestamps:
if not self.id: if not self.id:
# set username
while not self.username:
username = uuid.uuid4().hex[:30]
try:
AstakosUser.objects.get(username = username)
except AstakosUser.DoesNotExist, e:
self.username = username
self.is_active = False
if not self.provider:
self.provider = 'local'
self.date_joined = datetime.now() self.date_joined = datetime.now()
self.updated = datetime.now() self.updated = datetime.now()
if not self.id:
# set username
while not self.username:
username = uuid.uuid4().hex[:30]
try:
AstakosUser.objects.get(username = username)
except AstakosUser.DoesNotExist, e:
self.username = username
self.is_active = False
if not self.provider:
self.provider = 'local'
report_user_event(self)
super(AstakosUser, self).save(**kwargs) super(AstakosUser, self).save(**kwargs)
def renew_token(self): def renew_token(self):
...@@ -144,3 +149,26 @@ class Invitation(models.Model): ...@@ -144,3 +149,26 @@ class Invitation(models.Model):
def __unicode__(self): def __unicode__(self):
return '%s -> %s [%d]' % (self.inviter, self.username, self.code) return '%s -> %s [%d]' % (self.inviter, self.username, self.code)
def report_user_event(user):
def should_send(user):
# report event incase of new user instance
# or if specific fields are modified
if not user.id:
return True
db_instance = AstakosUser.objects.get(id = user.id)
for f in BILLING_FIELDS:
if (db_instance.__getattribute__(f) != user.__getattribute__(f)):
return True
return False
if QUEUE_CONNECTION and should_send(user):
l = [[elem, str(user.__getattribute__(elem))] for elem in BILLING_FIELDS]
details = dict(l)
body = Receipt(QUEUE_CLIENT_ID, user.email, '', 0, details).format()
msgsubtype = 'create' if not user.id else 'modify'
exchange = '%s.%s.#' %(QUEUE_CONNECTION, msgsubtype)
conn = exchange_connect(exchange)
routing_key = exchange.replace('#', body['id'])
exchange_send(conn, routing_key, body)
exchange_close(conn)
\ No newline at end of file
...@@ -58,3 +58,8 @@ RECAPTCHA_PUBLIC_KEY = getattr(settings, 'ASTAKOS_RECAPTCHA_PUBLIC_KEY', '') ...@@ -58,3 +58,8 @@ RECAPTCHA_PUBLIC_KEY = getattr(settings, 'ASTAKOS_RECAPTCHA_PUBLIC_KEY', '')
RECAPTCHA_PRIVATE_KEY = getattr(settings, 'ASTAKOS_RECAPTCHA_PRIVATE_KEY', '') RECAPTCHA_PRIVATE_KEY = getattr(settings, 'ASTAKOS_RECAPTCHA_PRIVATE_KEY', '')
RECAPTCHA_OPTIONS = getattr(settings, 'ASTAKOS_RECAPTCHA_OPTIONS', {'theme': 'white'}) RECAPTCHA_OPTIONS = getattr(settings, 'ASTAKOS_RECAPTCHA_OPTIONS', {'theme': 'white'})
# set AstakosUser fields to propagate in the billing system
BILLING_FIELDS = getattr(settings, 'ASTAKOS_BILLING_FIELDS', ['id', 'is_active', 'provider', 'third_party_identifier'])
# Queue for billing.
QUEUE_CONNECTION = getattr(settings, 'ASTAKOS_QUEUE_CONNECTION', None) # Example: 'rabbitmq://guest:guest@localhost:5672/astakos.userEvent'
\ No newline at end of file
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