Skip to content

Commit 2985017

Browse files
authored
Merge pull request #24 from mpumzee/ft_add_user_list_fe
Added user sign up view
2 parents 3666728 + 274f8fe commit 2985017

File tree

15 files changed

+186
-22
lines changed

15 files changed

+186
-22
lines changed

Contributors.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Contributors
2+
3+
## This project is a result of the hard work of the following volunteers

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ ChitChat is a Python-powered Social Network.
44

55
## Installation
66

7+
### Backend
8+
79
1. cd into backend folder
810
```cd backend```
911

@@ -38,6 +40,17 @@ To run this project do the following:
3840
```python manage.py runserver```
3941

4042

43+
### FrontEnd
44+
1. cd into the frontend folder
45+
```cd frontend```
46+
47+
2. Install dependencies
48+
```npm install```
49+
50+
3. Run the development server
51+
```npm run dev```
52+
53+
4154

4255
## Contributing
4356

backend/accounts/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ class CustomUserAdmin(UserAdmin):
2121

2222
admin.site.register(CustomUser, CustomUserAdmin)
2323
admin.site.unregister(Group)
24+
admin.site.register(Profile)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 4.2.4 on 2024-01-26 20:49
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("accounts", "0002_profile"),
9+
]
10+
11+
operations = [
12+
migrations.AlterField(
13+
model_name="profile",
14+
name="follows",
15+
field=models.ManyToManyField(
16+
blank=True, related_name="followers", to="accounts.profile"
17+
),
18+
),
19+
]

backend/accounts/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ class Profile(models.Model):
1717

1818
def __str__(self):
1919
return f" {self.user.username} - Profile"
20+
21+
class Meta:
22+
ordering = ["-pk"]

backend/api/serializers.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,34 @@
22
from rest_framework import serializers
33

44

5-
class UserSerializer(serializers.HyperlinkedModelSerializer):
5+
class UserSerializer(serializers.ModelSerializer):
6+
class Meta:
7+
model = CustomUser
8+
# lookup_field = "pk"
9+
exclude = ["password", "groups", "user_permissions", "is_staff", "is_active"]
10+
11+
12+
class FollowerSerializer(serializers.ModelSerializer):
613
url = serializers.HyperlinkedIdentityField(
7-
view_name="api:user-detail",
8-
lookup_field="pk",
14+
view_name="api:profile-detail", lookup_field="pk"
915
)
16+
username = serializers.SerializerMethodField()
17+
18+
def get_username(self, obj):
19+
return f"{obj.user.username}"
1020

1121
class Meta:
12-
model = CustomUser
13-
lookup_field = "pk"
14-
fields = [
15-
"username",
16-
"email",
17-
"pk",
18-
"url",
19-
]
22+
model = Profile
23+
fields = ["username", "url"]
2024

2125

2226
class ProfileSerializer(serializers.ModelSerializer):
2327
url = serializers.HyperlinkedIdentityField(
2428
view_name="api:profile-detail", lookup_field="pk"
2529
)
2630
user = serializers.SerializerMethodField()
27-
follows = serializers.HyperlinkedIdentityField(
28-
view_name="api:profile-detail", lookup_field="pk", many=True
29-
)
30-
followers = serializers.HyperlinkedIdentityField(
31-
view_name="api:profile-detail", lookup_field="pk", many=True
32-
)
31+
follows = FollowerSerializer(many=True, read_only=True)
32+
followers = FollowerSerializer(many=True, read_only=True)
3333

3434
def get_user(self, obj):
3535
return obj.user.username

backend/api/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
path("api/users/", views.CustomUserList.as_view(), name="users"),
1111
path("api/users/<int:pk>/", views.CustomUserDetail.as_view(), name="user-detail"),
1212
path("api/profiles/", views.ProfileList.as_view(), name="profiles"),
13+
path("api/signup", views.SignUpView.as_view(), name="signup-view"),
1314
path(
1415
"api/profiles/<int:pk>/", views.ProfileDetail.as_view(), name="profile-detail"
1516
),

backend/api/views.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ProfileList(generics.ListAPIView):
5858

5959
queryset = Profile.objects.all()
6060
serializer_class = ProfileSerializer
61-
permission_classes = [permissions.IsAuthenticated]
61+
# permission_classes = [permissions.IsAuthenticated]
6262

6363

6464
class ProfileDetail(generics.RetrieveAPIView):
@@ -116,3 +116,18 @@ def post(self, request, username):
116116
{"message": f"you're no longer following {profile.user.username}"},
117117
status=200,
118118
)
119+
120+
121+
class SignUpView(APIView):
122+
"""
123+
username: str
124+
email: str
125+
password: str
126+
"""
127+
128+
def post(self, request):
129+
serializer = UserSerializer(data=request.data)
130+
if serializer.is_valid():
131+
serializer.save()
132+
return Response(serializer.data, status=201)
133+
return Response(serializer.errors, status=400)

backend/chitchat/settings.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"django.contrib.staticfiles",
4343
# Third Party apps
4444
"rest_framework",
45+
"corsheaders",
4546
# Local apps
4647
"api",
4748
"accounts",
@@ -51,6 +52,7 @@
5152

5253
MIDDLEWARE = [
5354
"django.middleware.security.SecurityMiddleware",
55+
"corsheaders.middleware.CorsMiddleware", # CORS
5456
"django.contrib.sessions.middleware.SessionMiddleware",
5557
"django.middleware.common.CommonMiddleware",
5658
"django.middleware.csrf.CsrfViewMiddleware",
@@ -139,6 +141,11 @@
139141
# Use Django's standard `django.contrib.auth` permissions,
140142
# or allow read-only access for unauthenticated users.
141143
"DEFAULT_PERMISSION_CLASSES": [
142-
"rest_framework.permissions.IsAuthenticated",
144+
# "rest_framework.permissions.IsAuthenticated",
143145
]
144146
}
147+
148+
149+
CORS_ALLOWED_ORIGINS = [
150+
"http://localhost:5173",
151+
]

backend/db.sqlite3.bak

152 KB
Binary file not shown.

0 commit comments

Comments
 (0)