Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mbu 8 add category and iq route #17

Merged
merged 4 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions api/data/images/Credit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
Animals :
https://unsplash.com/fr/photos/ours-polaire-sur-un-sol-enneige-pendant-la-journee-qQWV91TTBrE
Hans-Jurgen Mager

Brainteaser :
https://unsplash.com/fr/photos/3-x-3-rubiks-cube-CmO_GydmKaY
Alan de la Cruz

Celebrity :
https://unsplash.com/fr/photos/tapis-rouge-dans-les-escaliers-Xjp9CDaxAX8
Claudio Schwarz

Entertainments :
https://unsplash.com/fr/photos/figurine-de-robot-bleu-et-noir-S-vkpXA3os8
Craig Sybert

for kids :
https://unsplash.com/fr/photos/cinq-jouets-de-voitures-de-course-de-couleurs-assorties-Hld-gd-WN7k
Baby Natur

General :
https://unsplash.com/fr/photos/fruit-de-pomme-rouge-sur-quatre-livres-pyle-OyCl7Y4y0Bk
Element5 digital

Geography :
https://unsplash.com/fr/photos/globe-de-bureau-sur-table-nXt5HtLmlgE
Kyle Glenn

History :
https://unsplash.com/fr/photos/livres-dans-une-bibliotheque-en-verre-jKU2NneZAbI
Clarisse Meyer

Hobbies :
https://unsplash.com/fr/photos/deux-femme-debout-a-cote-de-la-femme-assise-devant-la-table-ZrhtQyGFG6s
Kelsey Chance

Humanity :
https://unsplash.com/fr/photos/cinq-mains-humaines-sur-une-surface-brune-LjqARJaJotc
Clay Banks

Literature :
https://unsplash.com/fr/photos/lot-a-livre-ouvert-Oaqk7qqNh_c
Patrick Tomasso

Movies :
https://unsplash.com/fr/photos/gros-plan-dune-bobine-de-film-tV80374iytg
Denise Jans

Music :
https://unsplash.com/fr/photos/disque-vinyle-noir-sur-disque-vinyle-noir-fi3_lDi3qPE
Eric Krull

Newest :
https://unsplash.com/fr/photos/papier-dimprimante-blanc-et-noir-oHoBIbDj7lo
Glen Carrie

People :
https://unsplash.com/fr/photos/barack-obama-v_e3Hha4EBA
History In HD

Rated :
https://unsplash.com/fr/photos/un-fond-jaune-avec-beaucoup-de-petits-cercles-94qPvR72FWk
Lucas K

Religion Faith :
https://unsplash.com/fr/photos/personne-tenant-une-page-de-livre-avec-des-guirlandes-lumineuses-wnrxQGBhbh8
Timothy Eberly

Science-Technology :
https://unsplash.com/fr/photos/photo-du-film-matrix-iar-afB0QQw
Markus Spiske

Sport :
https://unsplash.com/fr/photos/paire-de-chaussures-de-course-noires-bE6k8SQT2FQ
Kelly Sikkema

Television :
https://unsplash.com/fr/photos/televiseur-a-tube-cathodique-noir-sur-table-en-bois-marron-ZNTPlG050tk
TopSphere Media

Video Game :
https://unsplash.com/fr/photos/personne-assise-sur-une-chaise-de-jeu-tout-en-jouant-a-un-jeu-video-Mf23RF8xArY
Florian Olivo

Worlds :
https://unsplash.com/fr/photos/photographie-a-loeil-de-poisson-de-la-ville-KRELIShKxTM
Joshua Rawson-Harris

Community :
https://unsplash.com/fr/photos/homme-en-chemise-noire-et-jean-bleu-marchant-sur-un-tapis-violet-et-jaune-AqK0Sw2Gj-c
Greg Rosenke
Binary file added api/data/images/animals.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/community.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/default.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/entertainment.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/for_kids.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/general.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/geography.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/history.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/hobbies.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/humanities.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/literature.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/movies.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/music.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/newest.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/people.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/rated.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/religion_faith.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/science_technology.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/sports.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/television.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/video_games.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/data/images/world.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion api/masteriq/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,6 @@
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


INIT_DATA_FOLDER = os.path.join('data', 'csv')
INIT_DATA_FOLDER = os.path.join('data', 'csv')
IMAGES_FOLDER = os.path.join('data', 'images')
DEFAULT_IMAGE = "default.jpeg"
9 changes: 9 additions & 0 deletions api/masteriq/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@
"""
from django.contrib import admin
from django.urls import path
from django.urls.conf import include
from rest_framework.routers import DefaultRouter

from masteriqapp import views

router = DefaultRouter()

router.register("category", views.IQView, basename="iq")

urlpatterns = [
path('admin/', admin.site.urls),
path("", include(router.urls))
]
2 changes: 1 addition & 1 deletion api/masteriqapp/migrations/0002_auto_20240303_1147.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def load_initial_data(apps, schema_editor):
f = os.path.join(directory, filename)
if os.path.isfile(f) and filename.endswith(".csv"):
df = pd.read_csv(f)
category_name = filename[:-3].replace('-', ' ').title()
category_name = filename[:-4].replace('-', ' ').title()
category = category_model.objects.create(name=category_name)
for row in df.index:
if pd.isnull(df['A'][row]) or pd.isnull(df['B'][row]):
Expand Down
32 changes: 32 additions & 0 deletions api/masteriqapp/migrations/0004_auto_20240308_0923.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 5.0.2 on 2024-03-08 08:23
import os

from django.conf import settings
from django.db import migrations
import glob


def add_category_images(apps, schema_editor):
category_model = apps.get_model('masteriqapp', 'Category')

for category in category_model.objects.all():
directory = settings.IMAGES_FOLDER
img_name = category.name.lower().replace(' ', '_') + ".*"
print(img_name)
f = os.path.join(directory, img_name)
files = glob.glob(f)
print("found: " + str(len(files)))
if len(files) > 0:
print(files[0])
category.image_path = os.path.join(files[0])
category.save()


class Migration(migrations.Migration):
dependencies = [
('masteriqapp', '0005_category_image_path'),
]

operations = [
migrations.RunPython(add_category_images)
]
18 changes: 18 additions & 0 deletions api/masteriqapp/migrations/0005_category_image_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.2 on 2024-03-08 08:42

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('masteriqapp', '0003_alter_category_users'),
]

operations = [
migrations.AddField(
model_name='category',
name='image_path',
field=models.CharField(default='data\\images\\default.jpeg', max_length=255),
),
]
3 changes: 3 additions & 0 deletions api/masteriqapp/models/Category.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.db import models
from django.conf import settings
import os

class Category(models.Model):
name = models.CharField(max_length=255)
users = models.ManyToManyField(settings.AUTH_USER_MODEL, through="IQ")
image_path = models.CharField(max_length=255, default=os.path.join(settings.IMAGES_FOLDER, settings.DEFAULT_IMAGE))


14 changes: 14 additions & 0 deletions api/masteriqapp/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.test import TestCase, Client
import django.apps


class RouteTestCases(TestCase):
def test_route(self):
c = Client()
response = c.get("/category/1/image/")
assert response.status_code == 200

response = c.get("/category/iq/")
assert response.json()["1"]["category_name"] is not None
assert response.json()["1"]["user_iq"] is not None
assert response.status_code == 200
3 changes: 0 additions & 3 deletions api/masteriqapp/views.py

This file was deleted.

38 changes: 38 additions & 0 deletions api/masteriqapp/views/IQView.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from django.http import HttpResponse
from django.http import JsonResponse
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.generics import get_object_or_404
from django.apps import apps
from PIL import Image
from rest_framework import status
from rest_framework.response import Response

masteriq = apps.get_app_config("masteriqapp")


class IQView(viewsets.ViewSet):
category_model = masteriq.get_model("Category")
queryset = category_model.objects.all()

@action(detail=True, methods=["GET"], url_path="image")
def category_image(self, request, pk):
category = get_object_or_404(self.queryset, pk=pk)
try:
with open(category.image_path, 'rb') as img:
return HttpResponse(img.read(), content_type="image/jpeg", status=status.HTTP_200_OK)
except IOError:
return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)

@action(detail=False, methods=["GET"], url_path="iq")
def category_with_iq(self, request):
#TODO: RESTRICT TO CONNECTED USER
answer_dict = {}
for category in self.queryset:
cat_dict = {
"category_name": category.name,
"user_iq": 100 #TODO: REPLACE WITH USER IQ
}
answer_dict[category.id] = cat_dict
return JsonResponse(answer_dict)

1 change: 1 addition & 0 deletions api/masteriqapp/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .IQView import IQView
Loading