From 3ad357f4173c944885c1b8eb7c280efaabd9024c Mon Sep 17 00:00:00 2001 From: Mohamed Nouffer Date: Thu, 6 Oct 2022 17:42:12 +0530 Subject: [PATCH] update location filter --- config/settings.py | 1 + rog/.DS_Store | Bin 0 -> 6148 bytes rog/backend.py | 28 +++++++ rog/migrations/0034_alter_customuser_email.py | 18 +++++ rog/migrations/0035_alter_customuser_email.py | 18 +++++ rog/migrations/0036_alter_customuser_email.py | 18 +++++ rog/models.py | 3 +- rog/serializers.py | 12 ++- rog/urls.py | 5 +- rog/views.py | 73 +++++++++++++----- 10 files changed, 154 insertions(+), 22 deletions(-) create mode 100644 rog/.DS_Store create mode 100644 rog/backend.py create mode 100644 rog/migrations/0034_alter_customuser_email.py create mode 100644 rog/migrations/0035_alter_customuser_email.py create mode 100644 rog/migrations/0036_alter_customuser_email.py diff --git a/config/settings.py b/config/settings.py index 3091d3f..b50fd4f 100644 --- a/config/settings.py +++ b/config/settings.py @@ -148,6 +148,7 @@ MEDIA_ROOT = BASE_DIR / "media/" #STATICFILES_DIRS = (os.path.join(BASE_DIR, "static2"),os.path.join(BASE_DIR, "media")) +AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend' , 'rog.backend.EmailOrUsernameModelBackend', ) AUTH_USER_MODEL = 'rog.CustomUser' diff --git a/rog/.DS_Store b/rog/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9bf20143cab149b419e95b5246963fa6d49b1c43 GIT binary patch literal 6148 zcmeHKyG{c^3>-s>2%40X`wRTRDhgka9{>p{iUb!aJnF0Xu6$a?4Z;b zTokHc?RuNC7GEt$=?Y8r`uMj*0QzE~o%@f34I3_Yfv!oJ}YBge5(wT2n*9*tQq{C|XuzIr9gkte@-ru4e))N(_ zfD|}Y;5xSp@Ba_<3-kXeNjoVZ1^$%+He0P%OTJR|*2&9xuWj^4y4M`&Zd?b2A=)u9 i+A%lYj_;x<>zc25-V4XXpfewIqJ9Qk7nv0JYXy$iFcrN3 literal 0 HcmV?d00001 diff --git a/rog/backend.py b/rog/backend.py new file mode 100644 index 0000000..484c433 --- /dev/null +++ b/rog/backend.py @@ -0,0 +1,28 @@ +from django.conf import settings +#from django.contrib.auth import get_user_model +from .models import CustomUser +from django.contrib.auth.backends import ModelBackend + +class EmailOrUsernameModelBackend(ModelBackend): + """ + This is a ModelBacked that allows authentication + with either a username or an email address. + + """ + def authenticate(self, username=None, password=None): + if '@' in username: + kwargs = {'email': username} + else: + kwargs = {'username': username} + try: + user = CustomUser.objects.get(**kwargs) + if user.check_password(password): + return user + except User.DoesNotExist: + return None + + def get_user(self, username): + try: + return CustomUser.objects.get(pk=username) + except get_user_model().DoesNotExist: + return None \ No newline at end of file diff --git a/rog/migrations/0034_alter_customuser_email.py b/rog/migrations/0034_alter_customuser_email.py new file mode 100644 index 0000000..0822fbb --- /dev/null +++ b/rog/migrations/0034_alter_customuser_email.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.9 on 2022-10-06 10:51 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('rog', '0033_alter_templocation_sub_loc_id'), + ] + + operations = [ + migrations.AlterField( + model_name='customuser', + name='email', + field=models.CharField(max_length=255, verbose_name='user name'), + ), + ] diff --git a/rog/migrations/0035_alter_customuser_email.py b/rog/migrations/0035_alter_customuser_email.py new file mode 100644 index 0000000..c5d37e6 --- /dev/null +++ b/rog/migrations/0035_alter_customuser_email.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.9 on 2022-10-06 10:52 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('rog', '0034_alter_customuser_email'), + ] + + operations = [ + migrations.AlterField( + model_name='customuser', + name='email', + field=models.EmailField(max_length=254, unique=True, verbose_name='user name'), + ), + ] diff --git a/rog/migrations/0036_alter_customuser_email.py b/rog/migrations/0036_alter_customuser_email.py new file mode 100644 index 0000000..2cab59c --- /dev/null +++ b/rog/migrations/0036_alter_customuser_email.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.9 on 2022-10-06 11:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('rog', '0035_alter_customuser_email'), + ] + + operations = [ + migrations.AlterField( + model_name='customuser', + name='email', + field=models.CharField(max_length=255, unique=True, verbose_name='Email'), + ), + ] diff --git a/rog/models.py b/rog/models.py index 5bfd3d0..d9a1296 100644 --- a/rog/models.py +++ b/rog/models.py @@ -1,5 +1,6 @@ from dataclasses import field import email +from enum import unique from pyexpat import model from sre_constants import CH_LOCALE from typing import ChainMap @@ -182,7 +183,7 @@ class CustomUser(AbstractBaseUser, PermissionsMixin): GB2 = '大垣-3時間', '大垣-3時間' GB3 = '大垣-5時間', '大垣-5時間' - email = models.EmailField(_("email address"), unique=True) + email = models.CharField(_("Email"), max_length=255, unique=True) is_staff = models.BooleanField(default=False) is_active = models.BooleanField(default=True) group = models.CharField(max_length=255, diff --git a/rog/serializers.py b/rog/serializers.py index 810bda6..d2186ef 100644 --- a/rog/serializers.py +++ b/rog/serializers.py @@ -139,4 +139,14 @@ class TestSerialiser(serializers.ModelSerializer): class Meta: model = TestModel - fields = ('id', 'testbane', 'wanttogo', 'like', 'checkin') \ No newline at end of file + fields = ('id', 'testbane', 'wanttogo', 'like', 'checkin') + + +class ChangePasswordSerializer(serializers.Serializer): + model = CustomUser + + """ + Serializer for password change endpoint. + """ + old_password = serializers.CharField(required=True) + new_password = serializers.CharField(required=True) \ No newline at end of file diff --git a/rog/urls.py b/rog/urls.py index d7a0cca..dc5e7d3 100644 --- a/rog/urls.py +++ b/rog/urls.py @@ -1,6 +1,6 @@ from rest_framework import urlpatterns from rest_framework.routers import DefaultRouter -from .views import LocationViewSet, Location_lineViewSet, Location_polygonViewSet, Jpn_Main_PerfViewSet, Jpn_PerfViewSet, LocationsInPerf, SubInPerf, ExtentForSubPerf, SubPerfInMainPerf, ExtentForMainPerf, LocationsInSubPerf, CatView, RegistrationAPI, LoginAPI, UserAPI, UserActionViewset, UserMakeActionViewset, UserDestinations, UpdateOrder, LocationInBound, DeleteDestination, CustomAreaLocations, GetAllGifuAreas, CustomAreaNames, userDetials, UserTracksViewSet, CatByCity +from .views import LocationViewSet, Location_lineViewSet, Location_polygonViewSet, Jpn_Main_PerfViewSet, Jpn_PerfViewSet, LocationsInPerf, SubInPerf, ExtentForSubPerf, SubPerfInMainPerf, ExtentForMainPerf, LocationsInSubPerf, CatView, RegistrationAPI, LoginAPI, UserAPI, UserActionViewset, UserMakeActionViewset, UserDestinations, UpdateOrder, LocationInBound, DeleteDestination, CustomAreaLocations, GetAllGifuAreas, CustomAreaNames, userDetials, UserTracksViewSet, CatByCity, ChangePasswordView from django.urls import path, include from knox import views as knox_views @@ -40,5 +40,6 @@ urlpatterns += [ path('updateorder/', UpdateOrder, name='updateorder'), path('delete_destination/', DeleteDestination, name='delete_detination'), path('customareanames/', CustomAreaNames, name='custom_area_name'), - path('userdetials/', userDetials, name='user_detials') + path('userdetials/', userDetials, name='user_detials'), + path('change-password/', ChangePasswordView.as_view(), name='change-password'), ] \ No newline at end of file diff --git a/rog/views.py b/rog/views.py index 6454fb4..6405229 100644 --- a/rog/views.py +++ b/rog/views.py @@ -1,15 +1,17 @@ +from curses.ascii import NUL from django.core.serializers import serialize from .models import Location, Location_line, Location_polygon, JpnAdminMainPerf, JpnAdminPerf, JpnSubPerf, Useractions, GifuAreas, RogUser, CustomUser, UserTracks from rest_framework import viewsets -from .serializers import LocationSerializer, Location_lineSerializer, Location_polygonSerializer, JPN_main_perfSerializer, JPN_perfSerializer, JPN_sub_perSerializer, LocationCatSerializer, CreateUserSerializer, UserSerializer, LoginUserSerializer, UseractionsSerializer, UserDestinationSerializer, GifuAreaSerializer, LocationEventNameSerializer, RogUserSerializer, UserTracksSerializer +from .serializers import LocationSerializer, Location_lineSerializer, Location_polygonSerializer, JPN_main_perfSerializer, JPN_perfSerializer, JPN_sub_perSerializer, LocationCatSerializer, CreateUserSerializer, UserSerializer, LoginUserSerializer, UseractionsSerializer, UserDestinationSerializer, GifuAreaSerializer, LocationEventNameSerializer, RogUserSerializer, UserTracksSerializer, ChangePasswordSerializer from knox.models import AuthToken -from rest_framework import viewsets, permissions, generics +from rest_framework import viewsets, permissions, generics, status from rest_framework.decorators import action from rest_framework.response import Response from rest_framework.parsers import JSONParser, MultiPartParser from .serializers import LocationSerializer from django.http import JsonResponse +from rest_framework.permissions import IsAuthenticated from .serializers import TestSerialiser from .models import TestModel @@ -66,9 +68,9 @@ def LocationsInPerf(request): locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom, category=cat) else: if grp: - locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom, category=cat, cp__gt=0, group__contains=grp) + locs = Location.objects.filter(geom__within=perf_geom.geom, category=cat, group__contains=grp) else: - locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom, category=cat) + locs = Location.objects.filter(geom__within=perf_geom.geom, category=cat) else: if is_rog: if grp: @@ -77,9 +79,9 @@ def LocationsInPerf(request): locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom) else: if grp: - locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom, group__contains=grp) + locs = Location.objects.filter(geom__within=perf_geom.geom, group__contains=grp) else: - locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom) + locs = Location.objects.filter(geom__within=perf_geom.geom) serializer = LocationSerializer(locs, many=True) return JsonResponse(serializer.data, safe=False) @@ -99,9 +101,9 @@ def LocationsInSubPerf(request): locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom, category=cat) else: if grp: - locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom, category=cat, group__contains=grp) + locs = Location.objects.filter(geom__within=perf_geom.geom, category=cat, group__contains=grp) else: - locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom, category=cat) + locs = Location.objects.filter(geom__within=perf_geom.geom, category=cat) else: if is_rog: if grp: @@ -109,7 +111,7 @@ def LocationsInSubPerf(request): else: locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom) else: - locs = Location.objects.filter(~Q(cp=0), geom__within=perf_geom.geom) + locs = Location.objects.filter(geom__within=perf_geom.geom) serializer = LocationSerializer(locs, many=True) return JsonResponse(serializer.data, safe=False) @@ -138,9 +140,9 @@ def LocationInBound(request): locs = Location.objects.filter(~Q(cp=0), geom__within=pl, category=cat, event_name__isnull=True) else: if grp: - locs = Location.objects.filter(~Q(cp=0), geom__within=pl, category=cat, event_name__isnull=True, cp=0, group__contains=grp) + locs = Location.objects.filter(geom__within=pl, category=cat, event_name__isnull=True, group__contains=grp) else: - locs = Location.objects.filter(~Q(cp=0), geom__within=pl, category=cat, event_name__isnull=True) + locs = Location.objects.filter(geom__within=pl, category=cat, event_name__isnull=True) else: if is_rog: if grp: @@ -149,9 +151,9 @@ def LocationInBound(request): locs = Location.objects.filter(~Q(cp=0), geom__within=pl, event_name__isnull=True) else: if grp: - locs = Location.objects.filter(~Q(cp=0), geom__within=pl, event_name__isnull=True, group__contains=grp) + locs = Location.objects.filter(geom__within=pl, event_name__isnull=True, group__contains=grp) else: - locs = Location.objects.filter(~Q(cp=0), geom__within=pl, event_name__isnull=True) + locs = Location.objects.filter(geom__within=pl, event_name__isnull=True) if len(locs) > 50: return JsonResponse({"too_many_points": True}, safe=False, status=500) else: @@ -236,7 +238,7 @@ def CatByCity(request): serializer = LocationCatSerializer(cats, many=True) return JsonResponse(serializer.data, safe=False) else: - return null + return None @@ -392,9 +394,9 @@ def CustomAreaLocations(request): locs = Location.objects.filter(~Q(cp=0), event_name__isnull=False, category=cat, event_name=name) else: if grp: - locs = Location.objects.filter(~Q(cp=0), event_name__isnull=False, category=cat, event_name=name, group__contains=grp) + locs = Location.objects.filter(event_name__isnull=False, category=cat, event_name=name, group__contains=grp) else: - locs = Location.objects.filter(~Q(cp=0), event_name__isnull=False, category=cat, event_name=name) + locs = Location.objects.filter(event_name__isnull=False, category=cat, event_name=name) else: if is_rog: if grp: @@ -403,9 +405,9 @@ def CustomAreaLocations(request): locs = Location.objects.filter(~Q(cp=0), event_name__isnull=False, event_name=name) else: if grp: - locs = Location.objects.filter(~Q(cp=0), event_name__isnull=False, event_name=name, group__contains=grp) + locs = Location.objects.filter(event_name__isnull=False, event_name=name, group__contains=grp) else: - locs = Location.objects.filter(~Q(cp=0), event_name__isnull=False, event_name=name) + locs = Location.objects.filter(event_name__isnull=False, event_name=name) serializer = LocationSerializer(locs, many=True) return JsonResponse(serializer.data, safe=False) @@ -418,6 +420,41 @@ def CustomAreaNames(request): return JsonResponse(serializer.data, safe=False) +class ChangePasswordView(generics.UpdateAPIView): + """ + An endpoint for changing password. + """ + serializer_class = ChangePasswordSerializer + model = CustomUser + permission_classes = (IsAuthenticated,) + + def get_object(self, queryset=None): + obj = self.request.user + return obj + + def update(self, request, *args, **kwargs): + self.object = self.get_object() + serializer = self.get_serializer(data=request.data) + + if serializer.is_valid(): + # Check old password + if not self.object.check_password(serializer.data.get("old_password")): + return Response({"old_password": ["Wrong password."]}, status=status.HTTP_400_BAD_REQUEST) + # set_password also hashes the password that the user will get + self.object.set_password(serializer.data.get("new_password")) + self.object.save() + response = { + 'status': 'success', + 'code': status.HTTP_200_OK, + 'message': 'Password updated successfully', + 'data': [] + } + + return Response(response) + + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + class TestActionViewSet(viewsets.ModelViewSet): serializer_class = TestSerialiser queryset = TestModel.objects.all()