proiect finalizat
This commit is contained in:
@@ -1,2 +1,10 @@
|
||||
from .main import MainApplication
|
||||
app = MainApplication()
|
||||
from .main_application import MainApplication
|
||||
app = MainApplication()
|
||||
|
||||
'''
|
||||
The 'app' package contains all the UI and network components to run the application'. This package contains the
|
||||
following packages:
|
||||
- http (tools to make HTTP requests to the API)
|
||||
- models (schematics for the HTTP requests and user info validation)
|
||||
- views (UI design)
|
||||
'''
|
||||
@@ -1,3 +1,9 @@
|
||||
from .request_builder import RequestBuilder
|
||||
|
||||
request_builder = RequestBuilder()
|
||||
|
||||
"""
|
||||
The 'http' package contains the tools for making HTTP requests to the backend. This package contains:
|
||||
- request_builder: class through which the request is build
|
||||
- request_handler: object resulted from the builder and can be used to make requests.
|
||||
"""
|
||||
|
||||
@@ -2,6 +2,18 @@ from .request_handler import _HttpRequestHandler
|
||||
|
||||
|
||||
class RequestBuilder:
|
||||
"""
|
||||
A Builder for HTTP Request class
|
||||
|
||||
Attributes:
|
||||
__url (str): URL at which the request is made
|
||||
__headers (dict):
|
||||
__body (dict):
|
||||
__params (dict): Query params
|
||||
__method (str): HTTP method (GET, POST, ...)
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.__url = ""
|
||||
self.__headers = {}
|
||||
@@ -9,26 +21,27 @@ class RequestBuilder:
|
||||
self.__params = None
|
||||
self.__method = "GET"
|
||||
|
||||
def set_url(self, url=str):
|
||||
def set_url(self, url):
|
||||
self.__url = url
|
||||
return self
|
||||
|
||||
def set_headers(self, headers=dict):
|
||||
def set_headers(self, headers):
|
||||
self.__headers = headers
|
||||
return self
|
||||
|
||||
def set_body(self, body=dict):
|
||||
def set_body(self, body):
|
||||
self.__body = body
|
||||
return self
|
||||
|
||||
def set_params(self, params=dict):
|
||||
def set_params(self, params):
|
||||
self.__params = params
|
||||
return self
|
||||
|
||||
def set_method(self, method=str):
|
||||
def set_method(self, method):
|
||||
self.__method = method
|
||||
return self
|
||||
|
||||
def build(self):
|
||||
"""Based on the set parameters it returns a Request Handler object"""
|
||||
return _HttpRequestHandler(url=self.__url, method=self.__method, body=self.__body,
|
||||
params=self.__params, headers=self.__headers)
|
||||
|
||||
@@ -3,16 +3,26 @@ import requests
|
||||
|
||||
|
||||
class _HttpRequestHandler:
|
||||
"""
|
||||
A Builder for HTTP Request class
|
||||
|
||||
Attributes:
|
||||
url (str): URL at which the request is made
|
||||
headers (dict):
|
||||
body (dict):
|
||||
params (dict): Query params
|
||||
method (str): HTTP method (GET, POST, ...)
|
||||
"""
|
||||
|
||||
def __init__(self, url, method="GET", params=None, headers=None, body=None):
|
||||
self.url = url
|
||||
self.method = method
|
||||
self.headers = headers
|
||||
print(body)
|
||||
self.body = json.dumps(body)
|
||||
self.params = params
|
||||
|
||||
def make_request(self):
|
||||
print(self.body)
|
||||
"""Make HTTP request and returns the answer"""
|
||||
response = requests.request(method=self.method, url=self.url,
|
||||
params=self.params, data=self.body, headers=self.headers)
|
||||
return response
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import json
|
||||
import tkinter as tk
|
||||
from PIL import Image, ImageTk
|
||||
from app.views.login import LoginView
|
||||
from app.views.initial import InitialView
|
||||
from app.views.signup import SignupView
|
||||
@@ -52,6 +51,3 @@ class MainApplication(tk.Tk):
|
||||
self.current_logged_user['id'] = data['info']['id']
|
||||
self.current_logged_user['username'] = data['info']['username']
|
||||
self.current_logged_user['email'] = data['info']['email']
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
"""
|
||||
The 'models' package contains schematics for the HTTP requests and validations for the information provided by the
|
||||
user during the authentication. The package contains:
|
||||
- http_models
|
||||
- view_models
|
||||
"""
|
||||
@@ -1,2 +1,6 @@
|
||||
from .login import LoginRequestModel
|
||||
from .signup import SignupRequestModel
|
||||
|
||||
"""
|
||||
The 'http_models' package contains the schematics for the body data when making HTTP requests
|
||||
"""
|
||||
@@ -1,2 +1,7 @@
|
||||
from .login import LoginModel
|
||||
from .signup import SignupModel
|
||||
|
||||
"""
|
||||
The 'view models' package contains the validations for the information the user provides during the authentication
|
||||
(login/sign up)
|
||||
"""
|
||||
@@ -0,0 +1,8 @@
|
||||
"""
|
||||
The 'views' package contains all the UI implementation of the app. The app contains the following views:
|
||||
- InitialView
|
||||
- LoginView
|
||||
- SingUpView
|
||||
- MainPageView
|
||||
- ChatView
|
||||
"""
|
||||
@@ -5,6 +5,10 @@ from app.views.message import MessageView
|
||||
|
||||
|
||||
class ChatWindow(tk.Toplevel):
|
||||
"""
|
||||
This view is opened in a separate window and takes care of the communication between the two users. It receives
|
||||
both the users' information and retrieves the conversation_url.
|
||||
"""
|
||||
def __init__(self, master=None, user_data=None, current_logged_user=None):
|
||||
super().__init__(master)
|
||||
|
||||
@@ -48,6 +52,7 @@ class ChatWindow(tk.Toplevel):
|
||||
}
|
||||
|
||||
def create_widgets(self, conversation):
|
||||
"""Creates the UI of this view"""
|
||||
# Create a frame for displaying messages
|
||||
self.messages_frame = tk.Frame(self)
|
||||
self.messages_frame.pack()
|
||||
@@ -70,6 +75,10 @@ class ChatWindow(tk.Toplevel):
|
||||
self.send_button.pack(side=tk.RIGHT)
|
||||
|
||||
def update_chat(self):
|
||||
"""
|
||||
This function is scheduled each second to make a request to the API to retrieve the latest chat between the
|
||||
users
|
||||
"""
|
||||
self.messages_frame.destroy()
|
||||
self.messages_frame = tk.Frame(self)
|
||||
self.messages_frame.pack()
|
||||
@@ -85,7 +94,9 @@ class ChatWindow(tk.Toplevel):
|
||||
pass
|
||||
|
||||
def send_message(self):
|
||||
"""At the submit button, the text is sent to the database"""
|
||||
message = self.entry_widget.get()
|
||||
|
||||
self.define_send_message_params(message, "text")
|
||||
if not message:
|
||||
return
|
||||
@@ -101,6 +112,7 @@ class ChatWindow(tk.Toplevel):
|
||||
self.update_chat()
|
||||
|
||||
def get_conversation_url(self):
|
||||
"""Fetches the conversation_url"""
|
||||
request_builder.set_url(self.url)
|
||||
request_builder.set_body(self.body)
|
||||
request_builder.set_headers(self.headers)
|
||||
@@ -113,6 +125,7 @@ class ChatWindow(tk.Toplevel):
|
||||
return data['conversation_url']
|
||||
|
||||
def get_conversation(self):
|
||||
"""Fetches the conversation between the users"""
|
||||
request_builder.set_url(self.url)
|
||||
request_builder.set_body(self.body)
|
||||
request_builder.set_headers(self.headers)
|
||||
|
||||
@@ -2,6 +2,9 @@ import tkinter as tk
|
||||
|
||||
|
||||
class InitialView(tk.Frame):
|
||||
"""
|
||||
This class is the first rendered view of the application. This offers the options to Login or sign Up
|
||||
"""
|
||||
def __init__(self, master=None):
|
||||
super().__init__(master)
|
||||
self.create_widgets()
|
||||
|
||||
@@ -14,6 +14,10 @@ def hash_password(password):
|
||||
|
||||
|
||||
class LoginView(tk.Frame):
|
||||
"""
|
||||
This view takes care of the validation of the user in the system. This class receives the login information and
|
||||
validates them. The response is shown to the user.
|
||||
"""
|
||||
def __init__(self, master=None):
|
||||
super().__init__(master)
|
||||
self.form_frame = None
|
||||
@@ -27,6 +31,9 @@ class LoginView(tk.Frame):
|
||||
self.params = None
|
||||
|
||||
def create_widgets(self):
|
||||
"""
|
||||
Creates the layout of the view
|
||||
"""
|
||||
# Create a frame to hold the entry fields and labels
|
||||
self.form_frame = tk.Frame(self)
|
||||
|
||||
@@ -71,6 +78,10 @@ class LoginView(tk.Frame):
|
||||
self.master.change_to_initial_view()
|
||||
|
||||
def on_login(self):
|
||||
"""
|
||||
After login button pressed, the validation of the provided information starts and will redirect the
|
||||
user to next view if everything is accepted
|
||||
"""
|
||||
# Retrieve values from entry fields
|
||||
email = self.email_entry.get()
|
||||
password = self.password_entry.get()
|
||||
|
||||
@@ -6,6 +6,10 @@ from app.views.user_frame import UserFrame
|
||||
|
||||
|
||||
class MainPageView(tk.Frame):
|
||||
"""
|
||||
This is the view from which the user will open a chat with anyone which is registered in the system. Each chat is
|
||||
shown as a box with a photo, the username and the email.
|
||||
"""
|
||||
def __init__(self, master=None):
|
||||
super().__init__(master)
|
||||
self.form_frame = None
|
||||
@@ -20,6 +24,7 @@ class MainPageView(tk.Frame):
|
||||
self.body = None
|
||||
|
||||
def create_widgets(self):
|
||||
"""Takes care of the creation of the UI"""
|
||||
# Create a main frame to center the user frame and button frame
|
||||
main_frame = tk.Frame(self.master)
|
||||
main_frame.pack(expand=True)
|
||||
@@ -57,6 +62,7 @@ class MainPageView(tk.Frame):
|
||||
self.master.change_to_initial_view()
|
||||
|
||||
def open_chat(self, user_data):
|
||||
"""Opens a chat in a different window"""
|
||||
# Pass both the clicked user data and the current logged user info to ChatWindow
|
||||
chat_window = ChatWindow(self, user_data, self.master.current_logged_user)
|
||||
chat_window.mainloop() # Main loop for the new window
|
||||
|
||||
@@ -7,6 +7,9 @@ import base64
|
||||
|
||||
|
||||
class MessageView(tk.Frame):
|
||||
"""
|
||||
This view is the message that appears in main windows of the chat. This is configured to hold text and images.
|
||||
"""
|
||||
def __init__(self, master, message, current_logged_user, user_data, *args, **kwargs):
|
||||
super().__init__(master, *args, **kwargs)
|
||||
self.current_logged_user = current_logged_user
|
||||
|
||||
@@ -14,6 +14,10 @@ def hash_password(password):
|
||||
|
||||
|
||||
class SignupView(tk.Frame):
|
||||
"""
|
||||
This view takes care of the registration of the user in the system. This class receives the sing up
|
||||
information and validates them. The response is shown to the user.
|
||||
"""
|
||||
def __init__(self, master=None):
|
||||
super().__init__(master)
|
||||
self.form_frame = None
|
||||
@@ -27,6 +31,7 @@ class SignupView(tk.Frame):
|
||||
self.params = None
|
||||
|
||||
def create_widgets(self):
|
||||
"""Creates the layout of the view"""
|
||||
# Create a frame to hold the entry fields and labels
|
||||
self.form_frame = tk.Frame(self)
|
||||
|
||||
@@ -78,6 +83,10 @@ class SignupView(tk.Frame):
|
||||
self.master.change_to_initial_view()
|
||||
|
||||
def on_signup(self):
|
||||
"""
|
||||
After sign up button pressed, the validation of the provided information starts and will redirect the
|
||||
user to next view if everything is accepted
|
||||
"""
|
||||
# Retrieve values from entry fields
|
||||
username = self.username_entry.get()
|
||||
email = self.email_entry.get()
|
||||
|
||||
@@ -3,11 +3,13 @@ from PIL import Image, ImageTk
|
||||
|
||||
|
||||
class UserFrame(tk.Frame):
|
||||
"""
|
||||
This class takes care of the aspect of the user blocks from the MainPageView.
|
||||
"""
|
||||
def __init__(self, master, user_data, current_logged_user, click_callback, *args, **kwargs):
|
||||
super().__init__(master, *args, **kwargs)
|
||||
|
||||
self.current_logged_user = current_logged_user
|
||||
print(user_data)
|
||||
|
||||
self.user_data = {
|
||||
'id': user_data[0],
|
||||
@@ -45,7 +47,6 @@ class UserFrame(tk.Frame):
|
||||
# Bind the click event to the user frame
|
||||
user_frame.bind("<Button-1>", lambda event, user_data=self.user_data, current_logged_user=self.current_logged_user: self.on_user_click(user_data, current_logged_user))
|
||||
|
||||
|
||||
# Pack the user frame
|
||||
user_frame.pack(side=tk.LEFT, padx=10, pady=10)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user