So i am stuck at a point in my project. I am trying to create users and I have extended User model using the OneToOnefield method as recommended in Documentation to create a user profile. There is no problem in creation of the user as all the details are correctly stored in both the auth_user and appname_userprofile table.
The problem I get is when I try to login with any user stored in the auth_user table. Except one case( which i created before creating the userProfile model). So in my auth_user Table the pass for this one case is encoded but for the remaining cases it's plain text.
Here is my function handling the view -
def login_view(request):
if request.method == 'POST':
print(request.body)
username = request.POST.get('id_username')
password = request.POST.get('id_password')
print(username, password)
try:
import pdb; pdb.set_trace()
user = authenticate(username=username, password=password)
print('This is ',user)
if user is not None:
login(request,user)
else:
return redirect('fileupload')
print('This is ',user).... some more code
Now when i pass the credentials for this one case and try to authenticate. the login works(except my code gets stuck at some later point because this user doesn't have any profile).
When i pass the credentials for user created after creating the UserProfile model. The
authenticate(username=username, password=password)
function returns
None
I have included this in my settings.py -
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
This is my model -
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
contact_no = models.CharField(max_length=10, null=True)
# email = models.EmailField()
department = models.CharField(max_length=25, null=True)
status = models.IntegerField(null=True)
industry_segment = models.CharField(max_length=50, null=True)
created_at = models.DateTimeField(auto_now_add=True)
created_by = models.IntegerField(null=True)
updated_at = models.DateTimeField(auto_now=True)
updated_by = models.IntegerField(null=True)
#receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
#receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.userprofile.save()
This is how i am saving the users -
def user_view(request):
print(request.user)
if request.is_ajax():
print(request.user)
data = json.load(request)
u_name = data['txtfirstName'] + " " + data['txtlastName']
user=User(username=u_name,password='test123',email=data['txtemailId'])
user.save()
# userprofile= UserProfile.objects.create(user=request.user)
user.userprofile.contact_no = data['txtcontactNo']
# user.userprofile.email = data['txtemailId']
user.userprofile.department = data['txtdeptvalue']
user.userprofile.status = data['txtstatusvalue']
user.userprofile.industry_segment = 'Software'
user.userprofile.created_at = datetime.datetime.now()
user.userprofile.created_by = request.user.id
user.userprofile.updated_at = datetime.datetime.now()
user.userprofile.updated_by = request.user.id
user.save()
ignore the formatting of this code... but it is working
So can anyone help me with the authentication problem ?
You must not create users like that. The password will not be hashed, so will never match in authenticate. You must always use the create_user manager method.
user = User.objects.create_user(username=u_name, password='test123', email=data['txtemailId'])
Related
I'm combining django-invitations with django-allauth for user invitation and signup.
I'd like the Administrator (when creating an invitation through the Django Admin) to provide extra data (here a foreign key to Patient object). This is archieved by adding an extra field to the (custom) invitation model:
class PatientInvitation (AbstractBaseInvitation):
email = models.EmailField(unique=True, verbose_name=_('e-mail address'),
max_length=app_settings.EMAIL_MAX_LENGTH)
created = models.DateTimeField(verbose_name=_('created'),
default=timezone.now)
patient = models.ForeignKey(Patient, blank=True, null=True, on_delete=models.CASCADE)
#classmethod
def create(cls, email, inviter=None, patient=None, **kwargs):
key = get_random_string(64).lower()
instance = cls._default_manager.create(
email=email,
key=key,
inviter=inviter,
patient=patient,
**kwargs)
return instance
def key_expired(self):
expiration_date = (
self.sent + datetime.timedelta(
days=app_settings.INVITATION_EXPIRY))
return expiration_date <= timezone.now()
def send_invitation(self, request, **kwargs):
current_site = kwargs.pop('site', Site.objects.get_current())
invite_url = reverse('invitations:accept-invite',
args=[self.key])
invite_url = request.build_absolute_uri(invite_url)
ctx = kwargs
ctx.update({
'invite_url': invite_url,
'site_name': current_site.name,
'email': self.email,
'key': self.key,
'inviter': self.inviter,
})
When the invited user signs up, I would like this data to end up in the Custom user model:
class customUser(AbstractUser):
username_validator = MyValidator()
is_patient = models.BooleanField(default=False)
patient = models.ForeignKey(Patient, null=True, blank=True, on_delete=models.CASCADE)
username = models.CharField(
_('username'),
max_length=150,
unique=True,
)
I've looked into the signals to pass the data, but couldn't find how exactly to do this.
Another option seems to add the PK of the foreign key to a hidden field on the signup form (this seems unsafe though).
I'm a bit stuck on this one, so if anyone could point me in the right direction, It would be greatly appreciated :)
Regards,
Joris
Use allauth's signals instead; you'll have much more options and you can still accomplish exactly what you want.
I would create signals.py in your app directory.
Register your signals file under your apps.py like so:
from django.apps import AppConfig
class AccountsConfig(AppConfig):
name = 'app_name'
def ready(self): ###<-----------
import app_name.signals ###<-----------
Use the user_signed_up signal to update the user with the invitation's data:
signals.py
from allauth.account.signals import user_signed_up
from invitations.utils import get_invitation_model
def user_signed_up(request, user, **kwargs):
try:
Invitation = get_invitation_model() ### Get the Invitation model
invite = Invitation.objects.get(email=user.email) ### Grab the Invitation instance
user.patient = invite.patient ### Pass your invitation's patient to the related user
user.save()
except Invitation.DoesNotExist:
print("this was probably not an invited user.")
class Setting(models.Model):
id=models.AutoField(primary_key=True, unique=True)
ldap_server = models.CharField(max_length=100, default='ldap://yourDomain.in')
ldap_server_username = models.CharField(max_length=100, null=True)
ldap_server_password = models.CharField(max_length=100, null=True)
def save(self, *args, **kwargs):
ldap_server=self.ldap_server
ldap_server_username = self.ldap_server_username
ldap_server_password = self.ldap_server_password
try:
l = ldap.initialize(ldap_server)
l.protocol_version = ldap.VERSION3
l.set_option(ldap.OPT_REFERRALS, 0)
l.simple_bind_s(ldap_server_username, ldap_server_password)
super(Setting, self).save(*args, **kwargs)
except:
messages.error(request, "You have logged in..!")
here I faced the error in
messages.error(request, "You have logged in..!")
I can't use
messages.error(request, "You have logged in..!")
Anybody know alternative way to show error
message.
Try this in admin.py:
#admin.py
from models import Setting
from django.contrib import admin
from django import forms
class SettingForm(forms.ModelForm):
class Meta:
model = Setting
def clean(self):
ldap_server = self.cleaned_data.get('ldap_server')
ldap_server_username = self.cleaned_data.get('ldap_server_username')
ldap_server_password = self.cleaned_data.get('ldap_server_password')
# your ldap logic here
if your_condition:
raise form.ValidationErro('You have logged in..!')
return self.cleaned_data
class SettingAdmin(admin.ModelAdmin):
form = SettingForm
list_display = ('ldap_server', 'ldap_server_username', 'ldap_server_password')
admin.site.register(Setting, SettingAdmin)
Remove save() method from your model.
You can move SettingForm in forms.py file and then import it in admin.py
I hope this will help
I want to create a messaging function in ma django app. User should be able to write other users a textmessage.
models.py
from django.contrib.auth.models import User
class Message(models.Model):
recipient = models.ForeignKey(User, null=True)
contentDescription = models.CharField(max_length=1000, null=True)
By default, with no forms.py entry I get a selection, which will be unuseful with many users. I want the message sender to type in the user name, or in the first step the user id (which I could resolve with ajax from the name) .
Integer
But with forms.py
recipient = forms.IntegerField( widget=forms.NumberInput , required=False,)
I get:
Cannot assign "11": "Transport.recipient" must be a "User" instance.
ChoiceField and NumberInput
with:
recipient = forms.ChoiceField( widget=forms.NumberInput, required=False,)
I get the error message "not valid"
Is it possible to write the foreignkey 'manually' at all?
Try this:
recipient = forms.ModelChoiceField(queryset=User.objects.all(), widget=forms.Select, required=False)
considering your
models.py -
from django.contrib.auth.models import User
class Message(models.Model):
recipient = models.ManytoMany(User, null=True)
contentDescription = models.TextField()
forms.py
from .models import Message
from django import forms
from django.contrib.auth.models import User
class MailForm(forms.ModelForm):
recipient = forms.Charfield()
class Meta:
model = Message
fields = ('contentDescription',)
def clean_recipient(self):
user_list = self.cleaned_data.get('recipient')
# considering you post user_list of usernames as 'username1,username2,username3'
if user_list is not None:
user_list = user_list.split(',')
user_qs = User.objects.filter(username__in=userlist)
else:
raise forms.ValidationError('Error in this field')
return user_qs
def save(self, user_qs):
self.instance.user = user_qs
return super().save()
in views.py -
from .forms import MailForm
def your_view(request):
form = MailForm(request.POST or None)
if form.is_valid():
user_qs=form.cleaned_data.get('recipient')
form.save(user_qs)
#return render here
else:
#create your context here and return render
This is not perfect but can give you an idea how to implement. With the details you gave this is the best I can do for now.
Ive been running into a number of problem in relation to using django's custom model. This one in particular is not raising any errors. For some reason after authenticating via steam and returning to the landing page the database tables for both steamuser_user (custom user) and social_auth_usersocialauth are empty. Nothing is being saved, no errors are being displayed etc.
My custom model which is quite similar to the one on django docs official page is as follows:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from django.contrib.auth.models import BaseUserManager
# Create your models here.
class UserManager(BaseUserManager):
def create_user(self, steamid, username, password=None):
if not steamid:
msg = 'User has no Steam ID set'
raise ValueError(msg)
if not username:
msg = 'User has no name set'
raise ValueError(msg)
user = self.model(steamid=steamid,
username=username)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, steamid, username, password):
super_user = self.create_user(steamid=steamid,
username=username,
password=password)
super_user.is_staff = True
super_user.is_admin = True
super_user.is_mod = True
super_user.save(using=self._db)
return super_user
class User(AbstractBaseUser):
steamid = models.CharField(max_length=20, unique=True)
username = models.CharField(max_length=80)
email = models.EmailField(null=True,blank=True)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
is_mod = models.BooleanField(default=False)
date_joined = models.DateTimeField(auto_now_add=True)
reputation = models.IntegerField(max_length=6, default=0)
USERNAME_FIELD = 'steamid'
objects = UserManager()
def __unicode__(self):
return self.username
def get_full_name(self):
return self.steamid
def get_short_name(self):
return self.username
The settings I've used are as follows:
SOCIAL_AUTH_USER_MODEL = 'steamuser.User'
AUTH_USER_MODEL = 'steamuser.User'
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
'social.apps.django_app.context_processors.backends',
'social.apps.django_app.context_processors.login_redirect',
)
AUTHENTICATION_BACKENDS = (
'social.backends.steam.SteamOpenId',
'django.contrib.auth.backends.ModelBackend',
)
#Steam OpenAuth
SOCIAL_AUTH_STEAM_API_KEY = 'B1D7C629D093D4B72577F2F11DE4EBE2'
LOGIN_URL = '/'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/'
SOCIAL_AUTH_ENABLED_BACKENDS = (
'steam',
)
Any help would be appreciated!
EDIT
Backends steam.py
def get_user_details(self, response):
player = self.get_json(USER_INFO, params={
'key': self.setting('API_KEY'),
'steamids': self._user_id(response)
})
if len(player['response']['players']) > 0:
player = player['response']['players'][0]
details = {'steamid': player.get('steamid'),
'username': player.get('personaname'),
}
else:
details = {}
return details
EDIT 2
Well despite my logical reasoning, I just gave up and created a custom pipeline to create the new steam user as follows:
from django.contrib.auth import get_user_model
def create_steamuser(details, user=None, *args, **kwargs):
if user:
return {'is_new': False}
if not details:
return
try:
steam_user = get_user_model().objects.get(steamid=details['steamid'])
except steam_user.DoesNotExist:
get_user_model().objects.create_user(details['steamid'], details['username'])
return {
'is_new': True,
}
Now I still have the problem where social_user is not being created. I've set the social user model to use my new custom model but there must be something that I am missing.
python-social-auth won't be able to pass the steamid and date_joined parameters to your custom create_user() method in the manager. To make that possible you have three options:
Set =None to those parameters and set some default vaules for them
Override the default create_user pipeline and pass the extra parameters.
Add a custom pipeline function before create_user and fill details with steamid and date_joined, then define SOCIAL_AUTH_STEAM_USER_FIELDS = ('username', 'email', 'steamid', 'date_joined').
I'm using userena 1.2 and Django 1.4. I'm trying to create multiple user profiles but I had no luck following many post found here on SO.
I've create 2 custom Models and a form like so:
class BaseProfile(UserenaBaseProfile):
""" Default profile """
user = models.OneToOneField(User, unique=True, verbose_name = _('user'), related_name='base_profile')
#property # accessing a method as a property
def is_seller(self):
"""Find out if this is a Seller user"""
try:
self.seller
return True
except Seller.DoesNotExist:
return False
def get_profile_type(self):
"""return the profile type model"""
if self.is_seller:
return self.seller
else:
return self.customer
class Seller(BaseProfile):
company = models.CharField(_('Company'),max_length=100, null=True, blank=True,)
class Customer(BaseProfile):
favourite_snack = models.CharField( _('favourite snack'), max_length=5 )
And overridden the Signup form:
class SignupFormExtra(SignupForm):
# stuff in here
def save(self):
# stuff in here
Now the problem is inside the save method.
Based on this answer I've tried to implement a custom manager but I had no luck (I'm a django newbie). The important point that I understood is that the Save method shoud return a Django User, not a userena profile.
def save(self):
user = super(SpamSignupForm,self).save()
new_customer = Customer.objects.create_user()
return new_user
Then I've tried something like this:
def save(self):
new_user = super(SignupFormExtra, self).save()
new_user.save()
customer = Customer(profile = new_user.get_profile(), user=new_user)
customer.save()
return new_user
The get_profile() method will always (I guess) returns the base profile defined in settings.AUTH_PROFILE_MODULE.
Also it seems wrong to me that the author used a profile field in the subprofile as a OneToOne relation to the Baseprofile WHILE implementing a multi-table inheritance, why? It doesn't seem right to me.
class Customer(Baseprofile):
profile = models.OneToOneField(Baseprofile,
unique=True,
verbose_name=_('profile'),
related_name='student')
Yes, basically I've spent a full day trying to figure this out and I'm still lost.
it is better to use django-registration. install and add it to INSTALLED_APPS =(.....'registration',
now create two user in
registration/models.py
.................
class Seller(models.Model):
user=models.OneToOneField(User)
companyname= models.CharField(max_length=100,blank=True)
def __unicode__(self):
return self.user.username
class Customer(models.Model):
user=models.OneToOneField(User)
birth_date = models.DateField(null=True, blank=True)
favourite_snack = models.CharField(max_length=5)
def __unicode__(self):
return self.user.username
in registration/views.py
........
from django.contrib.auth import authenticate, login
from registration.models import Seller,Customer
from django.http import HttpResponse
def auth_login(request,utype):
temp_login='registration/%slogin.html' %utype
try:
username = request.POST['username']
password = request.POST['password']
except KeyError:
return render(request,temp_login)
user = authenticate(username=username, password=password)
if utype=='seller':
try:Seller.objects.get(user=user)
except: return render(request,temp_login,{'errors':True})
if utype=='customer':
try:Customer.objects.get(user=user)
except: return render(request,temp_login,{'errors':True})
if user.is_active:
login(request,user)
return HttpResponseRedirect('/'+request.GET['next'])#,render(request,'%s/home.html' %utype)
return render(request,temp_login,{'errors':True})
edit registration/auth_urls.py
urlpatterns = patterns('',
url(r'^login/(employer|jobseeker)/$',
auth_login,name='auth_login'),
registration/backends/default/views.py
inside class RegistrationView(BaseRegistrationView):
inside def register(self, request, **cleaned_data): add
if self.args[0]=='e': usertype=Seller()
else: usertype=Customer()
usertype.user=new_user
usertype.save()
registration/backends/default/urls.py
add these line
url(r'^register/([ej])/$',
RegistrationView.as_view(),
name='registration_register'),