def register(self, request): data = (request.data).dict() person = models.Person() person.first_name = data["first_name"] person.last_name = data["last_name"] person.phone_no = data["phone_no"] person.email = data["email"] person.image = None person.password = make_password(data["password"]) if data["user_type"] == '1': user = models.User() person.user_type = 1 user.address = data["address"] user.save() person.user_id = user.id person.organizer_id = None elif data["user_type"] == '2': organizer = models.Organizer() person.user_type = 2 organizer.address = data["address"] organizer.organization = data["organization"] organizer.experience = data["experience"] organizer.cnic = data["cnic"] organizer.save() person.organizer_id = organizer.id person.user_id = None person.save() serializer = serializers.PersonSerializer(person) return Response(status=status.HTTP_200_OK, data=serializer.data)
def top_organizers(self, request): newlist = [] organizers = self.queryset.filter(user_type=2) serializer = serializers.PersonSerializer(organizers, many=True) if serializer.data: newlist = sorted(serializer.data, key=lambda k: k['organizer']['rating'])[:10] return Response(status=status.HTTP_200_OK, data=newlist)
def post(self, request): serializer = serializers.PersonSerializer(data=request.data) if serializer.is_valid(): serializer.save() res = Response(serializer.data, status=status.HTTP_201_CREATED) return res res = Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return res
def update_user(request, id): data = {} person = models.Person.objects.get(id=id) if person: if request.data["user_type"] == "1": user = models.User.objects.get(id=person.user_id) address = request.data["address"] if address: serializer = serializers.UserSerializer( user, data={"address": request.data["address"]}) if serializer.is_valid(): serializer.save() elif request.data["user_type"] == "2": organizer = models.Organizer.objects.get(id=person.organizer_id) serializer = serializers.OrganizerSerializer( organizer, data={ "address": request.data["address"], "experience": request.data["experience"], "organization": request.data["organization"] }) if serializer.is_valid(): serializer.save() data["errors"] = str(serializer.errors) if request.data.get("first_name", None): data["first_name"] = request.data.get("first_name", None) if request.data.get("last_name", None): data["last_name"] = request.data.get("last_name", None) if request.data.get("phone_no", None): data["phone_no"] = request.data.get("phone_no", None) if "image" in request.data: if person.image: image = str(person.image) path = "media\\uploads\\users\\" + image.split("/")[-1] os.remove(path) data["image"] = request.data["image"] else: data["image"] = person.image serializer = serializers.PersonOnlySerializer(person, data=data) if serializer.is_valid(): person = serializer.save() serializer = serializers.PersonSerializer(person) return Response(status=status.HTTP_200_OK, data=serializer.data) if "errors" not in data: data["errors"] = str(serializer.errors) else: data["errors"] += str(serializer.errors) return Response(status=status.HTTP_400_BAD_REQUEST, data=data) return Response(status=status.HTTP_400_BAD_REQUEST, data={})
def setFirebaseInstanceToken(request, id): person = models.Person.objects.get(id=id) if person: data = {} person.firebaseinstancetoken = request.data.get("firebase_token", None) person.save() serializer = serializers.PersonSerializer(person) return Response(status=status.HTTP_200_OK, data=serializer.data) return Response(status=status.HTTP_400_BAD_REQUEST, data=[])
def updatePass(self, request): if request.data.get("email", None) and request.data.get( "password", None): person = models.Person.objects.get( email=request.data.get("email", None)) new = make_password(request.data.get("password", None)) person.password = new person.save() serializer = serializers.PersonSerializer(person) return Response(status=status.HTTP_200_OK, data=serializer.data) return Response(status=status.HTTP_400_BAD_REQUEST, data=[])
def login(self, request): data = request.data user = self.queryset.get(email=request.data['email']) if user.user_id or user.organizer_id: if user: if hasher.check_password(request.data['password'], user.get_password()): serializer = serializers.PersonSerializer(user) return Response(status=status.HTTP_200_OK, data=serializer.data) else: return Response( status=status.HTTP_400_BAD_REQUEST, data= "This Email is registered as Social Login! Please use Social login option" ) return Response(status=status.HTTP_400_BAD_REQUEST, data="Invalid Email or Password!")
def list(self, request): queryset = Person.objects.all() serializer = serializers.PersonSerializer(queryset, many=True) return Response(serializer.data)