Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dheerajsps patch 1 #12

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 2 additions & 40 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<<<<<<< HEAD

name: Django Tests
on:
push:
Expand All @@ -21,7 +21,7 @@ jobs:
python-version:3.6
-name: Install dependencies
run: |
python -m pip install --upgrade pip
python3 -m pip install --upgrade pip
pip3 install -r requirements.txt
-name: Lint with flake8
run: |
Expand All @@ -35,41 +35,3 @@ jobs:
-name: Django Testing
run : |
python3 manage.py test
=======
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the action will run.
on:
# Triggers the workflow on push or pull request events but only for the master branch
push:
branches: [ master ]
pull_request:
branches: [ master ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

# Runs a single command using the runners shell
- name: Run a one-line script
run: echo Hello, world!

# Runs a set of commands using the runners shell
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
>>>>>>> f0b084f597c2d73179d049e46e029f826e8d6147
3 changes: 0 additions & 3 deletions .github/workflows/testing_one.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,3 @@ jobs:
run: |
python3 -m pip install --upgrade pip
pip3 install pylint
pip3 install -r requirements.txt
python3 manage.py test

1 change: 1 addition & 0 deletions jangoadmin/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config="api.ApiConfig"
4 changes: 2 additions & 2 deletions jangoadmin/api/apps.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.apps import AppConfig


class ApiConfig(AppConfig):
name = 'api'
def ready(self):
from api import signals
8 changes: 8 additions & 0 deletions jangoadmin/api/enums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from enum import Enum
class TokenType(Enum):
Reset_Password="Reset Password"
Forget_Password="Forget Password"
Email_Registration_Confirmation="Email Registration Confirmation"



27 changes: 27 additions & 0 deletions jangoadmin/api/migrations/0002_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.0.14 on 2021-04-22 06:18

import api.enums
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('api', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Token',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('token_type', models.CharField(choices=[(api.enums.TokenType['Reset_Password'], 'Reset Password'), (api.enums.TokenType['Forget_Password'], 'Forget Password'), (api.enums.TokenType['Email_Registration_Confirmation'], 'Email Registration Confirmation')], max_length=100, verbose_name='Token Type')),
('token', models.CharField(blank=True, default=None, max_length=100, null=True, verbose_name='Token')),
('created_at', models.DateTimeField(blank=True, default=None, null=True, verbose_name='Created At')),
('expiry_minutes', models.IntegerField(default=30, verbose_name='Expiry Minutes')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
18 changes: 18 additions & 0 deletions jangoadmin/api/migrations/0003_user_avatar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.0.14 on 2021-04-23 07:20

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('api', '0002_token'),
]

operations = [
migrations.AddField(
model_name='user',
name='avatar',
field=models.FileField(blank=True, default=None, null=True, upload_to='avatars', verbose_name='Avatar'),
),
]
12 changes: 11 additions & 1 deletion jangoadmin/api/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models
from django.contrib.auth.models import AbstractBaseUser,PermissionsMixin
from django.contrib.auth.base_user import BaseUserManager
from .enums import TokenType
class UserManager(BaseUserManager):
def create_superuser(self,email,username,password):
'''
Expand Down Expand Up @@ -29,11 +30,20 @@ class User(AbstractBaseUser,PermissionsMixin):
created_on=models.DateTimeField("Created On",auto_now_add=True)
updated_on=models.DateTimeField("Updated On",auto_now_add=True)
email=models.EmailField("Email",null=False,blank=False,unique=True,error_messages={"unique":"OOPS,An account with this email is already regisgtered"})
avatar=models.FileField("Avatar",null=True,blank=True,upload_to="avatars",default=None)
username=models.CharField("UserName",null=False,blank=False,max_length=100,unique=True,error_messages={"unique":"An UserName with this username is already regisgtered"})
objects=UserManager()
REQUIRED_FIELDS=['username']
USERNAME_FIELD='email'
def save(self,*args,**kwargs):
self.email=self.email.lower()
self.username=self.username.lower()
super(User,self).save(*args,**kwargs)
super(User,self).save(*args,**kwargs)
class Token(models.Model):
token_type=models.CharField("Token Type",max_length=100,choices=((type,type.value) for type in TokenType))
token=models.CharField("Token",max_length=100,null=True,blank=True,default=None)
user=models.ForeignKey(User,on_delete=models.CASCADE)
created_at=models.DateTimeField("Created At",null=True,blank=True,default=None)
expiry_minutes=models.IntegerField("Expiry Minutes",default=30)
def __str__(self):
return str(self.token) + "_" + str(self.token)
3 changes: 3 additions & 0 deletions jangoadmin/api/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import logging,traceback
from api.models import User
logger=logging.getLogger(__name__)
5 changes: 5 additions & 0 deletions jangoadmin/api/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ def test_login_without_data(self):
my_dict=ast.literal_eval(self.data)
response=self.client.post(reverse("login"),data=my_dict,format="json")
self.assertEqual(response.status_code,status.HTTP_400_BAD_REQUEST)
def test_change_password_with_data(self):
self.data='{"new_password":"rahul123","confirm_password":"rahul123","old_password":"test@123"}'
my_dict=ast.literal_eval(self.data)
response=self.client.post(reverse("ChangePassWord"),data=my_dict,format="json")
self.assertEqual(response.status_code,status.HTTP_401_UNAUTHORIZED)
8 changes: 4 additions & 4 deletions jangoadmin/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from django.conf.urls import url
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns
from api.views import UserSignUp,LoginView

from api.views import UserSignUp,LoginView,ChangePassWord,AvatarView
urlpatterns = [
path('register/', UserSignUp.as_view(),name="register"),
path('login/', LoginView.as_view(),name="login"),
path('change-password/', ChangePassWord.as_view(),name="ChangePassWord"),
# path('profile/', MyProfile),
# path('avatar/', Avatar),
# path('change-password/', ChangePassword),
path('avatar/', AvatarView.as_view(),name="AvatarView"),

# path('forgot-password/', ForgotPassword),
# path('logout/', Logout),
# path('home/', Dashboard),
Expand Down
99 changes: 97 additions & 2 deletions jangoadmin/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,25 @@
# data = {"status":200,"data":{"message":1}}
# return JsonResponse(data)

import logging,traceback
import json,logging,traceback,os,boto3
from decouple import config
from boto3.s3.transfer import S3Transfer
from django.core.files.storage import FileSystemStorage
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import User
from .serializers import UserSerializer
from django.contrib.auth import authenticate,login
from rest_framework.authtoken.models import Token
from rest_framework.permissions import IsAuthenticated
from rest_framework.authentication import TokenAuthentication
logger=logging.getLogger(__name__)
def checkAuth(request):
if Token.objects.filter(key=request.META.get('HTTP_TOKEN')):
return Token.objects.filter(key=request.META.get('HTTP_TOKEN'))[0]
else:
return 0
class UserSignUp(APIView):
'''
API FOR SIGNUP
Expand Down Expand Up @@ -392,4 +402,89 @@ def post(self,request):
except Exception:
logger.exception(traceback.format_exc())
logger.exception("Something went wrong in " + "Post" + "login")
return Response({"status":True,"message":"Something went wrong"},status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response({"status":True,"message":"Something went wrong"},status=status.HTTP_500_INTERNAL_SERVER_ERROR)
class ChangePassWord(APIView):
'''
API FOR CHANGING PASSWORD
YOU SHOULD BE AUTHENICATED USER IN ORDER TO HIT THIS API

'''
authentication_classes = (TokenAuthentication,)
permission_classes=(IsAuthenticated,)
def post(self,request):
try:
params=request.data

try:
current_password=params.pop("new_password")
except Exception:
current_password=None
if not current_password:
return Response({"status":False, "message":"OOPS, Please Mention New Password"},status=status.HTTP_400_BAD_REQUEST)
try:
confirm_password=params.pop("confirm_password")
except Exception:
current_password=None
if not confirm_password:
return Response({"status":False,"message":"OOPS,Please Confirm your password Once"},status=status.HTTP_400_BAD_REQUEST)
if current_password!=confirm_password:
return Response({"status":False,"message":"OOPS,Passowrd didn't matched"},status=status.HTTP_400_BAD_REQUEST)
try:
old_password=params.pop("old_password")
except Exception:
old_password=None
if not old_password:
return Response({"status":False,'message':"Old Password is required"},status=status.HTTP_400_BAD_REQUEST)
email=request.user.email
user=authenticate(email=email,password=old_password)
if not user:
return Response({"status":False,"message":"Credentials are invalid"},status=status.HTTP_400_BAD_REQUEST)
user=User.objects.get(email=email)
user.set_password(current_password)
user.save()
return Response({"status":True,"message":"Password Updated Successfully"},status=status.HTTP_200_OK)
except Exception:
logger.exception(traceback.format_exc())
logger.exception("Something went wrong in " + "POST" + "changepassword")
return Response({"status":False,"message":"OOPS,Something went wrong"},status=status.HTTP_500_INTERNAL_SERVER_ERROR)
class AvatarView(APIView):
def get(self,request):
try:
token=checkAuth(request)
if token== 0:
data={"status":403,"data":{"message":"Not logged in"}}
return Response(data)
AWS_STORAGE_URL=config("AWS_STORAGE_URL",default="")
user=token.user
if str(token.user.avatar)!="":
data={"status":200,"data":{"url":AWS_STORAGE_URL+str(token.user.profile.avatar)}}
else:
data={"status":404,"data":{"message":"avatar missing"}}
return Response(data)
except Exception:
logger.exception(traceback.format_exc())
logger.exception("Something went wrong in" + "GET" + "AvatarView")
return Response({"status":False,"message":"Something went wrong"},status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def post(self,request):
try:
avatar = request.FILES['file']
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID', default='')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY', default='')
AWS_STORAGE_BUCKET_RGN = config('AWS_STORAGE_BUCKET_RGN', default='us-east-1')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME', default='test')
fs = FileSystemStorage(os.getcwd()+'/static/img/avatars')
fname = avatar.name
filename = fs.save(fname,avatar)
s3_path = 'avatars/'+fname
local_path = os.getcwd()+'/static/img/avatars/'+fname
transfer = S3Transfer(boto3.client('s3', AWS_STORAGE_BUCKET_RGN, aws_access_key_id = AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY,use_ssl=False))
client = boto3.client('s3')
transfer.upload_file(local_path, AWS_STORAGE_BUCKET_NAME, s3_path,extra_args={'ACL': 'public-read'})
User.objects.filter(user=token.user.id).update(avatar=s3_path)
os.remove(local_path)
token = checkAuth(request)
user = token.user
except Exception as error:
logger.exception(traceback.format_exc())
logger.exception("Something went wrong " + 'GET' + 'AvatarView')
return Response({"status":False,"message":"Something went wrong","error":error},status=status.HTTP_500_INTERNAL_SERVER_ERROR)
13 changes: 6 additions & 7 deletions jangoadmin/jangoadmin/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@
}

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
'DEFAULT_PARSER_CLASSES': (

'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
),
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.MultiPartParser',
'rest_framework.parsers.FileUploadParser',
Expand Down Expand Up @@ -129,6 +127,7 @@
}
}


# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

Expand Down
2 changes: 1 addition & 1 deletion jangoadmin/jangoadmin/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#router.register(r'bread', BreadViewSet),

urlpatterns = [
#path('admin/', admin.site.urls),
path('admin/', admin.site.urls),
#path('', include(router.urls)),
#path('secret/', include('secret.urls')),
path('api/', include('api.urls')),
Expand Down
1 change: 1 addition & 0 deletions jangoadmin/pytest.ini.save
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Loading