Revision 9c01d9d1

b/snf-astakos-app/astakos/im/models.py
41 41
from django.db import models
42 42
from django.contrib.auth.models import User, UserManager
43 43

  
44
from astakos.im.settings import DEFAULT_USER_LEVEL, INVITATIONS_PER_LEVEL, AUTH_TOKEN_DURATION
44
from astakos.im.settings import DEFAULT_USER_LEVEL, INVITATIONS_PER_LEVEL, AUTH_TOKEN_DURATION, BILLING_FIELDS, QUEUE_CONNECTION
45
from synnefo.lib.queue import exchange_connect, exchange_send, exchange_close, Receipt
46

  
47
QUEUE_CLIENT_ID = 3 # Astakos.
45 48

  
46 49
class AstakosUser(User):
47 50
    """
......
92 95
    def save(self, update_timestamps=True, **kwargs):
93 96
        if update_timestamps:
94 97
            if not self.id:
95
                # set username
96
                while not self.username:
97
                    username =  uuid.uuid4().hex[:30]
98
                    try:
99
                        AstakosUser.objects.get(username = username)
100
                    except AstakosUser.DoesNotExist, e:
101
                        self.username = username
102
                self.is_active = False
103
                if not self.provider:
104
                    self.provider = 'local'
105 98
                self.date_joined = datetime.now()
106 99
            self.updated = datetime.now()
100
        if not self.id:
101
            # set username
102
            while not self.username:
103
                username =  uuid.uuid4().hex[:30]
104
                try:
105
                    AstakosUser.objects.get(username = username)
106
                except AstakosUser.DoesNotExist, e:
107
                    self.username = username
108
            self.is_active = False
109
            if not self.provider:
110
                self.provider = 'local'
111
        report_user_event(self)
107 112
        super(AstakosUser, self).save(**kwargs)
108 113
    
109 114
    def renew_token(self):
......
144 149
        
145 150
    def __unicode__(self):
146 151
        return '%s -> %s [%d]' % (self.inviter, self.username, self.code)
152

  
153
def report_user_event(user):
154
    def should_send(user):
155
        # report event incase of new user instance
156
        # or if specific fields are modified
157
        if not user.id:
158
            return True
159
        db_instance = AstakosUser.objects.get(id = user.id)
160
        for f in BILLING_FIELDS:
161
            if (db_instance.__getattribute__(f) != user.__getattribute__(f)):
162
                return True
163
        return False
164
    
165
    if QUEUE_CONNECTION and should_send(user):
166
        l = [[elem, str(user.__getattribute__(elem))] for elem in BILLING_FIELDS]
167
        details = dict(l)
168
        body = Receipt(QUEUE_CLIENT_ID, user.email, '', 0, details).format()
169
        msgsubtype = 'create' if not user.id else 'modify'
170
        exchange = '%s.%s.#' %(QUEUE_CONNECTION, msgsubtype)
171
        conn = exchange_connect(exchange)
172
        routing_key = exchange.replace('#', body['id'])
173
        exchange_send(conn, routing_key, body)
174
        exchange_close(conn)
b/snf-astakos-app/astakos/im/settings.py
58 58
RECAPTCHA_PRIVATE_KEY = getattr(settings, 'ASTAKOS_RECAPTCHA_PRIVATE_KEY', '')
59 59
RECAPTCHA_OPTIONS = getattr(settings, 'ASTAKOS_RECAPTCHA_OPTIONS', {'theme': 'white'})
60 60

  
61
# set AstakosUser fields to propagate in the billing system
62
BILLING_FIELDS = getattr(settings, 'ASTAKOS_BILLING_FIELDS', ['id', 'is_active', 'provider', 'third_party_identifier'])
63

  
64
# Queue for billing.
65
QUEUE_CONNECTION = getattr(settings, 'ASTAKOS_QUEUE_CONNECTION', None) # Example: 'rabbitmq://guest:guest@localhost:5672/astakos.userEvent'

Also available in: Unified diff