Guest User

Untitled

a guest
Sep 14th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. how to use django's registration field(s) to custom table
  2. from django.contrib.auth.models import User
  3. from django.db.models.signals import post_save
  4.  
  5. class Token(models.Model):
  6. user = models.OneToOneField(User)
  7. api_key=models.CharField(max_length=50)
  8.  
  9.  
  10. def create_user_token(sender, instance, created, **kwargs):
  11. if created:
  12. profile, created = Token.objects.get_or_create(user=instance)
  13.  
  14. #this will definitely not work because i have no field name api_key on form
  15. post_save.connect(create_user_token, sender=User)
  16.  
  17. #password=posted from django-registration form as i have used django-registration
  18. digest = hmac.new('Secret', password, hashlib.sha256).hexdigest()
  19.  
  20. from django import forms
  21. from registration.forms import RegistrationForm
  22. from django.utils.translation import ugettext_lazy as _
  23. from registration.models import RegistrationProfile
  24.  
  25. attrs_dict = { 'class': 'required' }
  26.  
  27. class RegistrationFormEx(RegistrationForm):
  28. cell_phone = forms.CharField(widget=forms.TextInput(attrs=attrs_dict))
  29.  
  30. import hashlib
  31. import hmac
  32. from django.db import models
  33. from django.contrib.auth.models import User
  34. from registration.signals import user_registered
  35. from userInfo.forms import RegistrationFormEx
  36.  
  37.  
  38. class ExProfile(models.Model):
  39. user = models.ForeignKey(User, unique=True)
  40. cell_phone = models.CharField(max_length=200, blank=True)
  41. api_key= models.CharField(max_length=200, blank=True)
  42.  
  43. def user_created(sender, user, request, **kwargs):
  44. form = RegistrationFormEx(data=request.POST)
  45. digest = hmac.new(str(request.POST['password1']), str(request.POST['username']), hashlib.sha256).hexdigest()
  46. new_user = User.objects.get(username=request.POST['username'])
  47. //here I have added api_key hash algo, you can change it
  48. new_profile = ExProfile(user=new_user,cell_phone=request.POST['cell_phone'],api_key=digest)
  49. new_profile.save()
  50. return new_user
  51.  
  52. user_registered.connect(user_created)
  53.  
  54. from django.conf.urls import patterns, include, url
  55. import registration.backends.default.urls as regUrls
  56. from registration.views import register
  57. from userInfo.forms import RegistrationFormEx
  58.  
  59.  
  60. urlpatterns = patterns('',
  61. url(r'^accounts/register/$', register, {'backend': 'registration.backends.default.DefaultBackend','form_class': RegistrationFormEx}, name='registration_register'),
  62. (r'^accounts/', include('registration.backends.default.urls'))
  63.  
  64. )
  65.  
  66. profile = Token.objects.get(user=request.user)
  67. #set a new api key
  68. profile.api_key =hashlib.sha1(str(random.random()) +
  69. 'app_name' +
  70. str(time.time())).hexdigest()
  71. profile.save()
Add Comment
Please, Sign In to add comment