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

User account all API endpoints #272

Open
wants to merge 4 commits into
base: main
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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ EMAIL_PORT=port
EMAIL_HOST_USER=user
EMAIL_HOST_PASSWORD=password

GOOGLE_CLIENT_ID=id

CELERY_BROKER_URL= amqp://guest:guest@rabbitmq:5672// #pragma: allowlist secret
CELERY_RESULT_BACKEND= db+postgresql://postgres:postgres@postgres:5432/djangoindia-db #pragma: allowlist secret

Expand Down
6 changes: 6 additions & 0 deletions backend/djangoindia/api/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from .event import EventRegistrationSerializer, EventSerializer
from .media_library import FolderSerializer
from .partner_and_sponsor import CommunityPartnerAndSponsorSerializer
from .user import ChangePasswordSerializer, UserMeSerializer, UserSerializer
from .volunteer import VolunteerSerializer


__all__ = [
Expand All @@ -11,4 +13,8 @@
"EventSerializer",
"FolderSerializer",
"CommunityPartnerAndSponsorSerializer",
"UserSerializer",
"UserMeSerializer",
"ChangePasswordSerializer",
"VolunteerSerializer",
]
5 changes: 5 additions & 0 deletions backend/djangoindia/api/serializers/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from rest_framework import serializers


class BaseSerializer(serializers.ModelSerializer):
id = serializers.PrimaryKeyRelatedField(read_only=True)
113 changes: 113 additions & 0 deletions backend/djangoindia/api/serializers/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Module import
from rest_framework import serializers

from djangoindia.db.models import User

from .base import BaseSerializer


class UserSerializer(BaseSerializer):
class Meta:
model = User
fields = "__all__"
read_only_fields = [
"id",
"created_at",
"updated_at",
"is_superuser",
"is_staff",
"is_onboarded",
"is_password_autoset",
"is_email_verified",
]
extra_kwargs = {"password": {"write_only": True}}


class UserMeSerializer(BaseSerializer):
class Meta:
model = User
fields = [
"id",
"avatar",
"created_at",
"email",
"first_name",
"last_name",
"is_active",
"is_email_verified",
"is_onboarded",
"mobile_number",
"user_timezone",
"username",
"is_password_autoset",
"gender",
"organization",
]
read_only_fields = fields


class ChangePasswordSerializer(serializers.Serializer):
model = User

"""
Serializer for password change endpoint.
"""
old_password = serializers.CharField(required=True)
new_password = serializers.CharField(required=True, min_length=8)
confirm_password = serializers.CharField(required=True, min_length=8)

def validate(self, data):
if data.get("old_password") == data.get("new_password"):
raise serializers.ValidationError(
{"message": "New password cannot be same as old password."}
)

if data.get("new_password") != data.get("confirm_password"):
raise serializers.ValidationError(
{"message": "Confirm password should be same as the new password."}
)

return data


class ResetPasswordSerializer(serializers.Serializer):
"""
Serializer for password change endpoint.
"""

new_password = serializers.CharField(required=True, min_length=8)


class UserLiteSerializer(BaseSerializer):
class Meta:
model = User
fields = [
"id",
"first_name",
"last_name",
"avatar",
"username",
"email",
"role",
]
read_only_fields = [
"id",
"username",
"email",
]


class UserAdminLiteSerializer(BaseSerializer):
class Meta:
model = User
fields = [
"id",
"first_name",
"last_name",
"avatar",
"username",
"email",
]
read_only_fields = [
"id",
]
9 changes: 8 additions & 1 deletion backend/djangoindia/api/urls/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
from .authentication import urlpatterns as auth_urls
from .communication import urlpatterns as communication_urls
from .event import urlpatterns as event_urls
from .media_library import urlpatterns as media_library_urls
from .partner_and_sponsor import urlpatterns as community_partner_urls
from .user import urlpatterns as user_urls


urlpatterns = (
event_urls + communication_urls + community_partner_urls + media_library_urls
event_urls
+ communication_urls
+ community_partner_urls
+ media_library_urls
+ user_urls
+ auth_urls
)
39 changes: 39 additions & 0 deletions backend/djangoindia/api/urls/authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.urls import path

from djangoindia.api.views import (
ForgotPasswordEndpoint,
OauthEndpoint,
RequestEmailVerificationEndpoint,
ResetPasswordEndpoint,
SignInEndpoint,
SignOutEndpoint,
SignUpEndpoint,
VerifyEmailEndpoint,
)


urlpatterns = [
path("social-auth/", OauthEndpoint.as_view(), name="oauth"),
# Auth
path("sign-up/", SignUpEndpoint.as_view(), name="sign-up"),
path("sign-in/", SignInEndpoint.as_view(), name="sign-in"),
path("sign-out/", SignOutEndpoint.as_view(), name="sign-out"),
# email verification
path("email-verify/", VerifyEmailEndpoint.as_view(), name="email-verify"),
path(
"request-email-verify/",
RequestEmailVerificationEndpoint.as_view(),
name="request-reset-email",
),
# Password Manipulation
path(
"reset-password/<uidb64>/<token>/",
ResetPasswordEndpoint.as_view(),
name="password-reset",
),
path(
"forgot-password/",
ForgotPasswordEndpoint.as_view(),
name="forgot-password",
),
]
39 changes: 39 additions & 0 deletions backend/djangoindia/api/urls/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from django.urls import path

from djangoindia.api.views import (
ChangePasswordEndpoint,
SetUserPasswordEndpoint,
UpdateUserOnBoardedEndpoint,
UserEndpoint,
)


urlpatterns = [
# User Profile
path(
"users/me/",
UserEndpoint.as_view(
{
"get": "retrieve",
"patch": "partial_update",
"delete": "deactivate",
}
),
name="users",
),
path(
"users/me/onboard/",
UpdateUserOnBoardedEndpoint.as_view(),
name="user-onboard",
),
path(
"users/me/set-password/",
SetUserPasswordEndpoint.as_view(),
name="set-password",
),
path(
"users/me/change-password/",
ChangePasswordEndpoint.as_view(),
name="change-password",
),
]
25 changes: 25 additions & 0 deletions backend/djangoindia/api/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
from .authentication import (
ChangePasswordEndpoint,
ForgotPasswordEndpoint,
OauthEndpoint,
RequestEmailVerificationEndpoint,
ResetPasswordEndpoint,
SetUserPasswordEndpoint,
SignInEndpoint,
SignOutEndpoint,
SignUpEndpoint,
VerifyEmailEndpoint,
)
from .communication import ContactUsAPIView, SubscriberAPIView
from .event import EventAPIView, EventAttendeeViewSet
from .media_library import FolderLiteSerializer, FolderSerializer
from .partner_and_sponsor import CommunityPartnerAndSponsorAPIView
from .user import UpdateUserOnBoardedEndpoint, UserEndpoint


__all__ = [
Expand All @@ -12,4 +25,16 @@
"FolderLiteSerializer",
"FolderSerializer",
"CommunityPartnerAndSponsorAPIView",
"UserEndpoint",
"UpdateUserOnBoardedEndpoint",
"ChangePasswordEndpoint",
"ForgotPasswordEndpoint",
"ResetPasswordEndpoint",
"SetUserPasswordEndpoint",
"SignInEndpoint",
"SignOutEndpoint",
"SignUpEndpoint",
"VerifyEmailEndpoint",
"RequestEmailVerificationEndpoint",
"OauthEndpoint",
]
Loading
Loading