Added withdrawal capabilities.

TODO: improve logging, have researchers make use of withdrawal
This commit is contained in:
Malachy Byrne 2023-04-13 04:28:10 +01:00
parent b752941779
commit 41adade9fc
Signed by: malmal200
GPG Key ID: EC21443030A655D9
3 changed files with 35 additions and 24 deletions

View File

@ -6,7 +6,6 @@ from typing import List
class Transaction:
person: str
value: int
description: str
@dataclass

View File

@ -24,6 +24,7 @@ def get_project(ch, method, properties, body):
routing_key=properties.reply_to,
properties=pika.BasicProperties(correlation_id=properties.correlation_id),
body=project)
log(f"Replying to {properties.reply_to}")
log(f"got project {project_id}")

View File

@ -1,35 +1,46 @@
import pika
import os
import pickle
from functions import log
from functions import log, save_project
from classes import *
if not os.path.exists("approved_projects"):
os.makedirs("approved_projects")
class University:
def __init__(self):
self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password")))
self.channel = self.connection.channel()
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password")))
channel = connection.channel()
self.channel.queue_declare(queue='university_project')
self.channel.queue_declare(queue='withdraw_funds')
self.channel.basic_consume(queue="university_project", on_message_callback=self.receive_response, auto_ack=True)
self.channel.basic_consume(queue="withdraw_funds", on_message_callback=self.withdraw_funds, auto_ack=True)
self.response = None
self.corr_id = None
channel.queue_declare(queue='approved_projects')
def receive_response(self, ch, method, properties, body):
self.response = body
project = pickle.loads(self.response)
transaction = Transaction(self.name, int(self.amount))
project.history.append(transaction)
project.amount -= int(self.amount)
if project.amount >= 0:
save_project(project)
log(f"{self.name} withdrew {self.amount} from {project.acronym}. {project.amount} remaining")
else:
log(f"{self.name} attempter to withdraw {self.amount} from {project.acronym} but there was only {project.amount + int(self.amount)} remaining")
def process_approved_project(ch, method, properties, body):
proposal = body.decode()
acronym, title, description, amount = proposal.split(',')
def withdraw_funds(self, ch, method, properties, body):
body = body.decode()
proj_id, self.name, self.amount = body.split(',')
filename = os.path.join(os.getcwd(), 'approved_projects', f'{acronym}.txt')
with open(filename, 'w') as f:
f.write(f'Acronym: {acronym}\n')
f.write(f'Researcher: {description}\n')
f.write(f'Budget: {amount}\n')
log(f'Approved project received: {body}')
channel.basic_consume(queue='approved_projects', on_message_callback=process_approved_project, auto_ack=True)
self.channel.basic_publish(exchange='', routing_key='get_project',
properties=pika.BasicProperties(reply_to="university_project"),
body=proj_id)
try:
log('University waiting for approved projects...')
channel.start_consuming()
university = University()
university.channel.start_consuming()
except KeyboardInterrupt:
channel.stop_consuming()
connection.close()
university.channel.stop_consuming()
university.connection.close()