Skip to content

Commit

Permalink
Fixed views.py and authentication logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike014 committed Jan 19, 2025
1 parent dc96bec commit b96a5f9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 41 deletions.
1 change: 1 addition & 0 deletions movie_ranker/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

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

Expand Down
30 changes: 13 additions & 17 deletions movies/templates/movies/subscribe.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,29 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Subscribe - Movie Ranker</title>
<title>Login - Movie Ranker</title>
</head>
<body>
<h1>Subscribe to Premium</h1>
<p>Upgrade to premium to unlock advanced features and unlimited access to movies.</p>
<h1>Login</h1>
<p>Please log in to access your account.</p>

<!-- Navigation Links -->
<nav>
<!-- <nav>
<ul>
<li><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'user_profile' %}">User Profile</a></li>
</ul>
</nav>
</nav> -->

<form method="post" action="{% url 'subscribe' %}">
{% csrf_token %}
<button type="submit">Subscribe Now</button>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<br>
<button type="submit">Login</button>
</form>
</body>
</html>

<!-- Recap:
This HTML file represents the subscription page for the Movie Ranker application. It includes:
A title and heading indicating that this is the subscription page.
A paragraph explaining the benefits of upgrading to a premium subscription,
such as unlocking advanced features and unlimited access to movies.
A form that allows the user to subscribe to the premium service.
The form uses the POST method and includes a CSRF token for security.
When the form is submitted, it triggers the subscribe view. -->
</html>
21 changes: 12 additions & 9 deletions movies/templates/movies/user_profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,31 @@
<h1>User Profile</h1>
<p>Welcome, {{ user.username }}!</p>

<!-- Display user information if authenticated -->
{% if user.is_authenticated %}
<p>Username: {{ user.username }}, First name: {{ user.first_name }}, Last name: {{ user.last_name }}</p>
{% endif %}

<!-- Navigation Links -->
<nav>
<ul>
<li><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'subscribe' %}">Subscribe</a></li>
{% if user.is_authenticated %}
<li><a href="{% url 'logout' %}">Logout</a></li>
{% else %}
<li><a href="{% url 'login' %}">Login</a></li>
{% endif %}
</ul>
</nav>

<h2>Premium Subscription</h2>
<!-- <h2>Premium Subscription</h2>
<p>Upgrade to premium to unlock advanced features and unlimited access to movies.</p>
<form method="post" action="{% url 'subscribe' %}">
{% csrf_token %}
<button type="submit">Subscribe to Premium</button>
</form>
</form> -->
</body>
</html>

<!-- Recap:
This HTML file represents the user profile page for the Movie Ranker application. It includes:

A welcome message displaying the username of the logged-in user.
A section promoting the premium subscription, highlighting the benefits of upgrading to premium.
A form that allows the user to subscribe to the premium service.
The form uses the POST method and includes a CSRF token for security.
When the form is submitted, it triggers the subscribe view. -->
4 changes: 4 additions & 0 deletions movies/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
MovieListView,
SubscribeView,
UserProfileView,
LoginView,
LogoutView,
)

urlpatterns = [
Expand All @@ -32,6 +34,8 @@
path("movies/<int:movie_id>/", MovieDetailView.as_view(), name="movie_details"),
path("profile/", UserProfileView.as_view(), name="user_profile"),
path("subscribe/", SubscribeView.as_view(), name="subscribe"),
path("login/", LoginView.as_view(), name="login"),
path("logout/", LogoutView.as_view(), name="logout"),
]


Expand Down
43 changes: 28 additions & 15 deletions movies/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
SubscribeView(View): View to handle user subscriptions.
HomeView(View): View to display the home page.
"""

from django.contrib.auth import authenticate, login
from django.shortcuts import redirect, render
from django.views import View

Expand All @@ -29,6 +29,8 @@

class MovieListView(View):
def get(self, request):
if not request.user.is_authenticated:
return redirect('subscribe')
"""
View to display a list of movies filtered by genre.
Expand Down Expand Up @@ -69,6 +71,8 @@ def get(self, request):

class MovieDetailView(View):
def get(self, request, movie_id):
if not request.user.is_authenticated:
return redirect('subscribe')
"""
View to display the details of a specific movie, including its overview.
Expand Down Expand Up @@ -115,23 +119,10 @@ def get(self, request):
"""
return render(request, "movies/user_profile.html")


class SubscribeView(View):
def post(self, request):
"""
View to handle user subscriptions.
Args:
request (HttpRequest): The request object.
Returns:
HttpResponse: The response object with the rendered template.
"""
return redirect("user_profile")

def get(self, request):
"""
View to display the subscription form.
View to handle user subscriptions.
Args:
request (HttpRequest): The request object.
Expand All @@ -141,9 +132,20 @@ def get(self, request):
"""
return render(request, "movies/subscribe.html")

def post(self, request):
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('home')
else:
return render(request, 'movies/subscribe.html', {'error': 'Invalid credentials'})

class HomeView(View):
def get(self, request):
if not request.user.is_authenticated:
return redirect('subscribe')
"""
View to display the home page with a list of movies.
Expand Down Expand Up @@ -189,3 +191,14 @@ def get(self, request):
"sponsored_movies": sponsored_movies,
},
)

class LoginView(View):
def get(self, request):
return render(request, "movies/subscribe.html")

def post(self, request):
return redirect("home")

class LogoutView(View):
def get(self, request):
return redirect("subscribe")

0 comments on commit b96a5f9

Please sign in to comment.