Testing man
Testing man

Reputation: 741

simple way to add existing users database(non django) to django

O.K. I have a headache with this problem. I have to different sites(non django) with login option and I would like to join in it into one new website based on django.

Each of these two user databases consist of table with columns:(username, password, e-mail).

The problem is, I just can not copy it to User table in Django as we all know django is very rigid about it, so I am trying to think a way, existing users would be able to login to site as nothing has changed.

Is there any django/pythonic way to do so?

I was thinking to create an app, which would take a foreign key to User model. Within User model I would create two users (database_1, database_2), so whenever a user from database 1 would login, e.g. JohnSmith, he would be connected as database_1 user, but so would JessicaSimpson if she would be in database 1. I am just thing to create Authentication and Authorization app as system app in some way... Is this a right way thinking? Would love to hear from professionals. Thanks

in models:

from django.db import models
from django.contrib.auth.models import User, Group

# Create your models here.

class New_users(models.Model):
    new_user_id = models.ForeignKey(User, unique=False)
    username = models.CharField(max_length=25)
    password = models.CharField(max_length=25)
    email = models.CharField(max_length=25)

in views:

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.

def home(request):

         if request.method == 'POST':

         #if username and password from New_users are ok...
         login()#User where id is the same blah blah.... 

Upvotes: 0

Views: 69

Answers (2)

Rob L
Rob L

Reputation: 3734

I'm a professional, and I would add the old users to the new DB and put in random passwords. I would also make a table of old_users with their old hashed passwords.

I would flag these old users such that, when they visit the new app, they would be forced to enter their old pw (you'd need to know the hash method) and, if successful, then set the old pw to the new user, and log them in.

If that's too much trouble, you could write a script that sends all the old users an email (naturally, you have their email address) and a link to a change_password form. It's pretty easy to extend the django reset password functionality. And it's a good thing to have.

Upvotes: 2

MrName
MrName

Reputation: 2529

Could you just migrate the existing users into the new database by looping through the existing users and calling the create_user function? This would take care of the password hashing and everything, assuming that you can decrypt your current passwords back to plaintext.

Upvotes: 0

Related Questions