cleanup and fix for empty message body

This commit is contained in:
DJ2LS 2024-02-06 20:33:36 +01:00
parent 09a0bf0c0d
commit f526a9adb3
6 changed files with 3 additions and 27 deletions

View file

@ -6,7 +6,6 @@ def validate_freedata_callsign(callsign):
return re.compile(regexp).match(callsign) is not None
def validate_message_attachment(attachment):
print(attachment)
for field in ['name', 'type', 'data']:
if field not in attachment:
raise ValueError(f"Attachment missing '{field}'")

View file

@ -13,12 +13,8 @@ class SendMessageCommand(TxCommand):
"""
def set_params_from_api(self, apiParams):
print(apiParams)
origin = f"{self.config['STATION']['mycall']}-{self.config['STATION']['myssid']}"
self.message = MessageP2P.from_api_params(origin, apiParams)
print(self.message.id)
print(self.message.to_dict())
print("--------------------------------------- set params from api")
DatabaseManagerMessages(self.event_manager).add_message(self.message.to_dict(), direction='transmit', status='queued')
def transmit(self, modem):
@ -35,10 +31,7 @@ class SendMessageCommand(TxCommand):
self.log(f"Queued message found: {first_queued_message['id']}")
DatabaseManagerMessages(self.event_manager).update_message(first_queued_message["id"], update_data={'status': 'transmitting'})
message_dict = DatabaseManagerMessages(self.event_manager).get_message_by_id(first_queued_message["id"])
print(message_dict["id"])
message = MessageP2P.from_api_params(message_dict['origin'], message_dict)
print(message.id)
print("--------------------------------------- transmit")
# Convert JSON string to bytes (using UTF-8 encoding)
payload = message.to_payload().encode('utf-8')

View file

@ -42,8 +42,6 @@ class MessageP2P:
raise ValueError(f"Invalid destination given ({params['destination']})")
body = params['body']
if len(body) < 1:
raise ValueError(f"Body cannot be empty")
attachments = []
if 'attachments' in params:

View file

@ -11,17 +11,6 @@ class DatabaseManagerAttachments(DatabaseManager):
def add_attachment(self, session, message, attachment_data):
"""
Adds an attachment to a message, either by creating a new attachment or reusing an existing one.
Args:
- session: The current database session.
- message: The P2PMessage instance to which the attachment should be linked.
- attachment_data: A dictionary containing the attachment's data.
Returns:
- The Attachment instance.
"""
hash_sha512 = hashlib.sha512(attachment_data['data'].encode()).hexdigest()
existing_attachment = session.query(Attachment).filter_by(hash_sha512=hash_sha512).first()
@ -70,7 +59,7 @@ class DatabaseManagerAttachments(DatabaseManager):
try:
attachment = session.query(Attachment).filter_by(hash_sha512=hash_sha512).first()
if attachment:
return attachment.to_dict() # Assuming you have a to_dict method
return attachment.to_dict()
else:
self.log(f"No attachment found with SHA-512 hash: {hash_sha512}")
return None

View file

@ -4,12 +4,9 @@ import sqlite3
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from threading import local
from message_system_db_model import Base, Station, Status, Attachment, P2PMessage, MessageAttachment
from datetime import datetime
import json
from message_system_db_model import Base, Station, Status
import structlog
import helpers
import hashlib
class DatabaseManager:
def __init__(self, event_manger, uri='sqlite:///freedata-messages.db'):

View file

@ -1,6 +1,6 @@
# models.py
from sqlalchemy import Index, Table, Boolean, Column, String, Integer, JSON, ForeignKey, DateTime
from sqlalchemy import Index, Boolean, Column, String, Integer, JSON, ForeignKey, DateTime
from sqlalchemy.orm import declarative_base, relationship
Base = declarative_base()