messages work with database
This commit is contained in:
@ -6,12 +6,14 @@ class User(models.Model):
|
|||||||
mail = models.EmailField(unique=True)
|
mail = models.EmailField(unique=True)
|
||||||
password = models.CharField(max_length=100)
|
password = models.CharField(max_length=100)
|
||||||
id42 = models.DecimalField(max_digits=15, decimal_places=0, null=True, unique=True)
|
id42 = models.DecimalField(max_digits=15, decimal_places=0, null=True, unique=True)
|
||||||
|
pfp = models.CharField(max_length=1024)
|
||||||
|
|
||||||
class Message(models.Model):
|
class Message(models.Model):
|
||||||
id = models.AutoField(primary_key=True)
|
id = models.AutoField(primary_key=True)
|
||||||
date = models.DateTimeField(auto_now_add=True)
|
date = models.DateTimeField(auto_now_add=True)
|
||||||
sender = models.ForeignKey("User",on_delete=models.SET_NULL, null=True, related_name="sender")
|
sender = models.ForeignKey("User",on_delete=models.SET_NULL, null=True, related_name="sender")
|
||||||
to = models.ForeignKey("User", on_delete=models.CASCADE, related_name="to")
|
to = models.ForeignKey("User", on_delete=models.CASCADE, related_name="to")
|
||||||
|
content = models.TextField()
|
||||||
|
|
||||||
class Game(models.Model):
|
class Game(models.Model):
|
||||||
id = models.AutoField(primary_key=True)
|
id = models.AutoField(primary_key=True)
|
||||||
|
@ -6,11 +6,15 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/03 22:53:14 by edbernar #+# #+# #
|
# Created: 2024/08/03 22:53:14 by edbernar #+# #+# #
|
||||||
# Updated: 2024/08/23 23:56:06 by tomoron ### ########.fr #
|
# Updated: 2024/08/25 21:36:42 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
import json
|
||||||
|
from django.db.models import Q
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from ..models import User, Message
|
||||||
|
|
||||||
listMessage = {
|
listMessage = {
|
||||||
"type": "private_list_message",
|
"type": "private_list_message",
|
||||||
@ -48,22 +52,17 @@ listMessage = {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
def generate_random_string():
|
|
||||||
char = "abcdefghijklmnopqrstuvwxyz 123456789"
|
|
||||||
string = ""
|
|
||||||
|
|
||||||
for i in range(20):
|
|
||||||
string += char[random.randint(0, len(char) - 1)]
|
|
||||||
return string
|
|
||||||
|
|
||||||
def getPrivateListMessage(socket, content):
|
def getPrivateListMessage(socket, content):
|
||||||
# |TOM| Requete pour avoir la liste des messages privés grace à l'id de l'utilisateur
|
# |TOM| Requete pour avoir la liste des messages privés grace à l'id de l'utilisateur
|
||||||
copyListMessage = listMessage.copy()
|
if(not User.objects.filter(id=content["id"]).exists()):
|
||||||
for message in copyListMessage["content"]:
|
socket.sendError("User not found", 9008)
|
||||||
if (random.randint(1, 10) % 2 == 0):
|
return;
|
||||||
message["from"] = 9999999
|
id = socket.scope["session"].get("id", 0)
|
||||||
else:
|
other_id = content["id"]
|
||||||
message["from"] = content["id"]
|
messages = Message.objects.filter((Q(to=id) & Q(sender=other_id)) | (Q(to=other_id) & Q(sender=id))).order_by('date')
|
||||||
message["content"] = generate_random_string()
|
result = []
|
||||||
socket.send(text_data=json.dumps(copyListMessage))
|
for x in messages:
|
||||||
|
result.append({"from":x.sender.id, "content": x.content, "date" : x.date.strftime("%H:%M:%S %d/%m/%Y")})
|
||||||
|
print(result)
|
||||||
|
socket.send(text_data=json.dumps({"type":"private_list_message","content":result}))
|
||||||
|
|
||||||
|
@ -6,55 +6,61 @@
|
|||||||
# By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/03 15:10:23 by edbernar #+# #+# #
|
# Created: 2024/08/03 15:10:23 by edbernar #+# #+# #
|
||||||
# Updated: 2024/08/23 23:55:50 by tomoron ### ########.fr #
|
# Updated: 2024/08/25 21:23:08 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
from ..models import Message, User
|
||||||
|
|
||||||
data = [
|
#data = [
|
||||||
{
|
# {
|
||||||
"name": "Nessundorma",
|
# "name": "Nessundorma",
|
||||||
"status": "online",
|
# "status": "online",
|
||||||
"pfp": "https://wallpapers-clan.com/wp-content/uploads/2023/05/cool-pfp-02.jpg",
|
# "pfp": "https://wallpapers-clan.com/wp-content/uploads/2023/05/cool-pfp-02.jpg",
|
||||||
"id": 145564
|
# "id": 145564
|
||||||
},
|
# },
|
||||||
{
|
# {
|
||||||
"name": "Succotash",
|
# "name": "Succotash",
|
||||||
"status": "offline",
|
# "status": "offline",
|
||||||
"pfp": "https://i.pinimg.com/200x/28/75/96/287596f98304bf1adc2c411619ae8fef.jpg",
|
# "pfp": "https://i.pinimg.com/200x/28/75/96/287596f98304bf1adc2c411619ae8fef.jpg",
|
||||||
"id": 256981
|
# "id": 256981
|
||||||
},
|
# },
|
||||||
{
|
# {
|
||||||
"name": "Astropower",
|
# "name": "Astropower",
|
||||||
"status": "online",
|
# "status": "online",
|
||||||
"pfp": "https://ashisheditz.com/wp-content/uploads/2024/03/cool-anime-pfp-demon-slayer-HD.jpg",
|
# "pfp": "https://ashisheditz.com/wp-content/uploads/2024/03/cool-anime-pfp-demon-slayer-HD.jpg",
|
||||||
"id": 301547
|
# "id": 301547
|
||||||
},
|
# },
|
||||||
{
|
# {
|
||||||
"name": "Assaultive",
|
# "name": "Assaultive",
|
||||||
"status": "offline",
|
# "status": "offline",
|
||||||
"pfp": "https://i1.sndcdn.com/artworks-1Li0JIJrQGlojD3y-AEiNkw-t500x500.jpg",
|
# "pfp": "https://i1.sndcdn.com/artworks-1Li0JIJrQGlojD3y-AEiNkw-t500x500.jpg",
|
||||||
"id": 432448
|
# "id": 432448
|
||||||
},
|
# },
|
||||||
{
|
# {
|
||||||
"name": "Redshock",
|
# "name": "Redshock",
|
||||||
"status": "offline",
|
# "status": "offline",
|
||||||
"pfp": "https://cdn.pfps.gg/pfps/7094-boy-pfp.png",
|
# "pfp": "https://cdn.pfps.gg/pfps/7094-boy-pfp.png",
|
||||||
"id": 543211
|
# "id": 543211
|
||||||
},
|
# },
|
||||||
{
|
# {
|
||||||
"name": "Parley",
|
# "name": "Parley",
|
||||||
"status": "offline",
|
# "status": "offline",
|
||||||
"pfp": "https://pbs.twimg.com/media/EscE6ckU0AA-Uhe.png",
|
# "pfp": "https://pbs.twimg.com/media/EscE6ckU0AA-Uhe.png",
|
||||||
"id": 654123
|
# "id": 654123
|
||||||
}
|
# }
|
||||||
]
|
#]
|
||||||
|
|
||||||
def getPrivateListUser(socket, content=None):
|
def getPrivateListUser(socket, content=None):
|
||||||
# |TOM| Faire une requête à la base de données pour récupérer la liste des
|
# |TOM| Faire une requête à la base de données pour récupérer la liste des
|
||||||
# utilisateurs qui doivent apparaitre dans la liste du chat privé
|
# utilisateurs qui doivent apparaitre dans la liste du chat privé
|
||||||
# (ceux qui ont eu conversation avec l'utilisateur)
|
# (ceux qui ont eu conversation avec l'utilisateur)
|
||||||
# Si user existe pas, faire ça : socket.sendError("User not found", 9008)
|
# Si user existe pas, faire ça : socket.sendError("User not found", 9008)
|
||||||
|
id = socket.scope["session"].get("id", 0)
|
||||||
|
res = User.objects.raw("SELECT DISTINCT server_user.id AS id,'online' AS status, username, pfp FROM server_user RIGHT JOIN server_message ON server_message.to_id=server_user.id WHERE sender_id=%s;", [id])
|
||||||
|
data = []
|
||||||
|
for x in res:
|
||||||
|
data.append({"name":x.username, "status":x.status, "pfp":x.pfp, "id":x.id})
|
||||||
socket.send(text_data=json.dumps({"type": "private_list_user", "content": data}))
|
socket.send(text_data=json.dumps({"type": "private_list_user", "content": data}))
|
||||||
|
@ -6,11 +6,12 @@
|
|||||||
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
# By: edbernar <edbernar@student.42angouleme. +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2024/08/04 13:44:11 by edbernar #+# #+# #
|
# Created: 2024/08/04 13:44:11 by edbernar #+# #+# #
|
||||||
# Updated: 2024/08/23 23:54:32 by tomoron ### ########.fr #
|
# Updated: 2024/08/25 21:49:02 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
from datetime import datetime
|
from ..models import User, Message
|
||||||
|
import json
|
||||||
|
|
||||||
def sendPrivateMessage(socket, content):
|
def sendPrivateMessage(socket, content):
|
||||||
# |Tom| Requete pour vérifier si l'user existe
|
# |Tom| Requete pour vérifier si l'user existe
|
||||||
@ -21,14 +22,18 @@ def sendPrivateMessage(socket, content):
|
|||||||
# l'envoyeur recevra le message.
|
# l'envoyeur recevra le message.
|
||||||
|
|
||||||
try:
|
try:
|
||||||
time = content["time"]
|
dest = User.objects.filter(id=content["to"])
|
||||||
time = datetime.strptime(time, "%Y-%m-%dT%H:%M:%S.%fZ")
|
if(not dest.exists()):
|
||||||
time = time.strftime("%H:%M %d/%m/%Y")
|
socket.sendError("User not found", 9008)
|
||||||
|
return;
|
||||||
|
user = User.objects.filter(id=socket.scope["session"]["id"])
|
||||||
|
new_msg = Message.objects.create(sender=user[0], to=dest[0], content=content["content"])
|
||||||
|
new_msg.save()
|
||||||
jsonVar = {"type": "new_private_message", "content": {
|
jsonVar = {"type": "new_private_message", "content": {
|
||||||
"from": content["from"],
|
"from": content["from"],
|
||||||
"channel": content["to"],
|
"channel": content["to"],
|
||||||
"content": content["content"],
|
"content": content["content"],
|
||||||
"date": time
|
"date": new_msg.date.strftime("%H:%M:%S %d/%m/%Y")
|
||||||
}}
|
}}
|
||||||
socket.send(text_data=json.dumps(jsonVar))
|
socket.send(text_data=json.dumps(jsonVar))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
@ -0,0 +1,7 @@
|
|||||||
|
INSERT INTO "server_user" ("username", "mail", "password", "id42", "pfp") VALUES
|
||||||
|
('tomoron', 'a@a.com', 'a5f1efc32bcc4dc53e471f9620b78ca9', NULL, 'https://localhost/img.jpg'),
|
||||||
|
('patate', 'b@b.com', '5e547979d3f20f43c87e5e0e8406f267', NULL, 'https://localhost/pfp.jpg');
|
||||||
|
|
||||||
|
INSERT INTO "server_message" ("date", "sender_id", "to_id", "content") VALUES
|
||||||
|
('2024-08-25 18:10:54.27476+00', 1, 2, 'coucou'),
|
||||||
|
('2024-08-25 18:11:25.987857+00', 2, 1, 'coucou les musulmans moi je mange la glace');
|
Reference in New Issue
Block a user