Skip to content

Commit 6570aea

Browse files
committed
documentation + generate csv script + allocator use env for base url
1 parent aff6ce5 commit 6570aea

File tree

7 files changed

+145
-24
lines changed

7 files changed

+145
-24
lines changed

.gitignore

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ logs
2121
# Local env files
2222
.env
2323
.env.*
24+
*.env
2425
!.env.example
2526

2627
# the database, lol
27-
db/
28+
db/
29+
30+
# generated family csv
31+
families.csv
32+
33+
# db certfile, in case you need to make local connections
34+
certfile.crt

README.md

+27-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Mad3 is a fork of [mad2](https://github.com/icdocsoc/mad2), re-written for the g
66

77
This project is open-source and free to look at. If you wish to change something about the website, you can open a PR and we shall review it.
88

9+
Mad3 is written such that future webmasters (and anyone else) should be able to run it purely with an `.env` file, `docker compose up`, and the allocations script. You should never have to edit the code unless you are bugfixing or modifying features.
10+
911
## Project structure
1012

1113
Although using Nuxt 3, we have opted to use Nuxt 4 in our build. [`app/`](/app/) contains the frontend while the [`hono/`](/hono/) contains the server.
@@ -23,31 +25,49 @@ docker compose build
2325
```
2426

2527
Now run the postgres image.
28+
2629
```bash
2730
# If you want to run it in the background
2831
docker compose start postgres
29-
# You will need to then later run
32+
# You will need to then later run
3033
# docker compose stop postgres
3134

3235
# If you want to run it in the foreground to see the logs
3336
docker compose up postgres
3437
```
3538

3639
Finally, run the website.
40+
3741
```bash
3842
# The --bun is required as bunfigs are broken, as of writing this.
3943
bun run --bun dev
4044
```
4145

46+
We run the website this way rather than using the Docker image as we want hot-reload and other development features.
47+
4248
## Production
4349

44-
Docker images are made available for easy running on prod.
50+
Docker images are made available for easy running on prod. See the local development section for an explanation of how to fill out the environment variables.
51+
52+
To simulate a production run locally, run
53+
54+
```bash
55+
docker compose build
56+
57+
docker compose up
58+
```
59+
60+
Note that published Docker builds are generated from the branch `release/latest`.
4561

46-
## Allocations
62+
## Allocations & more
4763

48-
Run `run.py` in the allocations folder.
64+
Clone the repo locally and run `run.py` in the allocations folder. You can find your auth cookie from the developer tools (CTRL + SHIFT + I) -> Network -> refresh the page and see the Cookie tab of the request.
4965

50-
You can find your auth cookie from the developer tools (CTRL + SHIFT + I) -> Network -> refresh the page and see the Cookie tab of the request.
66+
Once the allocations are done, run `generateCsv.ts` to generate a CSV file that you can import into Excel and print out, for on the day organization. There also exists the admin page to facilitate this, but paper never goes wrong.
67+
68+
There also exists an admin page at `/admin` restricted to webmasters as per the env file, where you can search families, see statistics, and change the state of the website.
69+
70+
Should you intend to use this code outside of an Imperial context, you will need to change the sign in code, callback code, and `isFresherOrParent` function. These are found in [`hono/auth/auth.ts`](/hono/auth/auth.ts) and [`hono/auth/jwt.ts`](/hono/auth/jwt.ts).
5171

5272
## Contributors
5373

@@ -56,5 +76,6 @@ You can find your auth cookie from the developer tools (CTRL + SHIFT + I) -> Net
5676
- [@cybercoder-naj](https://github.com/cybercoder-naj)
5777
- [@Dropheart](https://github.com/Dropheart)
5878

59-
### mad2 Allocator Author
79+
### mad2 Allocator Author
80+
6081
- [@jackpordi](https://github.com/jackpordi)

allocations/allocator/allocate.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ def allocations_request(families, url=LOCAL_URL):
244244

245245
def allocate(auth, dry=True, url=LOCAL_URL, debug=False, max_children=MAX_CHILDREN):
246246
cookies = {"Authorization": auth}
247-
families = requests.get(url + "api/admin/allocations/all-families", cookies=cookies)
248-
freshers = requests.get(url + "api/admin/allocations/all-unallocated-freshers", cookies=cookies)
247+
families = requests.get(url + "/api/admin/allocations/all-families", cookies=cookies)
248+
freshers = requests.get(url + "/api/admin/allocations/all-unallocated-freshers", cookies=cookies)
249249

250250
if debug:
251251
print(json.dumps(families.json(), indent=4))
@@ -275,7 +275,7 @@ def allocate(auth, dry=True, url=LOCAL_URL, debug=False, max_children=MAX_CHILDR
275275
if input("Would you like allocate as per the above? (y/n) ") == 'y':
276276
# Construct allocations request to backend
277277
req = allocations_request(families, url=url)
278-
response = requests.post(url + "api/admin/allocations", json=req, cookies=cookies)
278+
response = requests.post(url + "/api/admin/allocations", json=req, cookies=cookies)
279279
print(response)
280280
else:
281281
print("Allocation cancelled")

allocations/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ rstr
55
scikit-learn
66
matplotlib
77
requests
8+
python-dotenv

allocations/run.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
import os
12
from allocator.allocate import allocate
3+
from dotenv import load_dotenv
4+
5+
load_dotenv('../.env');
6+
7+
BASE_URL = os.getenv('BASE_URL')
28

39
authCookie = input("Please insert your auth cookie:\nThis will only be saved in memory to do the allocations.\nSee the README if you're unsure how to get this.\n")
4-
allocate(authCookie, url="https://family.docsoc.co.uk/")
10+
allocate(authCookie, url=BASE_URL)

generateCsv.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { Student } from './hono/types';
2+
3+
var csv =
4+
'id,parent1,parent1shortcode,parent2,parent2shortcode,child1,child1shortcode,child2,child2shortcode,child3,child3shortcode,child4,child4shortcode\n';
5+
6+
const authCookie = prompt(
7+
"Please insert your auth cookie:\nThis will only be saved in memory to create the CSV.\nSee the README if you're unsure how to get this.\n"
8+
)!;
9+
10+
const req = await fetch(`${process.env.BASE_URL!}/api/admin/all-families`, {
11+
headers: {
12+
Cookie: `Authorization=${authCookie}`
13+
}
14+
})
15+
16+
const allFamilies = await req.json();
17+
18+
for (const family of allFamilies) {
19+
const familyId = family.id;
20+
21+
const kidsString = family.kids
22+
.map((student: Student) => `${student.name},${student.shortcode}`)
23+
.join(',');
24+
25+
csv += `${familyId},${family.parents[0].name},${family.parents[0].shortcode},${family.parents[1].name},${family.parents[1].shortcode},${kidsString}\n`;
26+
}
27+
28+
await Bun.write('families.csv', csv);

hono/README.md

+71-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## API Reference
44

55
> Note that where 400 indicates multiple errors, a relevant error message will be returned by the backend.
6+
> Types can be found in types.ts. This documentation will sometimes use the type definition instead of a type name, for ease of reading.
67
78
# **middleware**
89

@@ -145,18 +146,6 @@ type Response = {
145146
**200** - Returns family.
146147

147148
```ts
148-
type Student = {
149-
shortcode: string;
150-
jmc: boolean;
151-
role: "parent" | "fresher";
152-
completedSurvey: boolean;
153-
name: string | null;
154-
gender: "male" | "female" | "other" | "n/a" | null;
155-
interests: Interests | null;
156-
socials: string[] | null;
157-
aboutMe: string | null;
158-
}
159-
160149
{
161150
parents: [
162151
parent1: Student
@@ -170,7 +159,7 @@ type Student = {
170159

171160
## `GET /state`
172161

173-
**200** - returns JSON with current state.
162+
**200** - Returns JSON with current state.
174163

175164
```ts
176165
{
@@ -189,3 +178,72 @@ type Student = {
189178
**400** - Invalid body.
190179

191180
**200** - Updated state.
181+
182+
## `GET /allocations/all-families` - admin
183+
184+
**200** - Returns array of all families in the format the allocator wants.
185+
186+
```ts
187+
{
188+
_id: number,
189+
parents: {
190+
proposerId: AllocatorParent
191+
proposeeId: AllocatorParent
192+
}
193+
kids: AllocatorFresher[]
194+
hasFemale: boolean,
195+
hasJmc: boolean
196+
}[]
197+
```
198+
199+
## `GET /allocations/all-unallocated-freshers` - admin
200+
201+
**200** - Returns array of all freshers who aren't in a family.
202+
203+
```ts
204+
AllocatorFresher = {
205+
_id: string,
206+
student: AllocatorStudent
207+
interests: Interests
208+
family: number | undefined
209+
}[]
210+
```
211+
212+
## `POST /allocations` - admin
213+
214+
```ts
215+
{
216+
fresher: string,
217+
family: number
218+
}
219+
```
220+
221+
**200** - Successfully allocated students to families.
222+
223+
**400** - Invalid body.
224+
225+
## `GET /stats` - admin
226+
227+
**200** - Returns various stats about the families and freshers.
228+
229+
```ts
230+
{
231+
families: number, // num of marriages
232+
all_parents: number, // non-freshers who signed in at least once
233+
registered_parents: number, // non-freshers who completed the survey
234+
all_freshers: number, // all freshers, due to the seed file
235+
registered_freshers: number // freshers who completed the survey
236+
}
237+
```
238+
239+
## `GET /all-families` - admin
240+
241+
**200** Returns all families. This differs from the allocation routes, as it uses our sensible types.
242+
243+
```ts
244+
{
245+
id: number,
246+
parents: Student[],
247+
kids: Student[]
248+
}[]
249+
```

0 commit comments

Comments
 (0)