add creditevent listener to set user ``has_credits`` flag and pass the information...
authorSofia Papagiannaki <papagian@gmail.com>
Mon, 12 Mar 2012 12:00:10 +0000 (14:00 +0200)
committerSofia Papagiannaki <papagian@gmail.com>
Mon, 12 Mar 2012 12:00:10 +0000 (14:00 +0200)
Refs: #1824

snf-astakos-app/astakos/im/api.py
snf-astakos-app/astakos/im/functions.py
snf-astakos-app/astakos/im/migrations/0005_auto__add_field_astakosuser_has_credits.py [new file with mode: 0644]
snf-astakos-app/astakos/im/models.py
snf-astakos-app/astakos/im/queue/__init__.py [new file with mode: 0644]
snf-astakos-app/astakos/im/queue/listener.py [new file with mode: 0644]
snf-astakos-app/astakos/im/queue/userevent.py [new file with mode: 0644]
snf-astakos-app/astakos/im/settings.py

index 893b9ac..a40d4b2 100644 (file)
@@ -92,7 +92,8 @@ def authenticate(request):
                      'uniq':user.email,
                      'auth_token':user.auth_token,
                      'auth_token_created':user.auth_token_created.isoformat(),
-                     'auth_token_expires':user.auth_token_expires.isoformat()}
+                     'auth_token_expires':user.auth_token_expires.isoformat(),
+                     'has_credits':user.has_credits}
         response.content = json.dumps(user_info)
         response['Content-Type'] = 'application/json; charset=UTF-8'
         response['Content-Length'] = len(response.content)
index 1b84b26..078d11a 100644 (file)
@@ -41,7 +41,7 @@ from urlparse import urljoin
 from random import randint
 
 from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, SITENAME, BASEURL
-from astakos.im.models import Invitation
+from astakos.im.models import Invitation, AstakosUser
 
 logger = logging.getLogger(__name__)
 
@@ -98,3 +98,11 @@ def invite(inviter, username, realname):
     logger.info('Sent invitation %s', invitation)
     inviter.invitations = max(0, inviter.invitations - 1)
     inviter.save()
+
+def set_user_credibility(email, has_credits):
+    try:
+        user = AstakosUser.objects.get(email=email)
+        user.has_credits = has_credits
+        user.save()
+    except AstakosUser.DoesNotExist, e:
+        logger.exception(e)
\ No newline at end of file
diff --git a/snf-astakos-app/astakos/im/migrations/0005_auto__add_field_astakosuser_has_credits.py b/snf-astakos-app/astakos/im/migrations/0005_auto__add_field_astakosuser_has_credits.py
new file mode 100644 (file)
index 0000000..8f15e7d
--- /dev/null
@@ -0,0 +1,89 @@
+# 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 'AstakosUser.has_credits'
+        db.add_column('im_astakosuser', 'has_credits', self.gf('django.db.models.fields.BooleanField')(default=False), keep_default=False)
+
+
+    def backwards(self, orm):
+        
+        # Deleting field 'AstakosUser.has_credits'
+        db.delete_column('im_astakosuser', 'has_credits')
+
+
+    models = {
+        'auth.group': {
+            'Meta': {'object_name': 'Group'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'auth.permission': {
+            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        },
+        'auth.user': {
+            'Meta': {'object_name': 'User'},
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+        },
+        'contenttypes.contenttype': {
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        },
+        'im.astakosuser': {
+            'Meta': {'object_name': 'AstakosUser', '_ormbases': ['auth.User']},
+            'affiliation': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+            'auth_token': ('django.db.models.fields.CharField', [], {'max_length': '32', 'null': 'True', 'blank': 'True'}),
+            'auth_token_created': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
+            'auth_token_expires': ('django.db.models.fields.DateTimeField', [], {'null': 'True'}),
+            'email_verified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'has_credits': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'invitations': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+            'is_verified': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'level': ('django.db.models.fields.IntegerField', [], {'default': '4'}),
+            'provider': ('django.db.models.fields.CharField', [], {'max_length': '255', 'blank': 'True'}),
+            'third_party_identifier': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+            'updated': ('django.db.models.fields.DateTimeField', [], {}),
+            'user_ptr': ('django.db.models.fields.related.OneToOneField', [], {'to': "orm['auth.User']", 'unique': 'True', 'primary_key': 'True'})
+        },
+        'im.invitation': {
+            'Meta': {'object_name': 'Invitation'},
+            'accepted': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+            'code': ('django.db.models.fields.BigIntegerField', [], {'db_index': 'True'}),
+            'consumed': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+            'created': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'inviter': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'invitations_sent'", 'null': 'True', 'to': "orm['im.AstakosUser']"}),
+            'is_accepted': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'is_consumed': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'realname': ('django.db.models.fields.CharField', [], {'max_length': '255'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '255'})
+        }
+    }
+
+    complete_apps = ['im']
index 4cc54d1..98f27ad 100644 (file)
@@ -42,6 +42,7 @@ from django.db import models
 from django.contrib.auth.models import User, UserManager
 
 from astakos.im.settings import DEFAULT_USER_LEVEL, INVITATIONS_PER_LEVEL, AUTH_TOKEN_DURATION, BILLING_FIELDS, QUEUE_CONNECTION
+from astakos.im.queue.userevent import UserEvent
 from synnefo.lib.queue import exchange_connect, exchange_send, exchange_close, Receipt
 
 QUEUE_CLIENT_ID = 3 # Astakos.
@@ -74,6 +75,8 @@ class AstakosUser(User):
     
     email_verified = models.BooleanField('Email verified?', default=False)
     
+    has_credits = models.BooleanField('Has credits?', default=False)
+    
     @property
     def realname(self):
         return '%s %s' %(self.first_name, self.last_name)
@@ -165,11 +168,9 @@ def report_user_event(user):
         return False
     
     if QUEUE_CONNECTION and should_send(user):
-        l = [[elem, str(user.__getattribute__(elem))] for elem in BILLING_FIELDS]
-        details = dict(l)
-        details['eventType'] = 'create' if not user.id else 'modify'
-        body = Receipt(QUEUE_CLIENT_ID, user.email, '', 0, details).format()
+        eventType = 'create' if not user.id else 'modify'
+        body = UserEvent(QUEUE_CLIENT_ID, user, eventType, {}).format()
         conn = exchange_connect(QUEUE_CONNECTION)
-        routing_key = QUEUE_CONNECTION.replace('#', body['id'])
+        routing_key = QUEUE_CONNECTION.replace('*', 'user')
         exchange_send(conn, routing_key, body)
-        exchange_close(conn)
+        exchange_close(conn)
\ No newline at end of file
diff --git a/snf-astakos-app/astakos/im/queue/__init__.py b/snf-astakos-app/astakos/im/queue/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/snf-astakos-app/astakos/im/queue/listener.py b/snf-astakos-app/astakos/im/queue/listener.py
new file mode 100644 (file)
index 0000000..3416ead
--- /dev/null
@@ -0,0 +1,47 @@
+# Copyright 2011-2012 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+import logging
+import json
+
+from astakos.im.functions import set_user_credibility
+
+logger = logging.getLogger(__name__)
+
+def on_creditevent(msg):
+    try:
+        email = msg.get('userid')
+        has_credits = True if msg.get('status') == 'on' else False
+        set_user_credibility(email, has_credits)
+    except Exception, e:
+        logger.exception(e)
\ No newline at end of file
diff --git a/snf-astakos-app/astakos/im/queue/userevent.py b/snf-astakos-app/astakos/im/queue/userevent.py
new file mode 100644 (file)
index 0000000..349a3b1
--- /dev/null
@@ -0,0 +1,57 @@
+# Copyright 2011-2012 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.
+
+import json
+
+from time import time
+from hashlib import sha1
+from random import random
+
+class UserEvent(object):
+    def __init__(self, client, user, eventType, details={}):
+        self.eventVersion = '1.0'
+        self.occurredMillis = int(time() * 1000)
+        self.receivedMillis = self.occurredMillis
+        self.clientID = client
+        self.userID = user.email
+        self.is_active = user.is_active
+        self.role = 'default'
+        self.eventType = eventType
+        self.details = details
+        hash = sha1()
+        hash.update(json.dumps([client, self.userID, self.is_active, self.role,
+                                self.eventType, self.details, random()]))
+        self.id = hash.hexdigest()
+    
+    def format(self):
+        return self.__dict__
\ No newline at end of file
index 26fc370..e9082af 100644 (file)
@@ -62,7 +62,7 @@ RECAPTCHA_OPTIONS = getattr(settings, 'ASTAKOS_RECAPTCHA_OPTIONS', {'theme': 'wh
 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.#'
+QUEUE_CONNECTION = getattr(settings, 'ASTAKOS_QUEUE_CONNECTION', None) # Example: 'rabbitmq://guest:guest@localhost:5672/astakos.*'
 
 # Set where the user should be redirected after logout
 LOGOUT_NEXT = getattr(settings, 'ASTAKOS_LOGOUT_NEXT', '')