diff --git a/backend/backend/__pycache__/settings.cpython-310.pyc b/backend/backend/__pycache__/settings.cpython-310.pyc index dba1f24..8254b89 100644 Binary files a/backend/backend/__pycache__/settings.cpython-310.pyc and b/backend/backend/__pycache__/settings.cpython-310.pyc differ diff --git a/backend/backend/__pycache__/urls.cpython-310.pyc b/backend/backend/__pycache__/urls.cpython-310.pyc index e223a4d..8af5a0f 100644 Binary files a/backend/backend/__pycache__/urls.cpython-310.pyc and b/backend/backend/__pycache__/urls.cpython-310.pyc differ diff --git a/backend/backend/api/__pycache__/urls.cpython-310.pyc b/backend/backend/api/__pycache__/urls.cpython-310.pyc index cb9d521..adee24c 100644 Binary files a/backend/backend/api/__pycache__/urls.cpython-310.pyc and b/backend/backend/api/__pycache__/urls.cpython-310.pyc differ diff --git a/backend/backend/api/urls.py b/backend/backend/api/urls.py index 2e9bcbe..9bbb012 100644 --- a/backend/backend/api/urls.py +++ b/backend/backend/api/urls.py @@ -1,10 +1,12 @@ from rest_framework.routers import DefaultRouter from journaling.api.urls import journal_router +from habits.api.urls import habit_router from django.urls import path, include router = DefaultRouter() router.registry.extend(journal_router.registry) +router.registry.extend(habit_router.registry) urlpatterns = [ path('', include(router.urls)), diff --git a/backend/backend/settings.py b/backend/backend/settings.py index eddce99..d519688 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -11,10 +11,19 @@ """ from pathlib import Path +import json # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent +# Config File for the entire app +config_file = BASE_DIR.parent / "config.json" + +with config_file.open() as json_file: + config_data = json.load(json_file) + HOST_ADDRESS = config_data.get('HOST_ADDRESS', 'localhost') + + # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ @@ -38,13 +47,14 @@ 'rest_framework', 'corsheaders', 'journaling.apps.JournalingConfig', + 'habits.apps.HabitsConfig', ] CORS_ALLOWED_ORIGINS = [ 'http://localhost:5173', 'http://localhost:8081', - 'exp://192.168.254.26:8081', - 'http://192.168.254.26:8081', + f'exp://{HOST_ADDRESS}:8081', + f'http://{HOST_ADDRESS}:8081', ] MIDDLEWARE = [ diff --git a/backend/backend/urls.py b/backend/backend/urls.py index 5ec74d9..3dffe64 100644 --- a/backend/backend/urls.py +++ b/backend/backend/urls.py @@ -19,5 +19,6 @@ urlpatterns = [ path('admin/', admin.site.urls), - path('api/', include('journaling.api.urls')), + path('api/', include('backend.api.urls')), + path('api/', include('journaling.api.urls')) ] diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 index 275f3a4..6076570 100644 Binary files a/backend/db.sqlite3 and b/backend/db.sqlite3 differ diff --git a/backend/habits/__init__.py b/backend/habits/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/habits/admin.py b/backend/habits/admin.py new file mode 100644 index 0000000..40ffc01 --- /dev/null +++ b/backend/habits/admin.py @@ -0,0 +1,5 @@ +from django.contrib import admin +from .models import Habit + +# Register your models here. +admin.site.register(Habit) \ No newline at end of file diff --git a/backend/habits/api/serializers.py b/backend/habits/api/serializers.py new file mode 100644 index 0000000..807b5f4 --- /dev/null +++ b/backend/habits/api/serializers.py @@ -0,0 +1,13 @@ +from rest_framework import serializers +from ..models import Habit + +class HabitSerializer(serializers.ModelSerializer): + class Meta: + model = Habit + fields = ['id', 'name', 'description', 'good_habit', 'date_started', 'date_last_checked'] + + +class HabitCreateUpdateSerializer(serializers.ModelSerializer): + class Meta: + model = Habit + fields = ['name', 'description', 'good_habit'] \ No newline at end of file diff --git a/backend/habits/api/urls.py b/backend/habits/api/urls.py new file mode 100644 index 0000000..42cc8ca --- /dev/null +++ b/backend/habits/api/urls.py @@ -0,0 +1,10 @@ +from django.urls import path, include +from rest_framework.routers import DefaultRouter +from .views import HabitView + +habit_router = DefaultRouter() +habit_router.register(r'habit', HabitView) + +urlpatterns = [ + path('', include(habit_router.urls)), +] diff --git a/backend/habits/api/views.py b/backend/habits/api/views.py new file mode 100644 index 0000000..6f162f1 --- /dev/null +++ b/backend/habits/api/views.py @@ -0,0 +1,18 @@ +from rest_framework import viewsets, status +from ..models import Habit +from .serializers import HabitSerializer, HabitCreateUpdateSerializer +from rest_framework.response import Response + +# Create your views here. + +class HabitView(viewsets.ModelViewSet): + queryset = Habit.objects.all() + + def get_serializer_class(self): + if self.action in ['create', 'update', 'partial_update']: + return HabitCreateUpdateSerializer + else: + return HabitSerializer + + def create(self, request, *args, **kwargs): + return super().create(request, *args, **kwargs) diff --git a/backend/habits/apps.py b/backend/habits/apps.py new file mode 100644 index 0000000..0017368 --- /dev/null +++ b/backend/habits/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class HabitsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'habits' diff --git a/backend/habits/migrations/0001_initial.py b/backend/habits/migrations/0001_initial.py new file mode 100644 index 0000000..db9daa5 --- /dev/null +++ b/backend/habits/migrations/0001_initial.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.7 on 2023-12-13 18:21 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Habit', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=25)), + ('description', models.CharField(max_length=125)), + ('good_habit', models.BooleanField(default=True)), + ('date_started', models.DateTimeField(auto_now_add=True)), + ('date_last_checked', models.DateTimeField(auto_now=True)), + ], + ), + ] diff --git a/backend/habits/migrations/__init__.py b/backend/habits/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/habits/models.py b/backend/habits/models.py new file mode 100644 index 0000000..4134a8a --- /dev/null +++ b/backend/habits/models.py @@ -0,0 +1,11 @@ +from django.db import models + +# Create your models here. + + +class Habit(models.Model): + name = models.CharField(max_length = 25) + description = models.CharField(max_length = 125) + good_habit = models.BooleanField(default = True) + date_started = models.DateTimeField(auto_now_add=True) + date_last_checked = models.DateTimeField(auto_now=True) \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..efe4883 --- /dev/null +++ b/config.json @@ -0,0 +1,4 @@ + +{ + "HOST_ADDRESS": "" +} \ No newline at end of file diff --git a/frontendMobile/src/Api/apiEndpoints.js b/frontendMobile/src/Api/apiEndpoints.js index 31a0038..d39737c 100644 --- a/frontendMobile/src/Api/apiEndpoints.js +++ b/frontendMobile/src/Api/apiEndpoints.js @@ -1,7 +1,8 @@ // This is where all the API endpoints are handled (using a REST framework) // I made sure to put them all in one file to make frontend dev and backend dev communication a lot easier! -DOMAIN = 'http://192.168.254.26:8000' // Put the server to your backend here! +HOST = "" // Put the IP here! +DOMAIN = `http://${HOST}:8000/` const endpoints = { getJournal: `${DOMAIN}/api/journal`, @@ -10,6 +11,8 @@ const endpoints = { journal: `${DOMAIN}/api/create_journal/`, journalPage: `${DOMAIN}/api/create_journal_page/`, + getHabit: `${DOMAIN}/api/habit/`, + postApiResponse(endpoint, data, handleOk = () => {}, handleError = () => {}) { console.log("Sender's Data: ", data) @@ -80,4 +83,4 @@ const endpoints = { }; -export default endpoints; \ No newline at end of file +export default endpoints; diff --git a/frontendMobile/src/Components/Habits.js b/frontendMobile/src/Components/Habits.js new file mode 100644 index 0000000..c288009 --- /dev/null +++ b/frontendMobile/src/Components/Habits.js @@ -0,0 +1,48 @@ +import React, { useState, useCallback } from 'react'; +import { View, ScrollView, Text, Button, StyleSheet } from 'react-native'; +import { mainStyle } from '../Styles/Styles'; +import { useFocusEffect } from '@react-navigation/native' +import dateTimeFormat from '../Utils/DateTimeFormat'; +import endpoints from '../Api/apiEndpoints'; + +const s = StyleSheet.create(mainStyle); + +const Habits = ({sortBy, navigation}) => { + const getHabits = endpoints.getHabit; + + const [habits, setHabits] = useState([]); + useFocusEffect(useCallback(() => {endpoints.getAPIResponse(getHabits, setHabits)}, [])); + + const sortedHabits = habits.slice().sort(sortBy); + + const controlStreak = (goodHabit) => { + if (goodHabit) { + console.log("Habit is good!"); + } else { + console.log("Habit is bad!"); + } + } + + return ( + + {sortedHabits.map((habit) => ( + + {habit.name} + + {habit.description} + Streak: (coming soon) +