python3 -m venv venv
source venv/bin/activate
pip install django
django-admin startproject loginproject
python manage.py runserver
python manage.py startapp loginapp
python manage.py createsuperuser
python manage.py makemigrations
python manage.py migrate
conn = psycopg2.connect(
host="localhost",
port="5432",
database="postgres",
user="postgres",
password="mahima@123"
)
path('createTable/', CreateTableView.as_view(), name='createTable'),
cur.execute('''CREATE TABLE IF NOT EXISTS employees
(id SERIAL PRIMARY KEY,
username TEXT NOT NULL,
email TEXT NOT NULL,
password TEXT NOT NULL)''')
path('UpdateTable/', UpdateTableView.as_view(), name='UpdateTable'),
dict1 = {
"username": "username",
"password": "password",
"email": "email"
}
cur.execute(
"INSERT INTO employees (username, password, email) VALUES (%(username)s, %(password)s, %(email)s);", dict1)
Method | API | Function |
---|---|---|
POST | createTable/ | Create employees |
POST | updateTable/ | Update employees |
GET | get_employees/ | Get all employees |
GET | get_employee/id | Get employee by ID |
POST | add_employee/ | Add new employee |
POST | delete_employee/id | Delete employee |
POST | edit/id | edit employee details |
Method | API | Function |
---|---|---|
GET | get_token/id | Get token |
Method | API | Function |
---|---|---|
GET | register/id | Register |
Method | API | Function |
---|---|---|
POST | login/ | Login |
Method | API | Function |
---|---|---|
POST | send-email/ | Send email invite |
token_lifetime = timedelta(days=1)
token_expiry = datetime.now() + token_lifetime
access_token = jwt.encode(
{
'id': employee['id'],
'username': employee['username'],
'email': employee['email'],
'password': employee['password'],
'exp': int(token_expiry.timestamp())
},
'secret9742357373',
algorithm='HS256'
)
token = access_token
base_url = f"http://127.0.0.1:8000/register/{id}"
query_params = {'token': token}
registration_link = base_url + '?' + urlencode(query_params)
token = request.GET.get('token')
decoded_token = jwt.decode(token, 'secret9742357373', algorithms=['HS256'])
email = decoded_token.get('email')
send_mail(subject, message, from_email, recipient_list)
response1.set_cookie('id', if)
response1.set_cookie('is_admin', False)
user_id=int(request.COOKIES.get('id'))
is_admin=request.COOKIES.get('is_admin')
cur.execute("UPDATE employees SET is_admin = %s WHERE id= %s", [True,id])
response2.set_cookie('is_admin',True)
Method | API | Function |
---|---|---|
POST | make_admin/ | give admin acess to given id |
cur.execute("UPDATE employees SET is_active = %s WHERE id= %s", [False,id]
Method | API | Function |
---|---|---|
POST | deactivate/ | deactivate the account |