0

I am using django-nonrel for my project on GAE. My requirement is that in my application at a time only one user should login with the given username. I tried to implement the following suggested approaches: Allow only one concurrent login per user in django app and How can I detect multiple logins into a Django web application from different locations? But the problem is that both of the approaches working on the development server but didn't work on google app engine. So I switched to django-signals as my alternate approach. I created one post_login signal which will store the username for every login user in a table Visitor in database. On every logout,other signal post_logout will remove the user from this table.The part of codes are as:

#signals.py
post_login = django.dispatch.Signal(providing_args=['request', 'user'])
post_logout = django.dispatch.Signal(providing_args=['request', 'user'])
#models.py
def login_handler(sender,user, **kwargs):
    try:
        result=Visitor.objects.get(user=user)
        print "You already have login with your name"
    except:
        visitor=Visitor()
        visitor.user=user
        visitor.save()
post_login.connect(login_handler)


def logout_handler(sender,user, **kwargs):
    try:
        result=Visitor.objects.get(user=user)
        result.delete()
    except:
        return False
post_logout.connect(logout_handler)

#django.contrib.auth.__init.py__
def login(request):
 :
 user_logged_in.send(sender=user.__class__, request=request, user=user)
  post_login.send(sender=None,request=request, user=user)

def logout(request):
:
user_logged_out.send(sender=user.__class__, request=request, user=user)
post_logout.send(sender=None,request=request, user=user)

Please note that I am getting the following error while running my application on google app engine. Error: Server Error The server encountered an error and could not complete your request.

Also I am not able to login into Admin part of the application. Please help me to find right approach to implement this requirement or let me know where I am doing wrong. Thanks for your patience for reading this huge problem description :-)

Community
  • 1
  • 1
SRC
  • 590
  • 6
  • 22
  • "Error: Server Error" is not an error message; it's the default web page returned for a 500 error. Check the logs for your application and paste a traceback, or at least the actual error message. – Wooble Feb 20 '11 at 14:08
  • @Wooble:Thanks Wooble for your response..actually I don't know where to check logs or actual error message on google app engine..what do I need to do for this ? – SRC Feb 20 '11 at 18:07
  • @Wooble: I hope I need to use google analytics..I am checking it and update you soon.Thanks – SRC Feb 20 '11 at 18:14
  • @SRC: you can view the logs at appengine.google.com – Wooble Feb 21 '11 at 02:02
  • @Wooble:Yes,I checked the logs and corrected the code. But basic question is do django-nonrel supports django signals on GAE since for the login event it is not creating Visitor table (as per my code in models.py.Also same table is creating on development server) ? By the way thanks for pointing me towards appengine logs. I didn't aware about it :-) – SRC Feb 21 '11 at 07:01
  • I wonder if no one is aware about it.Friends at least let me know if I am writing my signal codes on proper place. – SRC Feb 21 '11 at 14:24

1 Answers1

2

1.

You should not be editing the django framework like you are doing. Don't touch the files inside django.contrib.auth

If you wish to send a signal after someone is logged in, then send the signal in your view where you log the person in

2.

Not sure what your actual error is because you are not displaying it (if this is a dev environment set DEBUG = True to get a better stack trace) But by lookingat you code, you are not grabbing the arguments correctly in the signal handler. It should look more like this:

def login_handler(sender, **kwargs):
    try:
        user = kwargs['user']
        request = kwargs['request']
        result=Visitor.objects.get(user=user)
        print "You already have login with your name"
    except:
        visitor=Visitor()
        visitor.user=user
        visitor.save()
post_login.connect(login_handler)
Community
  • 1
  • 1
MattoTodd
  • 14,467
  • 16
  • 59
  • 76
  • Thanks again MattoTodd for your reply..Actually I am not getting any error on the GAE but when I am accessing the values of Visitor table, I am not able to print anything. Its giving me message 'Status: 200 OK Vary: Cookie Content-Type: text/html; charset=utf-8 ' instead of the result. Note that I can see the table is created from Database Viewer on appengine.google.com – SRC Feb 24 '11 at 09:51
  • I come to know that this message I am getting due to printing the result before making Http response.So,rendering the template and using result there, giving expected output. By the way I have made certain changes in my code to include signal.send in my views as per your suggestion. Thanks a lot and good wishes!! – SRC Feb 24 '11 at 10:48