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

Sp 14 page categories #26

Merged
merged 7 commits into from
Mar 16, 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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.idea
db.sqlite3
__pycache__

# Capistrano
/config/
Capfile
Gemfile
Gemfile.lock
capistrano.log
1 change: 1 addition & 0 deletions api/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ name = "pypi"
django = "==5.0.2"
djangorestframework = "==3.14.0"
pandas = "*"
django-cors-headers = "*"

[dev-packages]

Expand Down
11 changes: 10 additions & 1 deletion api/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions api/masteriq/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

ALLOWED_HOSTS = []

CORS_ALLOWED_ORIGINS = [
"http://localhost:5173",
"http://127.0.0.1:5173",
]

# Application definition

Expand All @@ -40,9 +44,11 @@
'django.contrib.staticfiles',
'masteriqapp',
'rest_framework',
"corsheaders",
]

MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
Expand Down
2 changes: 1 addition & 1 deletion api/masteriq/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@

urlpatterns = [
path('admin/', admin.site.urls),
path("", include(router.urls))
path("api/", include(router.urls))
]
8 changes: 4 additions & 4 deletions api/masteriqapp/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
class RouteTestCases(TestCase):
def test_route(self):
c = Client()
response = c.get("/category/1/image/")
response = c.get("/api/category/1/image/")
assert response.status_code == 200

response = c.get("/category/iq/")
response = c.get("/api/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

response = c.get("/question/1/new/")
response = c.get("/api/question/1/new/")
assert response.status_code == 200
assert response.json()['id'] is not None
assert response.json()['text'] is not None
assert response.json()['category'] is not None

response = c.post("/question/new_community/", {
response = c.post("/api/question/new_community/", {
"question": "How old is Harry Potter at the beginning of the first book?",
"options": ["11", "15", "He wasn\'t born"],
"answer": "1"})
Expand Down
Binary file modified api/requirements.txt
Binary file not shown.
13 changes: 13 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.0.3",
"dotenv": "^16.4.5",
"vite": "^5.0.11"
}
}
15 changes: 14 additions & 1 deletion frontend/src/assets/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ p,
li,
input,
button,
label
label,
h1,
h2,
h3,
h4
{
font-family: 'Montserrat', Inter, sans-serif;
}
Expand All @@ -34,6 +38,15 @@ label
background-color: var(--color-background-btn-hover);
}

.title
{
font-family: 'Montserrat', Inter, sans-serif;
font-weight: 800;
color: black;
font-size: 1.75rem;
font-style: italic;
}

.container
{
max-width: 1100px;
Expand Down
9 changes: 0 additions & 9 deletions frontend/src/components/Categories.vue

This file was deleted.

84 changes: 84 additions & 0 deletions frontend/src/components/CategoryItem.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<script setup>
import axios from 'axios';
import { onMounted, ref } from 'vue';
const API_SERVER = import.meta.env.VITE_API_SERVER;

const imageUrl = ref('');

const fetchImage = async () => {
const response = await axios.get(`${API_SERVER}/api/category/${props.id}/image/`, { responseType: 'arraybuffer' });
imageUrl.value = 'data:image/jpeg;base64,' + arrayBufferToBase64(response.data);
}

const arrayBufferToBase64 = (buffer) => {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}

const props = defineProps({
id: Number,
category: Object
})
onMounted(() => {
fetchImage();
});
</script>

<template>
<RouterLink class="link" :to="{ name: 'Quiz', params: { id_category: id } }">
<div class="category-item" :style="{ backgroundImage: 'url(' + imageUrl + ')' }">
<div class="opacity-filter">
<h2 class="name">{{ category.category_name }}</h2>
<p class="iq">{{ category.user_iq }}</p>
</div>
</div>
</RouterLink>
</template>

<style scoped style="scss">
.link
{
text-decoration: none;
color: inherit;
transition: all 0.3s ease-in-out;
&:hover
{
transform: scale(1.05);
}
}
.category-item {
border-radius: 1.5rem;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
height: 100%;

.opacity-filter {
background-color: rgba(0, 0, 0, 0.5);
padding: 2rem 1rem;
border-radius: 1.5rem;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
}

.name {
color: white;
font-size: 1.5rem;
font-weight: 800;
font-style: italic;
}

.iq {
color: white;
font-size: 1.2rem;
font-weight: 600;
}
}
</style>
2 changes: 1 addition & 1 deletion frontend/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const router = createRouter({
component: HomeView
},
{
path: '/quiz',
path: '/quiz/:id_category',
name: 'Quiz',
component: () => import('../views/QuizView.vue')
},
Expand Down
47 changes: 45 additions & 2 deletions frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
<script setup>
import axios from 'axios';
import { ref, onMounted } from 'vue';
import CategoryItem from '@/components/CategoryItem.vue';
const API_SERVER = import.meta.env.VITE_API_SERVER;

const categories = ref([]);

const fetchCategories = async (query) => {
const response = await axios.get(`${API_SERVER}/api/category/iq/`, {
params: query
});
categories.value = response.data;
}

onMounted(() => {
fetchCategories();
});
</script>

<template>
<main class="container">
<h1>This is home page with categories</h1>
<button class="btn">Random question</button>
<p class="info">Maximise your IQs in each question category and climb the overall rankings.</p>
<h1 class="title">Categories</h1>
<div class="all-categories">
<CategoryItem v-for="category, key in categories" :id="parseInt(key)" :category></CategoryItem>
</div>
<div class="btn-container">
<button class="btn">Random question</button>

</div>
</main>
</template>

<style scoped>
.all-categories {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
row-gap: 1rem;
column-gap: 1.5rem;
}

.info {
margin: 1.5rem 0 1rem 0;
}

.btn-container {
display: flex;
justify-content: center;
padding: 2rem 0;
}
</style>
1 change: 1 addition & 0 deletions frontend/src/views/QuizView.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<section class="quiz container">
<h1>This is quiz page</h1>
Id category {{ $route.params.id_category }}
</section>
</template>
18 changes: 11 additions & 7 deletions frontend/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import dotenv from 'dotenv';
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
export default defineConfig(({ mode }) => {
dotenv.config({ path: `./.env.${mode}` });
return {
plugins: [
vue(),
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
}
})
Loading