update location filter

This commit is contained in:
Mohamed Nouffer
2022-10-06 17:42:12 +05:30
parent 29490698f5
commit 3ad357f417
10 changed files with 154 additions and 22 deletions

View File

@ -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'

BIN
rog/.DS_Store vendored Normal file

Binary file not shown.

28
rog/backend.py Normal file
View File

@ -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

View File

@ -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'),
),
]

View File

@ -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'),
),
]

View File

@ -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'),
),
]

View File

@ -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,

View File

@ -140,3 +140,13 @@ class TestSerialiser(serializers.ModelSerializer):
class Meta:
model = TestModel
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)

View File

@ -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'),
]

View File

@ -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()