Skip to content

Commit

Permalink
Merge pull request #224 from Arquisoft/pepito
Browse files Browse the repository at this point in the history
Pepito
  • Loading branch information
algarfer authored May 1, 2024
2 parents bdaf1dc + 5f53bbe commit e512aad
Show file tree
Hide file tree
Showing 15 changed files with 478 additions and 91 deletions.
149 changes: 149 additions & 0 deletions adminpanel/adminPanel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import tkinter as tk
from tkinter import scrolledtext, messagebox
import requests
import json

# Define the domain constant
DOMAIN = "http://158.179.216.208:8000"

# Global variable to store the auth token
auth_token = None

def format_json_display(json_text):
try:
parsed = json.loads(json_text)
formatted_json = json.dumps(parsed, indent=4)
return formatted_json
except json.JSONDecodeError:
return json_text # return original text if it's not JSON

def login():
global auth_token
try:
# Getting username and password from the GUI
username = username_entry.get()
password = password_entry.get()

# Sending POST request to login
response = requests.post(f"{DOMAIN}/login", json={"username": username, "password": password})
if response.status_code == 200:
auth_token = response.json().get('token')
response_display.config(state=tk.NORMAL)
response_display.delete('1.0', tk.END)
response_display.insert(tk.END, "Login Successful!\n")
response_display.config(state=tk.DISABLED)
else:
raise Exception("Login failed with status code: " + str(response.status_code))
except Exception as e:
messagebox.showerror("Login Error", str(e))

def send_post_request():
global auth_token
try:
# Extracting and preparing JSON data from the text field
json_data = json_input.get("1.0", tk.END)
json_payload = json.loads(json_data)

# Sending POST request
headers = {"Authorization": f"Bearer {auth_token}"}
response = requests.post(f"{DOMAIN}/admin/addGroups", json=json_payload, headers=headers)
response_display.config(state=tk.NORMAL)
response_display.delete('1.0', tk.END)
response_display.insert(tk.END, f"POST Response:\n{format_json_display(response.text)}\n")
response_display.config(state=tk.DISABLED)
except Exception as e:
messagebox.showerror("Request Error", str(e))

def send_get_groups_request():
global auth_token
try:
# Sending GET request
headers = {"Authorization": f"Bearer {auth_token}"}
response = requests.get(f"{DOMAIN}/admin/groups", headers=headers)
response_display.config(state=tk.NORMAL)
response_display.delete('1.0', tk.END)
response_display.insert(tk.END, f"Groups Data:\n{format_json_display(response.text)}\n")
response_display.config(state=tk.DISABLED)
except Exception as e:
messagebox.showerror("Request Error", str(e))

def send_remove_group_request():
global auth_token
try:
group_id = group_id_entry.get()
url = f"{DOMAIN}/admin/removeGroup/{group_id}"

# Sending GET request
headers = {"Authorization": f"Bearer {auth_token}"}
response = requests.get(url, headers=headers)
response_display.config(state=tk.NORMAL)
response_display.delete('1.0', tk.END)
response_display.insert(tk.END, f"Remove Group Response:\n{format_json_display(response.text)}\n")
response_display.config(state=tk.DISABLED)
except Exception as e:
messagebox.showerror("Request Error", str(e))

def send_gen_request():
global auth_token
try:
# Sending GET request
headers = {"Authorization": f"Bearer {auth_token}"}
response = requests.get(f"{DOMAIN}/admin/gen", headers=headers)
response_display.config(state=tk.NORMAL)
response_display.delete('1.0', tk.END)
response_display.insert(tk.END, f"Gen Response:\n{format_json_display(response.text)}\n")
response_display.config(state=tk.DISABLED)
except Exception as e:
messagebox.showerror("Request Error", str(e))

def main():
global json_input, response_display, group_id_entry, username_entry, password_entry

# Setting up the main window
root = tk.Tk()
root.title("jordiPanel Admin")
root.geometry("600x700") # Set the window size

# Login Frame
login_frame = tk.Frame(root, padx=10, pady=10)
login_frame.pack(pady=(10, 0))
tk.Label(login_frame, text="Username:").pack(side=tk.LEFT)
username_entry = tk.Entry(login_frame)
username_entry.pack(side=tk.LEFT, padx=(0, 10))
tk.Label(login_frame, text="Password:").pack(side=tk.LEFT)
password_entry = tk.Entry(login_frame, show="*")
password_entry.pack(side=tk.LEFT, padx=(0, 10))
login_button = tk.Button(login_frame, text="Login", command=login)
login_button.pack(side=tk.LEFT)


# Data Input Frame
data_frame = tk.Frame(root, padx=10, pady=10)
data_frame.pack(pady=(10, 0))
tk.Label(data_frame, text="JSON Data:").pack()
json_input = scrolledtext.ScrolledText(data_frame, height=10, width=70)
json_input.pack()

# Operations Frame
operations_frame = tk.Frame(root, padx=10, pady=10)
operations_frame.pack(pady=(10, 0))
post_button = tk.Button(operations_frame, text="Add Groups", command=send_post_request)
post_button.pack(side=tk.LEFT, padx=5)
get_groups_button = tk.Button(operations_frame, text="Get Groups", command=send_get_groups_request)
get_groups_button.pack(side=tk.LEFT, padx=5)
group_id_entry = tk.Entry(operations_frame, width=20)
group_id_entry.pack(side=tk.LEFT, padx=5)
remove_group_button = tk.Button(operations_frame, text="Remove Group", command=send_remove_group_request)
remove_group_button.pack(side=tk.LEFT, padx=5)
gen_button = tk.Button(operations_frame, text="Generate", command=send_gen_request)
gen_button.pack(side=tk.LEFT, padx=5)

# Response Display Area
response_display = scrolledtext.ScrolledText(root, height=20, width=70, bg="light grey")
response_display.pack(pady=(10, 0))
response_display.config(state=tk.DISABLED)

root.mainloop()

if __name__ == "__main__":
main()
122 changes: 122 additions & 0 deletions adminpanel/groups.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
[
{
"groupId": "capitals",
"questionItem": "Q6256",
"answer": "P36",
"statements": [
"The capital of <QuestionItem> is...",
"What is the capital of <QuestionItem>?",
"Select the capital of <QuestionItem>"
],
"categories": [
"capitals",
"geography"
]
},
{
"groupId": "continent",
"questionItem": "Q6256",
"answer": "P30",
"statements": [
"The continent of <QuestionItem> is...",
"What is the continent of <QuestionItem>?",
"Select the continent of <QuestionItem>"
],
"categories": [
"continent",
"geography"
]
},
{
"groupId": "languages",
"questionItem": "Q6256",
"answer": "P37",
"statements": [
"The language spoken in <QuestionItem> is...",
"What is the language spoken in <QuestionItem>?",
"Select the language spoken in <QuestionItem>"
],
"categories": [
"languages",
"geography"
]
},
{
"groupId": "population",
"questionItem": "Q6256",
"answer": "P1082",
"statements": [
"The population of <QuestionItem> is...",
"What is the population of <QuestionItem>",
"Select the population of <QuestionItem>"
],
"categories": [
"population",
"geography"
],
"plainText": true,
"filter": "FILTER(LANG(?question) = 'en')"
},
{
"groupId": "area",
"questionItem": "Q6256",
"answer": "P2046",
"statements": [
"The area of <QuestionItem> is...",
"What is the area of <QuestionItem>",
"Select the area of <QuestionItem>"
],
"categories": [
"area",
"geography"
],
"plainText": true,
"filter": "FILTER(LANG(?question) = 'en')"
},
{
"groupId": "gdp",
"questionItem": "Q6256",
"answer": "P2131",
"statements": [
"The GDP of <QuestionItem> is...",
"What is the GDP of <QuestionItem>",
"Select the GDP of <QuestionItem>"
],
"categories": [
"gdp",
"geography"
],
"plainText": true,
"filter": "FILTER(LANG(?question) = 'en')"
},
{
"groupId": "currency",
"questionItem": "Q6256",
"answer": "P38",
"statements": [
"The currency of <QuestionItem> is...",
"What is the currency of <QuestionItem>",
"Select the currency of <QuestionItem>"
],
"categories": [
"currency",
"geography",
"economy"
]
},
{
"groupId": "president",
"questionItem": "Q6256",
"answer": "P35",
"statements": [
"The president of <QuestionItem> is...",
"Who is the president of <QuestionItem>",
"Select the president of <QuestionItem>"
],
"categories": [
"president",
"geography",
"politics"
]
}
]
31 changes: 31 additions & 0 deletions adminpanel/groupsLoader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python3

# -*- coding: utf-8 -*-

import json
import requests

"""
Template File
:author Álvaro García Fdez:
:version 2024-04-25:
"""



def main():
data = None
with open('groups.json') as f:
data = json.load(f)
response = requests.post('http://localhost:8003/addGroups', json=data)
print(response.status_code)
print(response.text)

requests.get('http://localhost:8003/gen')
pass

if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion gatewayservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"start": "node gateway-service.js",
"dev": "nodemon gateway-service.js",
"test": "jest"
"test": "jest --coverage"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion jordi/routes/jordi-think.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = function (app, questionsRepository, groupsRepository) {
try {
const { groupId } = req.params;
await groupsRepository.removeGroups({ groupId: groupId });
await questionsRepository.deleteQuestions({ groupId: groupId });
await questionsRepository.deleteQuestions(groupId);
res.json({ message: i18next.t("group_removed") })
} catch (error) { next(error); }
});
Expand Down
2 changes: 1 addition & 1 deletion userhistory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"start": "node history-service.js",
"dev": "nodemon history-service.js",
"test": "jest"
"test": "jest --coverage"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion users/authservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"start": "node auth-service.js",
"dev": "nodemon auth-service.js",
"test": "jest"
"test": "jest --coverage"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion users/userservice/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"scripts": {
"start": "node user-service.js",
"dev": "nodemon user-service.js",
"test": "jest"
"test": "jest --coverage"
},
"repository": {
"type": "git",
Expand Down
28 changes: 28 additions & 0 deletions webapp/src/__test__/components/InfoSnackBar.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import '@testing-library/jest-dom';
import { fireEvent, screen } from "@testing-library/react";
import React from "react";
import InfoSnackBar from "../../views/components/InfoSnackBar";
import { customRender } from "../utils/customRenderer";

const render = customRender();

describe("InfoSnackBar component", () => {
test("renders snackbar with provided info message", () => {
const infoMsg = "important info";
const setMsg = jest.fn();

render(<InfoSnackBar msg={infoMsg} setMsg={setMsg} />);
expect(screen.getByRole("alert")).toBeInTheDocument();
expect(screen.getByText(`Info: ${infoMsg}`)).toBeInTheDocument();
expect(screen.getByLabelText("close")).toBeInTheDocument();
fireEvent.click(screen.getByLabelText("close"));
expect(setMsg).toHaveBeenCalledWith("");
});

test("does not render Snackbar when no info message provided", () => {
const setMsg = jest.fn();

render(<InfoSnackBar msg="" setMsg={setMsg} />);
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
});
});
Loading

0 comments on commit e512aad

Please sign in to comment.