Commits

Mikhail Korobov committed 3335a40

User field in NetcashOrder model

Comments (0)

Files changed (6)

 netcash.forms.NetcashForm can be used to construct the html form. It is
 a helper form for html output and it shouldn't perform any validation.
 
+Pass all the fields to form 'initial' argument. Form also has an optional
+'user' parameter: it is the User instance the order is purchased by. If
+'user' is specified, 'm_9' (cardholder email address) will be filled
+automatically if it is not passed with 'initial'.
+
 Example::
 
     # views.py
             # 'm_5': 'extra param 2',
             # 'm_6': 'extra param 3',
             # 'm_9': order.user.email # cardholder email address
-        })
+        }, user=order.user)
 
         return direct_to_template(request, 'pay_with_netcash.html', {'form': form})
 
 
 NetcashForm has a 'target' attribute with Netcash URL.
 
-Please note that it's up to you to provide Order model with any fields you want
-and to implement your order processing logic. Order handling should be
-performed in ``netcash.signals.data`` signal handler.
+Please note that it's up to you to implement the order processing logic.
+Order handling should be performed in ``netcash.signals.data`` signal handler.
 
 ``netcash.signals.data`` signal
 -------------------------------
 
-This signal is sent when Netcash posts data to Data URL.
+When Netcash posts data to Data URL ``netcash.signals.data`` signal is sent.
+This signal won't be sent for suspicious data (when request is coming from
+untrusted ip or form validation fails).
+
 Signal subscribers will get an 'order' argument with ``NetcashOrder`` instance.
 
 Example::
     m_10 = forms.CharField(max_length=100, required=False)
 
     def __init__(self, *args, **kwargs):
+        user = kwargs.pop('user', None)
+        if user and 'm_9' not in kwargs['initial']:
+            kwargs['initial']['m_9'] = user.email
+
         super(NetcashForm, self).__init__(*args, **kwargs)
 
         # new order reference number is issued each time form is instantiated
-        self.order = NetcashOrder.objects.create()
+        self.order = NetcashOrder.objects.create(user=user)
         self.fields['p2'].initial = self.order.pk
 
 
 
     class Meta:
         model = NetcashOrder
-        exclude = ['created_at', 'updated_at', 'trusted', 'gateway',
-                   'request_ip', 'debug_info']
+        fields = ['Reference', 'TransactionAccepted', 'Amount', 'Reason',
+                   'RETC', 'Extra1', 'Extra2', 'Extra3',]

netcash/migrations/0010_add_user.py

+# 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 'NetcashOrder.user'
+        db.add_column('netcash_netcashorder', 'user', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['auth.User'], null=True, blank=True), keep_default=False)
+
+
+    def backwards(self, orm):
+        
+        # Deleting field 'NetcashOrder.user'
+        db.delete_column('netcash_netcashorder', 'user_id')
+
+
+    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'})
+        },
+        'netcash.netcashgateway': {
+            'Meta': {'object_name': 'NetcashGateway'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'default': "'gateway'", 'max_length': '50'}),
+            'netcash_ip': ('django.db.models.fields.IPAddressField', [], {'max_length': '15', 'null': 'True', 'blank': 'True'}),
+            'secret': ('django.db.models.fields.CharField', [], {'max_length': '40'})
+        },
+        'netcash.netcashorder': {
+            'Amount': ('django.db.models.fields.DecimalField', [], {'null': 'True', 'max_digits': '12', 'decimal_places': '2', 'blank': 'True'}),
+            'CardHolderIpAddr': ('django.db.models.fields.IPAddressField', [], {'max_length': '15', 'null': 'True', 'blank': 'True'}),
+            'Extra1': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True', 'blank': 'True'}),
+            'Extra2': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True', 'blank': 'True'}),
+            'Extra3': ('django.db.models.fields.CharField', [], {'max_length': '50', 'null': 'True', 'blank': 'True'}),
+            'Meta': {'object_name': 'NetcashOrder'},
+            'RETC': ('django.db.models.fields.CharField', [], {'max_length': '25', 'null': 'True', 'blank': 'True'}),
+            'Reason': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+            'Reference': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'TransactionAccepted': ('django.db.models.fields.NullBooleanField', [], {'default': 'None', 'null': 'True', 'blank': 'True'}),
+            'created_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'debug_info': ('django.db.models.fields.CharField', [], {'max_length': '255', 'null': 'True', 'blank': 'True'}),
+            'gateway': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['netcash.NetcashGateway']", 'null': 'True', 'blank': 'True'}),
+            'request_ip': ('django.db.models.fields.IPAddressField', [], {'max_length': '15', 'null': 'True', 'blank': 'True'}),
+            'trusted': ('django.db.models.fields.NullBooleanField', [], {'default': 'None', 'null': 'True', 'blank': 'True'}),
+            'updated_at': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'user': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'})
+        }
+    }
+
+    complete_apps = ['netcash']

netcash/models.py

 from django.db import models
 from django.core.urlresolvers import reverse
 from django.contrib.sites.models import Site
+from django.contrib.auth.models import User
 
 def full_url(link):
     current_site = Site.objects.get_current()
     def save(self, *args, **kwargs):
         if not self.secret:
             self.secret = random_secret()
-
         return super(NetcashGateway, self).save(*args, **kwargs)
 
     def __unicode__(self):
     request_ip = models.IPAddressField(null=True, blank=True)
     debug_info = models.CharField(max_length=255, null=True, blank=True)
     trusted = models.NullBooleanField(default=None)
+    user = models.ForeignKey(User, null=True, blank=True)
 
     def save(self, *args, **kwargs):
         self.updated_at = datetime.now()
 from netcash.models import NetcashGateway, NetcashOrder
 from netcash.forms import NetcashForm
 import netcash.signals
+from django.contrib.auth.models import User
 
 class NetcashTest(TestCase):
 
         self.assertEqual(NetcashOrder.objects.count(), 0)
 
     def test_data_request(self):
+        user = User.objects.create_user('kmike', 'example@example.com')
+
         # signal handler
         def data_handler(sender, order, **kwargs):
             data_handler.called = True
         form = NetcashForm(initial={
             'p3': 'description',
             'p4': 100,
-        })
+        }, user=user)
         reference = str(form.order.pk)
         self.assertEqual(form.order.trusted, None)
+        self.assertEqual(form.initial['m_9'], user.email)
 
         self.assertFalse(data_handler.called)
 
         self.assertEqual(order.gateway, self.gateway)
         self.assertEqual(order.request_ip, '127.0.0.1')
         self.assertEqual(order.trusted, True)
+        self.assertEqual(order.user, user)
 
         self.assertTrue(data_handler.called)
         self.assertEqual(data_handler.order, order)
 
 setup(
     name='django-netcash',
-    version='0.2.2',
+    version='0.3.0',
     author='Mikhail Korobov',
     author_email='kmike84@gmail.com',
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.