mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-23 06:49:37 +02:00
feat: add num_cities field to RegionSerializer, update RegionCard to display city count, and enhance CSRF token handling
This commit is contained in:
parent
abe870506f
commit
22790ae7c0
6 changed files with 30 additions and 7 deletions
|
@ -33,10 +33,14 @@ class CountrySerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
|
|
||||||
class RegionSerializer(serializers.ModelSerializer):
|
class RegionSerializer(serializers.ModelSerializer):
|
||||||
|
num_cities = serializers.SerializerMethodField()
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Region
|
model = Region
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
read_only_fields = ['id', 'name', 'country', 'longitude', 'latitude']
|
read_only_fields = ['id', 'name', 'country', 'longitude', 'latitude', 'num_cities']
|
||||||
|
|
||||||
|
def get_num_cities(self, obj):
|
||||||
|
return City.objects.filter(region=obj).count()
|
||||||
|
|
||||||
class CitySerializer(serializers.ModelSerializer):
|
class CitySerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { sequence } from '@sveltejs/kit/hooks';
|
||||||
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
const PUBLIC_SERVER_URL = process.env['PUBLIC_SERVER_URL'];
|
||||||
|
|
||||||
export const authHook: Handle = async ({ event, resolve }) => {
|
export const authHook: Handle = async ({ event, resolve }) => {
|
||||||
|
event.cookies.delete('csrftoken', { path: '/' });
|
||||||
try {
|
try {
|
||||||
let sessionid = event.cookies.get('sessionid');
|
let sessionid = event.cookies.get('sessionid');
|
||||||
|
|
||||||
|
|
|
@ -54,7 +54,14 @@
|
||||||
>
|
>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h2 class="card-title overflow-ellipsis">{region.name}</h2>
|
<h2 class="card-title overflow-ellipsis">{region.name}</h2>
|
||||||
<p>{region.id}</p>
|
<div>
|
||||||
|
<div class="badge badge-primary">
|
||||||
|
<p>{region.id}</p>
|
||||||
|
</div>
|
||||||
|
<div class="badge badge-neutral-300">
|
||||||
|
<p>{region.num_cities} {$t('worldtravel.cities')}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="card-actions justify-end">
|
<div class="card-actions justify-end">
|
||||||
<!-- <button class="btn btn-info" on:click={moreInfo}>More Info</button> -->
|
<!-- <button class="btn btn-info" on:click={moreInfo}>More Info</button> -->
|
||||||
{#if !visited}
|
{#if !visited}
|
||||||
|
@ -65,9 +72,11 @@
|
||||||
{#if visited}
|
{#if visited}
|
||||||
<button class="btn btn-warning" on:click={removeVisit}>{$t('adventures.remove')}</button>
|
<button class="btn btn-warning" on:click={removeVisit}>{$t('adventures.remove')}</button>
|
||||||
{/if}
|
{/if}
|
||||||
<button class="btn btn-neutral-300" on:click={goToCity}
|
{#if region.num_cities > 0}
|
||||||
>{$t('worldtravel.view_cities')}</button
|
<button class="btn btn-neutral-300" on:click={goToCity}
|
||||||
>
|
>{$t('worldtravel.view_cities')}</button
|
||||||
|
>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -62,6 +62,7 @@ export type Region = {
|
||||||
country: string;
|
country: string;
|
||||||
latitude: number;
|
latitude: number;
|
||||||
longitude: number;
|
longitude: number;
|
||||||
|
num_cities: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type City = {
|
export type City = {
|
||||||
|
|
|
@ -287,7 +287,8 @@
|
||||||
"marked_visited": "marked as visited",
|
"marked_visited": "marked as visited",
|
||||||
"regions_in": "Regions in",
|
"regions_in": "Regions in",
|
||||||
"region_stats": "Region Stats",
|
"region_stats": "Region Stats",
|
||||||
"all_visited": "You've visited all regions in"
|
"all_visited": "You've visited all regions in",
|
||||||
|
"cities": "cities"
|
||||||
},
|
},
|
||||||
"auth": {
|
"auth": {
|
||||||
"username": "Username",
|
"username": "Username",
|
||||||
|
|
|
@ -53,18 +53,25 @@ async function handleRequest(
|
||||||
|
|
||||||
const headers = new Headers(request.headers);
|
const headers = new Headers(request.headers);
|
||||||
|
|
||||||
|
// Delete existing csrf cookie by setting an expired date
|
||||||
|
cookies.delete('csrftoken', { path: '/' });
|
||||||
|
|
||||||
|
// Generate a new csrf token (using your existing fetchCSRFToken function)
|
||||||
const csrfToken = await fetchCSRFToken();
|
const csrfToken = await fetchCSRFToken();
|
||||||
if (!csrfToken) {
|
if (!csrfToken) {
|
||||||
return json({ error: 'CSRF token is missing or invalid' }, { status: 400 });
|
return json({ error: 'CSRF token is missing or invalid' }, { status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the new csrf token in both headers and cookies
|
||||||
|
const cookieHeader = `csrftoken=${csrfToken}; Path=/; HttpOnly; SameSite=Lax`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await fetch(targetUrl, {
|
const response = await fetch(targetUrl, {
|
||||||
method: request.method,
|
method: request.method,
|
||||||
headers: {
|
headers: {
|
||||||
...Object.fromEntries(headers),
|
...Object.fromEntries(headers),
|
||||||
'X-CSRFToken': csrfToken,
|
'X-CSRFToken': csrfToken,
|
||||||
Cookie: `csrftoken=${csrfToken}`
|
Cookie: cookieHeader
|
||||||
},
|
},
|
||||||
body:
|
body:
|
||||||
request.method !== 'GET' && request.method !== 'HEAD' ? await request.text() : undefined,
|
request.method !== 'GET' && request.method !== 'HEAD' ? await request.text() : undefined,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue