Skip to content

Commit

Permalink
chore(pep8 & comments): remove unnecessary comments and reformat file…
Browse files Browse the repository at this point in the history
…s to be pep8 friendly.
  • Loading branch information
erfan-rfmhr committed Mar 26, 2024
1 parent 053b516 commit fa6a627
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 101 deletions.
36 changes: 15 additions & 21 deletions api_v1/views.py
Original file line number Diff line number Diff line change
@@ -1,70 +1,67 @@
from django.http import JsonResponse
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.pagination import PageNumberPagination
from .serializers import UserSerialized, PostSerialized, FollowerListSerialized, FollowingListSerialized, TopicSerialized
from rest_framework.permissions import IsAuthenticated

from relation.models import Relation
from utils.base_utils import get_random_topics
from user.models import User
from utils.base_utils import get_random_topics
from .serializers import UserSerialized, PostSerialized, FollowerListSerialized, FollowingListSerialized, \
TopicSerialized



# Create your views here.
def index(request):
return JsonResponse({'message': 'ZED API v1'})


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def user_profile(request):

user = request.user
user = UserSerialized(user)
return JsonResponse(user.data, safe=False)


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def user_posts(request):

user = request.user
posts = user.posts.all().order_by('-created_time')

paginator = PageNumberPagination()
paginator.page_size = 10
paginated_posts = paginator.paginate_queryset(posts, request)
result = PostSerialized(paginated_posts, many=True)
result = PostSerialized(paginated_posts, many=True)
return paginator.get_paginated_response(result.data)


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def user_followerlist(request):

user = request.user

followers = Relation.objects.filter(following=user)

followers = FollowerListSerialized(followers, many=True)

return JsonResponse(followers.data, safe=False)


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def user_followinglist(request):

user = request.user

following = Relation.objects.filter(follower=user)

following = FollowingListSerialized(following, many=True)

return JsonResponse(following.data, safe=False)


@api_view(['GET'])
@permission_classes([IsAuthenticated])
def user_suggestions(request):

current_user = request.user

topics = get_random_topics()
Expand All @@ -76,6 +73,3 @@ def user_suggestions(request):
suggestions = UserSerialized(random_suggestions, many=True)

return JsonResponse({'topics': serializedTopics.data, 'suggests': suggestions.data}, safe=False)



33 changes: 5 additions & 28 deletions config/settings.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
"""
Django settings for config project.
Generated by 'django-admin startproject' using Django 5.0.3.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""
import os
from datetime import timedelta
from pathlib import Path

from decouple import config
from datetime import timedelta

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY', False)

Expand All @@ -42,7 +29,6 @@
'debug_toolbar',
]


INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
Expand Down Expand Up @@ -104,9 +90,7 @@

WSGI_APPLICATION = 'config.wsgi.application'


# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
'default': {
Expand All @@ -115,9 +99,7 @@
}
}


# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
Expand All @@ -134,9 +116,7 @@
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

Expand All @@ -146,9 +126,7 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = 'static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
Expand All @@ -157,18 +135,17 @@
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.SessionAuthentication',
],
}

SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(days=1),
'REFRESH_TOKEN_LIFETIME': timedelta(days=30),
}
}
23 changes: 3 additions & 20 deletions config/urls.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
"""
URL configuration for config project.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('content.urls')),
path('', include('engagement.urls')),
path('', include('relation.urls')),
path('', include('user.urls')),
path('api/v1/',include('api_v1.urls')),
path('api/v1/', include('api_v1.urls')),

path('__debug__/', include('debug_toolbar.urls')),
]
Expand Down
1 change: 1 addition & 0 deletions content/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin

from .models import Topic, Post, Comment, Repost

admin.site.register(Topic)
Expand Down
1 change: 0 additions & 1 deletion content/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from django.contrib.auth import get_user_model
from django.db import models
from django.db.models import Q
from django.utils.translation import gettext_lazy as _

from utils.models import BaseModel
Expand Down
3 changes: 1 addition & 2 deletions content/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from .views import *


app_name = 'content'

urlpatterns = [
Expand All @@ -15,5 +14,5 @@
path('profile/<str:other_user_username>/', other_user_profile, name='other_user_profile'),
path('settings/', settings, name='settings'),
path('search/<str:query>/', search, name='search'),
path("unfollow/<str:followed_user_username>/",ActionUnFollowView.as_view(),name="actionunfollow"),
path("unfollow/<str:followed_user_username>/", ActionUnFollowView.as_view(), name="actionunfollow"),
]
12 changes: 4 additions & 8 deletions content/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from urllib.parse import unquote

from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.core.exceptions import ObjectDoesNotExist
Expand All @@ -7,13 +9,11 @@
from django.views import View

from content.models import Post, Comment, Topic
from engagement.models import LikedPost, LikeNotification
from engagement.models import LikedPost
from relation.models import Relation
from utils.base_utils import left_nav_post_form_processing, mobile_post_form_processing, get_random_topics, \
get_random_follow_suggestions, toggle_like_post, toggle_like_comment, format_date
from utils.session_utils import get_current_user
from urllib.parse import unquote


User = get_user_model()

Expand Down Expand Up @@ -218,9 +218,6 @@ def profile(request):
for post in posts:
post.is_liked = LikedPost.objects.filter(post=post, liker=current_user).exists()




if request.POST.get('profile_post_comment_submit_btn'):
current_post_id = request.POST.get('hidden_post_id')
return HttpResponseRedirect('/post/' + str(current_post_id) + '/')
Expand All @@ -242,7 +239,7 @@ def profile(request):
'posts_count': len(posts),
'followers_count': len(followers),
'followings_count': len(followings),

}

return render(request, 'profile/profile.html', data)
Expand Down Expand Up @@ -270,7 +267,6 @@ def other_user_profile(request, other_user_username):
for post in posts:
post.is_liked = LikedPost.objects.filter(post=post, liker=current_user).exists()


follow_current_user = Relation.objects.filter(
following=current_user,
follower=other_user,
Expand Down
1 change: 0 additions & 1 deletion engagement/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from .views import *


app_name = 'engagement'

urlpatterns = [
Expand Down
1 change: 1 addition & 0 deletions relation/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin

from .models import Relation

admin.site.register(Relation)
3 changes: 0 additions & 3 deletions relation/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
from django.urls import path


app_name = 'relation'

urlpatterns = [
Expand Down
1 change: 1 addition & 0 deletions user/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin

from .models import User

admin.site.register(User)
9 changes: 5 additions & 4 deletions user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from django.contrib.auth.models import AbstractUser
from django.db import models

from django.utils.translation import gettext_lazy as _


class User(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
profile_photo = models.ImageField(verbose_name=_('user profile photo'), upload_to='profile_photos/', blank=True, null=True)
banner_photo = models.ImageField(verbose_name=_('user banner photo'), upload_to='banner_photos/', blank=True, null=True)
profile_photo = models.ImageField(verbose_name=_('user profile photo'), upload_to='profile_photos/', blank=True,
null=True)
banner_photo = models.ImageField(verbose_name=_('user banner photo'), upload_to='banner_photos/', blank=True,
null=True)
bio = models.TextField(verbose_name=_('user bio'), null=True, blank=True)

class Meta:
Expand All @@ -22,5 +23,5 @@ def __str__(self):
full_name = "%s %s" % (self.first_name, self.last_name)
return full_name.strip()
else:
username = "%s" % (self.username, )
username = "%s" % (self.username,)
return username.strip()
1 change: 0 additions & 1 deletion user/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from .views import *


app_name = 'user'

urlpatterns = [
Expand Down
5 changes: 3 additions & 2 deletions user/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
from django.shortcuts import render, redirect
from django.views import View

from .models import User
Expand All @@ -22,7 +22,8 @@ def post(self, request):
if User.objects.filter(username=username).exists() or User.objects.filter(email=email).exists():
return render(request, self.template_name, {'credentials_taken': True})

user = User.objects.create_user(username=username, email=email, password=password, first_name=first_name, last_name=last_name)
user = User.objects.create_user(username=username, email=email, password=password, first_name=first_name,
last_name=last_name)
login(request, user)
return redirect('/')

Expand Down
2 changes: 1 addition & 1 deletion utils/base_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ def toggle_like_comment(current_user, current_comment):


def format_date(date):
return date.strftime('%b %Y')
return date.strftime('%b %Y')
Loading

0 comments on commit fa6a627

Please sign in to comment.