Skip to content

Commit

Permalink
Merge pull request #65 from wildjames/dev
Browse files Browse the repository at this point in the history
Help page integration
  • Loading branch information
wildjames authored Nov 4, 2023
2 parents 8ecad59 + 95b1386 commit 06a072e
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.env

.DS_STORE
.DS_STORE
dump.rdb
9 changes: 8 additions & 1 deletion todoqueue_backend/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class CustomUserAdmin(UserAdmin):
"date_joined",
"is_active",
"is_staff",
"has_logged_in",
"brownie_point_credit",
"brownie_point_debit",
)
Expand All @@ -35,7 +36,13 @@ class CustomUserAdmin(UserAdmin):
("Dates", {"fields": ("last_login", "date_joined")}),
(
"Additional Info",
{"fields": ("brownie_point_credit", "brownie_point_debit")},
{
"fields": (
"brownie_point_credit",
"brownie_point_debit",
"has_logged_in",
)
},
),
)
add_fieldsets = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.5 on 2023-11-04 09:32

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('accounts', '0003_alter_customuser_username'),
]

operations = [
migrations.AddField(
model_name='customuser',
name='has_logged_in',
field=models.BooleanField(default=False),
),
]
2 changes: 2 additions & 0 deletions todoqueue_backend/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class CustomUser(AbstractBaseUser, PermissionsMixin):
date_joined = models.DateTimeField(default=timezone.now)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)

has_logged_in = models.BooleanField(default=False)

brownie_point_credit = models.JSONField(default=dict)
brownie_point_debit = models.JSONField(default=dict)
Expand Down
1 change: 1 addition & 0 deletions todoqueue_backend/accounts/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Meta:
"email",
"username",
"date_joined",
"has_logged_in",
"brownie_point_credit",
"brownie_point_debit",
)
Expand Down
2 changes: 2 additions & 0 deletions todoqueue_backend/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework.routers import DefaultRouter
from .views import (
CustomUserViewSet,
GetUserData,
AuthView,
LogoutView,
RegisterView,
Expand All @@ -19,6 +20,7 @@
path("auth/", AuthView.as_view(), name="auth"),
path("token/", jwt_views.TokenObtainPairView.as_view(), name="token_obtain_pair"),
path("token/refresh/", jwt_views.TokenRefreshView.as_view(), name="token_refresh"),
path("user_info/", GetUserData.as_view(), name="user_info"),
path("logout/", LogoutView.as_view(), name="logout"),
path("register/", RegisterView.as_view(), name="register"),
path(
Expand Down
13 changes: 13 additions & 0 deletions todoqueue_backend/accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ def check_permissions(self, request):
)


class GetUserData(APIView):
permission_classes = (IsAuthenticated,)

def get(self, request):
"""Return the serialization of the user who authenticated this request"""
logger.info("Getting serialization of a single user")
user = request.user
serializer = CustomUserSerializer(user)
serialized_data = serializer.data
logger.info(f"User data: {serialized_data}")

return Response(serialized_data)

class AuthView(APIView):
permission_classes = (IsAuthenticated,)

Expand Down
22 changes: 18 additions & 4 deletions todoqueue_backend/todoqueue_backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

from logging import getLogger, INFO, DEBUG, basicConfig

basicConfig(level=DEBUG)
logger = getLogger(__name__)


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -31,7 +28,24 @@
SECRET_KEY = config("DJANGO_SECRET", default=os.urandom(32))

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get("DJANGO_DEBUG", "false").lower() == "true"
env_debug = os.environ.get("DJANGO_DEBUG", "false").lower()
if env_debug.isdigit():
env_debug = int(env_debug) == 1
else:
env_debug = env_debug in ["true", "yes"]

logging_level = os.environ.get("DJANGO_LOGGING_LEVEL", "info").lower()

if logging_level.lower() == "debug":
basicConfig(level=DEBUG)
else:
basicConfig(level=INFO)

logger = getLogger(__name__)
logger.info(f"Logging level: {logging_level}")
logger.info(f"Django is using DEBUG = {env_debug}")

DEBUG = env_debug

web_port = config("DJANGO_HOST_PORT", default=8000, cast=int)
logger.info("Whilelisting host for CSRF: {}".format(config("FRONTEND_URL", default=None)))
Expand Down
27 changes: 26 additions & 1 deletion todoqueue_frontend/src/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const logOutUser = async () => {
}


export const fetchUsers = async (selectedHousehold) => {
export const fetchHouseholdUsers = async (selectedHousehold) => {
if (!selectedHousehold) {
console.log("No household selected - skipping get users.");
return null;
Expand Down Expand Up @@ -107,6 +107,31 @@ export const fetchUsers = async (selectedHousehold) => {
};


export const fetchUserData = async () => {
const get_user_url = `${backend_url}/api/user_info/`;

console.log("Getting my user data");
try {
const res = await axios.get(
get_user_url,
{
headers: {
'Content-Type': 'application/json',
}
})
if (res.status !== 200) {
console.log("Failed to get my user data");
return null;
}
console.log("I got my user data!", res.data);
return res.data;
} catch (error) {
console.error("An error occurred while fetching my user data", error);
return null;
}
}


export const forgotPassword = async (email) => {

console.log("Resetting password");
Expand Down
23 changes: 18 additions & 5 deletions todoqueue_frontend/src/components/login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useEffect } from "react"; // Define the Login function.
import "./Login.css";
import "../../utils/buttons.css";

import { loginUser } from "../../api/users";
import { loginUser, fetchUserData } from "../../api/users";
import AlertMessage from "../popups/AlertPopup";
import Spinner from "../spinner/Spinner";

Expand All @@ -26,14 +26,27 @@ const Login = ({ setShowHouseholdSelector }) => {

setShowSpinner(true);
const data = await response;
setShowSpinner(false);

if (data.error) {
setShowSpinner(false);
setLoginError(data.error);
} else if (data.success) {
setShowSpinner(false);
console.log("Login successful. Redirecting to /");
window.location.href = "/";
const res = await fetchUserData();

if (res === null) {
setShowSpinner(false);
setLoginError("");
}

if (res.has_logged_in) {
setShowSpinner(false);
console.log("Login successful. Redirecting to /");
window.location.href = "/";
} else {
setShowSpinner(false);
console.log("This is my first login. Redirecting to help page.");
window.location.href = "/help";
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions todoqueue_frontend/src/components/navbar/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ export function Navigation({ households, selectedHousehold, setSelectedHousehold
)}
</Nav>

<Nav className="me-right navbar-links-container navbar-links-right">
<Nav.Link href="/help" className="navbar-link">Help</Nav.Link>
</Nav>

<Nav className="me-right navbar-links-container navbar-links-right">
{!isAuth && <Nav.Link href="/signup" className="navbar-link">Sign Up</Nav.Link>}
{isAuth ?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect, useState, useRef } from 'react';

import BasePopup from './BasePopup';
import { fetchUsers } from '../../api/users';
import { fetchHouseholdUsers } from '../../api/users';
import { addUserToHousehold, removeUserFromHousehold, fetchSelectedHousehold } from '../../api/households';

import Spinner from '../spinner/Spinner';
Expand All @@ -20,7 +20,7 @@ const HouseholdDetailsPopup = React.forwardRef((props, ref) => {
const updateUsers = async (id) => {
try {
console.log("Fetching users");
const users = await fetchUsers(id);
const users = await fetchHouseholdUsers(id);
if (users === null) {
console.log("Closing popup");
props.closePopup();
Expand Down
4 changes: 2 additions & 2 deletions todoqueue_frontend/src/components/tasks/Tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import CreateScheduledTaskPopup from '../popups/CreateScheduledTaskPopup';
import EditScheduledTaskPopup from '../popups/EditScheduledTaskPopup';

import { fetchTasks } from '../../api/tasks';
import { fetchUsers } from '../../api/users';
import { fetchHouseholdUsers } from '../../api/users';


const Tasks = ({ selectedHousehold, showSelectedHouseholdSelector, setShowHouseholdSelector }) => {
Expand Down Expand Up @@ -168,7 +168,7 @@ const Tasks = ({ selectedHousehold, showSelectedHouseholdSelector, setShowHouseh


const fetchSetUsers = async () => {
const data = await fetchUsers(selectedHousehold);
const data = await fetchHouseholdUsers(selectedHousehold);
if (data === null) {
setUsers([]);
return;
Expand Down

0 comments on commit 06a072e

Please sign in to comment.