Skip to content

Commit 02a5f53

Browse files
committed
Merge remote-tracking branch 'origin/main' into profile_page_new
2 parents 4fbcc74 + ac5c957 commit 02a5f53

File tree

6 files changed

+98
-54
lines changed

6 files changed

+98
-54
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ bun.lockb
5555
package-lock.json
5656
yarn.lock
5757
pnpm-lock.yaml
58+
59+
# image folder
60+
/public/uploaded_images/*
61+
!/public/uploaded_images/

backend/api/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
path("get_ratings_bulk/", foods_views.return_ratings_bulk, name="get_ratings_bulk"),
1313
# Add the new endpoint for image uploads
1414
path("upload_image/", views.upload_image, name="upload_image"),
15+
1516
]

backend/api/views.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
import requests
99
from django.http import JsonResponse
1010

11+
from django.http import JsonResponse
12+
from django.views.decorators.csrf import csrf_exempt
13+
from django.core.files.storage import default_storage
14+
from django.core.files.base import ContentFile
15+
import logging
16+
import os
17+
logger = logging.getLogger(__name__)
18+
19+
1120

1221
from .model_logic.locations.actions import (
1322
get_locations as get_locations_db,
@@ -37,6 +46,7 @@
3746

3847
# Get the list of locations at UCSC and their information
3948
@api_view(["GET"])
49+
@csrf_exempt
4050
def get_locations(request):
4151
# Get the last update time of the locations
4252
last_update: datetime | None = get_last_update_time(task_name="locations")
@@ -187,23 +197,36 @@ def current_logout(request):
187197
return JsonResponse({"message": "User has been logged out"})
188198

189199

190-
@api_view(["POST"])
191-
def upload_image(request):
192-
if request.method == "POST" and request.FILES.get("image"):
193-
# Handle image upload logic here
194-
uploaded_image = request.FILES["image"]
195-
# Process the uploaded image (e.g., save it to a storage location)
196-
# Return a JSON response indicating success
197-
return JsonResponse({"success": True, "message": "Image uploaded successfully"})
198-
else:
199-
# Return a JSON response with an error message if no image is provided or method is not POST
200-
return JsonResponse({"success": False, "message": "Image upload failed"})
201200

202201

203-
# @api_view(["GET"])
204-
# def get_user_rating(request):
205-
# user = get_rating(request.user_ids)
206-
# if(user==None):
207-
# return Response({"message": "No user found"})
208-
# else:
209-
# return Response({"message": "User found: {user}", "user data": user})
202+
@csrf_exempt
203+
def upload_image(request):
204+
try:
205+
if request.method == "POST" and request.FILES.get("image"):
206+
uploaded_image = request.FILES["image"]
207+
208+
# Define the folder path to save the image
209+
folder_path = 'public/uploaded_images'
210+
if not os.path.exists(folder_path):
211+
os.makedirs(folder_path)
212+
213+
# Save the image to the specific folder
214+
file_name = default_storage.save(os.path.join(folder_path, uploaded_image.name), ContentFile(uploaded_image.read()))
215+
file_url = default_storage.url(file_name)
216+
217+
return JsonResponse({
218+
"success": True,
219+
"message": "Image uploaded successfully",
220+
"imageName": uploaded_image.name,
221+
"imageUrl": file_url
222+
})
223+
else:
224+
return JsonResponse({"success": False, "message": "No image provided or incorrect request method"})
225+
except Exception as e:
226+
logger.error(f"Error during image upload: {e}")
227+
return JsonResponse({"success": False, "message": f"Image upload failed: {str(e)}"})
228+
229+
230+
231+
232+

components/food/images.tsx

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ import axios from "axios";
44
import { Food } from "@/interfaces/Food";
55
import { fetchUserInfo } from "@/app/requests"; // Import fetchUserInfo function
66
import "@/components/food/Images.css"; // Import the CSS file
7+
import Image from "next/image";
78

89
interface ImagesProps {
910
food: Food;
1011
}
1112

13+
interface ImageDetails {
14+
imageName: string;
15+
uploadedBy: string;
16+
date: string;
17+
imageUrl: string;
18+
}
19+
1220
const Images: React.FC<ImagesProps> = ({ food }) => {
1321
const [image, setImage] = useState<File | null>(null);
14-
const [uploadedImageDetails, setUploadedImageDetails] = useState<{
15-
imageName: string;
16-
uploadedBy: string; // Rename to avoid conflict
17-
date: string;
18-
imageUrl: string;
19-
} | null>(null);
22+
const [uploadedImages, setUploadedImages] = useState<ImageDetails[]>([]);
2023
const [userId, setUserId] = useState("anonymous");
2124

2225
useEffect(() => {
@@ -37,6 +40,7 @@ const Images: React.FC<ImagesProps> = ({ food }) => {
3740
console.error("No image selected");
3841
return;
3942
}
43+
//still testing gitignore
4044

4145
try {
4246
const formData = new FormData();
@@ -46,20 +50,29 @@ const Images: React.FC<ImagesProps> = ({ food }) => {
4650
// Send image data to backend
4751
const response = await axios.post(
4852
"http://localhost:8000/api/upload_image/",
49-
formData,
53+
formData
5054
);
5155

5256
// Log the response to debug
5357
console.log("API response:", response.data);
5458

5559
// Handle success and set uploaded image details
56-
const { imageName, user_id, date, imageUrl } = response.data;
57-
setUploadedImageDetails({
60+
const { imageName, imageUrl } = response.data;
61+
const date = new Date().toISOString();
62+
63+
// Remove "/public" from the imageUrl
64+
const cleanImageUrl = imageUrl.replace("/public", "");
65+
66+
console.log("Uploaded image URL:", cleanImageUrl); // Log the cleaned image URL
67+
68+
const newImageDetails: ImageDetails = {
5869
imageName,
59-
uploadedBy: user_id,
60-
date,
61-
imageUrl,
62-
});
70+
uploadedBy: userId,
71+
date: date,
72+
imageUrl: cleanImageUrl,
73+
};
74+
75+
setUploadedImages((prevImages) => [...prevImages, newImageDetails]);
6376
} catch (error) {
6477
// Handle error
6578
console.error("Failed to upload image:", error);
@@ -91,26 +104,29 @@ const Images: React.FC<ImagesProps> = ({ food }) => {
91104
Upload Image
92105
</button>
93106
</div>
94-
{uploadedImageDetails && (
95-
<div className="uploaded-image-details">
96-
<div className="image-details">
97-
<p>
98-
<strong>Name:</strong> {uploadedImageDetails.imageName}
99-
</p>
100-
<p>
101-
<strong>Uploaded by:</strong> {uploadedImageDetails.uploadedBy}
102-
</p>
103-
<p>
104-
<strong>Date:</strong> {formatDateTime(uploadedImageDetails.date)}
105-
</p>
107+
<div className="uploaded-images-list">
108+
{uploadedImages.map((details, index) => (
109+
<div key={index} className="uploaded-image-details">
110+
<div className="image-details">
111+
<p>
112+
<strong>Name:</strong> {details.imageName}
113+
</p>
114+
<p>
115+
<strong>Uploaded by:</strong> {details.uploadedBy}
116+
</p>
117+
<p>
118+
<strong>Date:</strong> {formatDateTime(details.date)}
119+
</p>
120+
</div>
121+
<Image
122+
src={details.imageUrl}
123+
alt={details.imageName}
124+
width={100}
125+
height={100}
126+
/>
106127
</div>
107-
<img
108-
src={uploadedImageDetails.imageUrl}
109-
alt={uploadedImageDetails.imageName}
110-
className="uploaded-image"
111-
/>
112-
</div>
113-
)}
128+
))}
129+
</div>
114130
</div>
115131
);
116132
};

components/location/categories.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ export default function LocationCategories({
2020
new Array(location.categories.length).fill(false).map((_, index) => {
2121
switch (index) {
2222
case 0: // Breakfast (6 AM - 11 AM)
23-
return currentHour < 11 && currentHour >= 6;
23+
return currentHour < 11;
2424
case 1: // Lunch (11 AM - 3 PM)
25-
return currentHour < 15 && currentHour >= 11;
25+
return currentHour < 15;
2626
case 2: // Dinner (6 PM - 9 PM)
27-
return currentHour < 21 && currentHour >= 18;
27+
return currentHour < 21;
2828
case 3: // Late Night (9 PM - 11 PM)
29-
return currentHour < 23 && currentHour >= 21;
29+
return currentHour < 23;
3030
default:
3131
return false;
3232
}

db.sqlite3

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)