Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AishaAgarwal committed Jan 8, 2024
0 parents commit 37b126c
Show file tree
Hide file tree
Showing 433 changed files with 41,101 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
26 changes: 26 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
SERVICE_ENV=development
HOST=127.0.0.1
PORT=8000
API_KEY=AIzaSyB_tOjzX6Em41dcetUBf1pqaOY4pU-E-kM
AUTH_DOMAIN=translate-fe564.firebaseapp.com
DATABASE_URL=https://translate-fe564-default-rtdb.firebaseio.com/
PROJECT_ID=translate-fe564
STORAGE_BUCKET=translate-fe564.appspot.com
MESSAGING_SENDER_ID=186396337537
APP_ID=1:186396337537:web:77729bd3347a8d3a933480
MEASUREMENT_ID=G-T4CXD6SY5G



FIREBASE_ACCOUNT_TYPE=service_account
FIREBASE_PROJECT_ID=translate-fe564
FIREBASE_PRIVATE_KEY_ID=2e4a746ba666c81889c594dac6abbd7399c6b7ce
FIREBASE_PRIVATE_KEY=-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDJo6weiI0i7xoG\nksZMs/edWV73LTzrOYmUPrtiYARYZwvFzHjtkUTNVjkho7vBerLTbrl1xxcYHPhj\npEchDPq8lOY+Xnby1CcYy5iqHQXVxjyWw9VbaPzoB7nnFk/FG/lUQPhKUoOxOjjI\n4pt+sAcuw+y+5IiD7LGHktMAMxy7KexkWJjkcx9xlaiEuA1sVmTIXiz1CGFAXc97\nzgCSsn4I8VKbhDtq3qIn+g17uVMu0T3+gUDGgz/a/c34EVYhRkcjpMlO/hPoMx4g\nwnxv1vAZkyGyjkHMZOjGLFxdnmqtvp8bDAdMeoHCy9gqNXk/Izz7/ajlAJJKkUbP\nmrTSeXrdAgMBAAECggEAGu0LzXH/otQZ/hm4eEKqBK2MBGbalTC0QgMUg8D9o45z\nIIE6/sJ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sWwnOFWKtEWiKFG8DbJ2TpECLtXiiIXgYGzKQO\nOTNOb6ZfZ2AmtMIvpxzCPKtnv2kRdt1PKp6VXlDr0WVqKKc+/O3tJ/RwMMKvEWBK\nPQAw14UomNhQFnpeyxtsK/A=\n-----END PRIVATE KEY-----\n
FIREBASE_CLIENT_EMAIL=[email protected]
FIREBASE_CLIENT_ID=106650411189339404056
FIREBASE_AUTH_URI=https://accounts.google.com/o/oauth2/auth
FIREBASE_TOKEN_URI=https://oauth2.googleapis.com/token
FIREBASE_AUTH_PROVIDER_X509_CERT_URL=https://www.googleapis.com/oauth2/v1/certs
FIREBASE_CLIENT_X509_CERT_URL=https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-a8gi7%40translate-fe564.iam.gserviceaccount.com
# universe_domain=googleapis.com

Empty file added account/__init__.py
Empty file.
Binary file added account/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file added account/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added account/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added account/__pycache__/admin.cpython-312.pyc
Binary file not shown.
Binary file added account/__pycache__/admin.cpython-38.pyc
Binary file not shown.
Binary file added account/__pycache__/admin.cpython-39.pyc
Binary file not shown.
Binary file added account/__pycache__/apps.cpython-312.pyc
Binary file not shown.
Binary file added account/__pycache__/apps.cpython-38.pyc
Binary file not shown.
Binary file added account/__pycache__/apps.cpython-39.pyc
Binary file not shown.
Binary file added account/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file added account/__pycache__/models.cpython-38.pyc
Binary file not shown.
Binary file added account/__pycache__/models.cpython-39.pyc
Binary file not shown.
Binary file added account/__pycache__/serializers.cpython-312.pyc
Binary file not shown.
Binary file added account/__pycache__/serializers.cpython-38.pyc
Binary file not shown.
Binary file added account/__pycache__/serializers.cpython-39.pyc
Binary file not shown.
Binary file added account/__pycache__/urls.cpython-312.pyc
Binary file not shown.
Binary file added account/__pycache__/urls.cpython-38.pyc
Binary file not shown.
Binary file added account/__pycache__/urls.cpython-39.pyc
Binary file not shown.
Binary file added account/__pycache__/views.cpython-38.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions account/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions account/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AccountConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'account'
Empty file added account/migrations/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
45 changes: 45 additions & 0 deletions account/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.conf import settings
from django.contrib.auth.models import User

from .serializers import UserSerializer
from utils import get_datetime

def register_user(username, email):
try:
user = User.objects.filter(username=username, email=email)
user_seralizer = UserSerializer(user.first())
user_data = user_seralizer.data
user_data['token'] = None
user_data['plan'] = 'free'
settings.FIREBASE_DB.child('users').child(username).update(user_data)
except:
pass
return None

def login_user(username, token):
try:
last_login = get_datetime()
settings.FIREBASE_DB.child('users').child(username).update({'token': token, 'last_login': last_login})
except:
pass
return None

def logout_user(username):
try:
last_logout = get_datetime()
settings.FIREBASE_DB.child('users').child(username).update({'token': None, 'last_logout': last_logout})
except:
pass
return None

def get_user_plan(username):
"""Getting user plan on the base of username."""
try:
data = settings.FIREBASE_DB.child('user_plan').child(username).get().val()
if data is None:
data = 'free'
else:
data = data['plan']
except:
data = 'free'
return data
38 changes: 38 additions & 0 deletions account/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from rest_framework import serializers
from django.contrib.auth.models import User
from rest_framework.validators import UniqueValidator
from django.contrib.auth.password_validation import validate_password


class RegisterSerializer(serializers.ModelSerializer):
email = serializers.EmailField(
required=True,
validators=[UniqueValidator(queryset=User.objects.all())]
)

password = serializers.CharField(write_only=True, required=True, validators=[validate_password])
password2 = serializers.CharField(write_only=True, required=True)

class Meta:
model = User
fields = ('username', 'password', 'password2', 'email')

def validate(self, attrs):
if attrs['password'] != attrs['password2']:
raise serializers.ValidationError({"password": "Password fields didn't match."})
return attrs

def create(self, validated_data):
user = User.objects.create(
username=validated_data['username'],
email=validated_data['email']
)
user.set_password(validated_data['password'])
user.save()
return user


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = '__all__'
3 changes: 3 additions & 0 deletions account/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
10 changes: 10 additions & 0 deletions account/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.urls import path

from . import views


urlpatterns = [
path('login/', views.LoginView.as_view(), name='login'),
path('register/', views.RegisterView.as_view(), name='register'),
path('logout/', views.LogoutView.as_view(), name='logout')
]
3 changes: 3 additions & 0 deletions account/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .register import RegisterView
from .login import LoginView
from .logout import LogoutView
Binary file added account/views/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file added account/views/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added account/views/__pycache__/__init__.cpython-39.pyc
Binary file not shown.
Binary file added account/views/__pycache__/login.cpython-312.pyc
Binary file not shown.
Binary file added account/views/__pycache__/login.cpython-38.pyc
Binary file not shown.
Binary file added account/views/__pycache__/login.cpython-39.pyc
Binary file not shown.
Binary file added account/views/__pycache__/logout.cpython-312.pyc
Binary file not shown.
Binary file added account/views/__pycache__/logout.cpython-38.pyc
Binary file not shown.
Binary file added account/views/__pycache__/logout.cpython-39.pyc
Binary file not shown.
Binary file not shown.
Binary file added account/views/__pycache__/register.cpython-38.pyc
Binary file not shown.
Binary file added account/views/__pycache__/register.cpython-39.pyc
Binary file not shown.
43 changes: 43 additions & 0 deletions account/views/login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from django.conf import settings
from django.contrib.auth.models import User

class LoginView(APIView):
def _validate(self, data):
"""Validating data."""
email = data.get('email')
if email is None:
return 'ERROR: email is not provided.', status.HTTP_400_BAD_REQUEST

password = data.get('password')
if password is None:
return 'ERROR: password is not provided.', status.HTTP_400_BAD_REQUEST
return data, status.HTTP_200_OK

def post(self, request, *args, **kwargs):
data = request.data
rd, sc = self._validate(data)
if sc != 200:
return Response(rd, sc)

# Checking existence firebase
email = data.get('email')
password = data.get('password')
try:
login = settings.AUTH.sign_in_with_email_and_password(email, password)
except:
return Response('ERROR: Given email or password is incorrect.', status=status.HTTP_401_UNAUTHORIZED)
data = {'token': login['idToken']}

# Checking local database
user = User.objects.filter(email=email)
if not user.exists():
user = User.objects.create(
username=login['localId'],
email=email
)
user.set_password(password)
user.save()
return Response(data, status=status.HTTP_200_OK)
12 changes: 12 additions & 0 deletions account/views/logout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from rest_framework.views import APIView
from rest_framework import status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated

from utils.auth import FirebaseAuthentication
class LogoutView(APIView):
authentication_classes = [FirebaseAuthentication]
permission_classes = [IsAuthenticated]
def post(self, request):
# Logout is just removing token from frentend side.
return Response({'messgae': 'User logout.'}, status=status.HTTP_200_OK)
54 changes: 54 additions & 0 deletions account/views/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from rest_framework.views import APIView
from rest_framework import status
from rest_framework.response import Response
from django.conf import settings

from ..serializers import RegisterSerializer
from plan.models import update_plan

class RegisterView(APIView):
def _validate(self, data):
email = data.get('email')
if email is None:
return 'ERROR: email is not provided.', status.HTTP_400_BAD_REQUEST

password = data.get('password')
if password is None:
return 'ERROR: password is not provided.', status.HTTP_400_BAD_REQUEST

password2 = data.get('password2')
if password2 is None:
return 'ERROR: password2 is not provided.', status.HTTP_400_BAD_REQUEST

if password != password2:
return 'ERROR: Password mismatch.', status.HTTP_400_BAD_REQUEST
return data, status.HTTP_200_OK

def post(self, request):
# Getting data
data = request.data
rd, sc = self._validate(data)
if sc != 200:
return Response(rd, sc)

# Making user in firebase
try:
email = data.get('email')
password = data.get('password')
login = settings.AUTH.create_user_with_email_and_password(email, password)
except:
return Response('ERROR: Given email already exists.', status=status.HTTP_400_BAD_REQUEST)

try:
data = data.dict()
except:
pass
data['username'] = login['localId']
serializer = RegisterSerializer(data=data)
# Saving data
if serializer.is_valid():
serializer.save()

# Adding free plan
update_plan(login['localId'], 'free')
return Response('User creates successfully.', status=status.HTTP_201_CREATED)
Binary file added custom_static/.DS_Store
Binary file not shown.
Loading

0 comments on commit 37b126c

Please sign in to comment.