From b93e168fa05a8178d6d84611d3122db4ae92a502 Mon Sep 17 00:00:00 2001 From: PureZinc <139257470+PureZinc@users.noreply.github.com> Date: Wed, 13 Dec 2023 13:24:42 -0500 Subject: [PATCH 1/2] Created Database Table: Habit Also made changes to hosting for security. new file: backend/habits/admin.py new file: backend/habits/api/serializers.py new file: backend/habits/api/urls.py new file: backend/habits/api/views.py new file: backend/habits/apps.py new file: backend/habits/migrations/0001_initial.py new file: backend/habits/migrations/__init__.py new file: backend/habits/models.py new file: config.json --- .../__pycache__/settings.cpython-310.pyc | Bin 2543 -> 2757 bytes backend/backend/settings.py | 14 ++++++++-- backend/db.sqlite3 | Bin 155648 -> 155648 bytes backend/habits/__init__.py | 0 backend/habits/admin.py | 5 ++++ backend/habits/api/serializers.py | 7 +++++ backend/habits/api/urls.py | 8 ++++++ backend/habits/api/views.py | 13 +++++++++ backend/habits/apps.py | 6 +++++ backend/habits/migrations/0001_initial.py | 25 ++++++++++++++++++ backend/habits/migrations/__init__.py | 0 backend/habits/models.py | 11 ++++++++ config.json | 4 +++ frontendMobile/src/Api/apiEndpoints.js | 3 ++- 14 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 backend/habits/__init__.py create mode 100644 backend/habits/admin.py create mode 100644 backend/habits/api/serializers.py create mode 100644 backend/habits/api/urls.py create mode 100644 backend/habits/api/views.py create mode 100644 backend/habits/apps.py create mode 100644 backend/habits/migrations/0001_initial.py create mode 100644 backend/habits/migrations/__init__.py create mode 100644 backend/habits/models.py create mode 100644 config.json diff --git a/backend/backend/__pycache__/settings.cpython-310.pyc b/backend/backend/__pycache__/settings.cpython-310.pyc index dba1f24cae9bac2df2ce9cd0a9dd2c24d0877636..d02c497bde5f56d4db4555c9692cf289a8df660e 100644 GIT binary patch delta 589 zcmYjOJ#Q015Z&2bpYP6}!Ex;PoKG;wjZH)o1qlTRh>r+GaeAjh!*^&0V@V$8CXEHX^@$USs`jlhr zXDt2?Q;SVqK^Yh$hKzu?2d_ACh`I0t3dNN?hEZb7dPdK{q)PS-k`6;tnImh=-WSkT zVw6($7%;8UqYNd;7;`+zK4&B(lk8Xk>zivVxb+%<#rZ-PB{4@P<~xk6Q@|OrDMe|O z{lF;mQBd|T>qxw!Jvr?uP`(2=OEXxcxMxy9Dq? zUBEIqRMjSj7N)p3#U&T?@_Aau3ay}{kE!ZfpjBL?tGGnhaG6Al)oWjW-GSP(VQSy+ zAGn=HFYx;l`|-}+-L0+N2d&n`^8Ed_*Y$%iTICPHj$HnuGAwu7FWoR`v$A_o5s*3iW^4nmuS9vXoRIN@(G9b98!u1T zJ8(TUk^VsS(Tb;+4bN}WKM%FT_C$14cqsHIN89FwQP{4i2 z3yb&)K7tP*)*|=-;wF`i1M~mQ%zt2DUbA1(?V0bpOvmT?ReT>X_Azb#hN+uPotH0K zh#?~&egdZ)Ik1?=Vh9iNk@bzffhGyGNTS_kWQ+kFO;gj=v@{cBcL6B6 z#?S-uM^c*s1(>8HrDBuPWk%tM_W)BAQAY1Xl$~jAruiOpC8nu>Ar&#whch~}RKgsU zF;8^5YmHXB)?8hwHCvtba=UR; zIvx!E@g_^MEFdx2w)R0BREz4)E~saFQ3cMASlcb|PO1O^ 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/db.sqlite3 b/backend/db.sqlite3 index 275f3a45ba7005776294fa24f9816e5d53fa1f57..acbd37cd0fc2e51c41e1dc349f6dd021c1db91cf 100644 GIT binary patch delta 713 zcmZoTz}awsbAq(sdjeH!FgdayMreOmj#yy=LgOUn-vu*IP2v!SQ!`|ZQiRV4f9iG!bCl>R#3$QRMG9o!pLx_b@l@Y~7>JTAh z2dMG0Fv>F`>{Hz=6#J=&S%Y76GW!Kj4t{lhMSf|1QDBJe<&$7#W@O~j0F&xqQVmS1 zZq~hjy@6SXaqe{X1V$CsxeN@fQ-E>F%{bjUkuk}sNz#>FTve5^xwIrPDJK;Y#ajRX delta 258 zcmZoTz}awsbAq(s8wLgjH6T_6Vn!g2pQvNZ_-13m5`JcBj-!*=1=j1E;+S1ZoK-HZ5b`1RQ_)qiS;@``^fWL%4lHZQMd$VAI3jbs~`(w;1 zTw#;h9Xx?XuyJh;^IFHstimfanLPj`a)@WMQ0%87W)*(j$?O+^A_jN)HtXKM-oPx# zv~xOp0;39R5(5M4l+BI;+>Fz$6B&~xztiKIz9N~Ce|ljeqxAH%$&61}fUf?teP^p@~Yc 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..5b62165 --- /dev/null +++ b/backend/habits/api/serializers.py @@ -0,0 +1,7 @@ +from rest_framework import serializers +from ..models import Habit + +class HabitSerializer(serializers.Serializer): + class Meta: + model = Habit + fields = ['id','name','description','good_habit','date_started','date_last_checked'] diff --git a/backend/habits/api/urls.py b/backend/habits/api/urls.py new file mode 100644 index 0000000..8fe5817 --- /dev/null +++ b/backend/habits/api/urls.py @@ -0,0 +1,8 @@ +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 = [] diff --git a/backend/habits/api/views.py b/backend/habits/api/views.py new file mode 100644 index 0000000..7d0acb4 --- /dev/null +++ b/backend/habits/api/views.py @@ -0,0 +1,13 @@ +from rest_framework import viewsets, status +from ..models import Habit +from .serializers import HabitSerializer +from rest_framework.response import Response +from rest_framework.views import APIView + +# Create your views here. + +class HabitView(viewsets.ModelViewSet): + queryset = Habit.objects.all() + serializer_class = HabitSerializer + + 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..dcd6ac6 --- /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..618c785 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 = "" +DOMAIN = `http://${HOST}:8000/` // Put the server to your backend here! const endpoints = { getJournal: `${DOMAIN}/api/journal`, From 3b3989a22cfed00ddbfa0de265e2193655776c99 Mon Sep 17 00:00:00 2001 From: PureZinc <139257470+PureZinc@users.noreply.github.com> Date: Mon, 18 Dec 2023 17:03:02 -0500 Subject: [PATCH 2/2] STARTING THE HABITS feature Started working on the new habits feature. Here's what I've done: modified: backend/backend/api/urls.py modified: backend/backend/urls.py modified: backend/habits/api/serializers.py modified: backend/habits/api/urls.py modified: backend/habits/api/views.py modified: config.json (This file is the config file for the app) new file: frontendMobile/src/Components/Habits.js new file: frontendMobile/src/Screens/Habits/CreateHabit.js --- .../__pycache__/settings.cpython-310.pyc | Bin 2757 -> 2759 bytes .../backend/__pycache__/urls.cpython-310.pyc | Bin 998 -> 1030 bytes .../api/__pycache__/urls.cpython-310.pyc | Bin 476 -> 485 bytes backend/backend/api/urls.py | 2 + backend/backend/urls.py | 3 +- backend/db.sqlite3 | Bin 155648 -> 155648 bytes backend/habits/api/serializers.py | 10 +- backend/habits/api/urls.py | 4 +- backend/habits/api/views.py | 11 +- config.json | 2 +- frontendMobile/src/Api/apiEndpoints.js | 8 +- frontendMobile/src/Components/Habits.js | 48 +++++++ .../src/Navigators/HomeStackNavigator.js | 2 + .../src/Screens/Habits/CreateHabit.js | 66 +++++++++ frontendMobile/src/Screens/HomeScreen.js | 27 +++- frontendMobile/src/Styles/Styles.js | 135 +++++++++++++++++- 16 files changed, 298 insertions(+), 20 deletions(-) create mode 100644 frontendMobile/src/Components/Habits.js create mode 100644 frontendMobile/src/Screens/Habits/CreateHabit.js diff --git a/backend/backend/__pycache__/settings.cpython-310.pyc b/backend/backend/__pycache__/settings.cpython-310.pyc index d02c497bde5f56d4db4555c9692cf289a8df660e..8254b8984d6f56dd32e9fb81e0d71178e5ec80d3 100644 GIT binary patch delta 33 ncmX>qdR&w@pO=@50SIn=t4v+9k+*}1mB+(BIArq-CS48yr-lj2 delta 31 lcmX>udQ_A*pO=@50SMaPSEer6$lJlh%;g!dc@~o{2LO#h2*CgV diff --git a/backend/backend/__pycache__/urls.cpython-310.pyc b/backend/backend/__pycache__/urls.cpython-310.pyc index e223a4d36ef60a697661c86b1dbe962f8ca0c66c..8af5a0f67f6ccad70172eecfdbbf573f1a9878ee 100644 GIT binary patch delta 119 zcmaFH-o~Mw&&$ij00hBrYg22O85kaeILLqv$Z-JTVxNiH)?%qFDXgiiDeTRR%}nXc zQ5-28!3>(58w)lv2?``7CTFMSrRXIVWa^a`%l8*4T) ZO}1y=D!>DjWMSYD5MbnC=3(Uc2LNe^4b}hv diff --git a/backend/backend/api/__pycache__/urls.cpython-310.pyc b/backend/backend/api/__pycache__/urls.cpython-310.pyc index cb9d521f324ac18d0bcb10ce0f16b863eb4323e0..adee24c80a3317ab13f5a613fd4526e8a884bb30 100644 GIT binary patch delta 260 zcmcb^{FIqDpO=@50SNBBs!h$B$Scd3Gf`XBhmj$LA%!J}EtfrtJ(nYjgOMSXv6&%? zGnF%oYXNsEa~9V^MxYpk#|q`Kr?94S0L6Hk8Jn5X8KZbp*n$}}*(Ww>sxsc<$w*Af zEQv45FD*$e(qy{DQjl1Zaf>}OFFB_)CG{4ApC;eL1Lj&${9tv(dWi*@dZk4<#YHSY z$KB#i$x6&i&xeSx0!510CUY|ytMP%DAmW#~vsFxJacWUkM$*vt^cp30uZv4As`Ig4W< zBT$Srg(Z~@D8|*y*vyp97{#5!8qA={HgS&T#H%8VJQH7;i`?Q)$x6&i&(|w0$|){l z0h(UKI@z1iIFT2`1QEZAoULL)i&Kk=WBf9cGxBp1i(|qvi%S!8f=fzMGV=q9^0QKt zONwKHOY$>|ON#PyHG)zTlS{N+zy@m~#C5^qx)5>Q$x9fuh4_FL@G$T&3NW%U^D*)< HbNmMYKq*3< 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/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 acbd37cd0fc2e51c41e1dc349f6dd021c1db91cf..6076570a476b0d852f9a84c43421bf3d274f119d 100644 GIT binary patch delta 812 zcmaKpPiWIn9LMvLwlS5y*GlciD)f~S|I{|kdri9}C!?iSMc82ogW>G7vlZE7?F2#S z#G4*edI%Im5f9#U_7G6P-9^xoqA)!OB6uDfqWIFzwhoj$NO&LK=Y8}2Ew4(;tJ3W# z*toOj2-sLT^Ds8*hnoO>Lm~7WT}C%i8|{OeXbK(e#MKwD^!WQp?V?0VQ+gs@K3IOZ;_e>eXues#W|{E>V&HY*zJ z;hTIz&w^2NvY@^wn~D@4vnAv7dufbf;!ON27{oZU@=J^I5_2;1(&Hh_$&vcfjH1n@ z`rAwO8T$*ERrochvoB!u1R8adZ@T7s#@j$4-|6fdz(Rldw)^g9+%CYZ!d^6;{Q^+T zQ9y}(d(m~q`4)`7whIX`o#kic|HiB{dPqtx)C_^-C>R@PdQ^ zNCqgHB+RP`WWwB$r~onnDDEZ6D;@xLkSoZgKoJXNUPX{51&DA+su_?Clazr-f}AYC z1}0fSBo_-W-vb5~J~sw_HNIK=bNSr(o^2NjV0y#rVQgs" + "HOST_ADDRESS": "" } \ No newline at end of file diff --git a/frontendMobile/src/Api/apiEndpoints.js b/frontendMobile/src/Api/apiEndpoints.js index 618c785..d39737c 100644 --- a/frontendMobile/src/Api/apiEndpoints.js +++ b/frontendMobile/src/Api/apiEndpoints.js @@ -1,8 +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! -HOST = "" -DOMAIN = `http://${HOST}:8000/` // Put the server to your backend here! +HOST = "" // Put the IP here! +DOMAIN = `http://${HOST}:8000/` const endpoints = { getJournal: `${DOMAIN}/api/journal`, @@ -11,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) @@ -81,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) +