Added withdrawal capabilities.
TODO: improve logging, have researchers make use of withdrawal
This commit is contained in:
parent
b752941779
commit
41adade9fc
@ -6,7 +6,6 @@ from typing import List
|
|||||||
class Transaction:
|
class Transaction:
|
||||||
person: str
|
person: str
|
||||||
value: int
|
value: int
|
||||||
description: str
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
|
|||||||
@ -24,6 +24,7 @@ def get_project(ch, method, properties, body):
|
|||||||
routing_key=properties.reply_to,
|
routing_key=properties.reply_to,
|
||||||
properties=pika.BasicProperties(correlation_id=properties.correlation_id),
|
properties=pika.BasicProperties(correlation_id=properties.correlation_id),
|
||||||
body=project)
|
body=project)
|
||||||
|
log(f"Replying to {properties.reply_to}")
|
||||||
log(f"got project {project_id}")
|
log(f"got project {project_id}")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,35 +1,46 @@
|
|||||||
import pika
|
import pika
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import pickle
|
||||||
|
|
||||||
from functions import log
|
from functions import log, save_project
|
||||||
|
from classes import *
|
||||||
|
|
||||||
if not os.path.exists("approved_projects"):
|
class University:
|
||||||
os.makedirs("approved_projects")
|
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")))
|
self.channel.queue_declare(queue='university_project')
|
||||||
channel = connection.channel()
|
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):
|
def withdraw_funds(self, ch, method, properties, body):
|
||||||
proposal = body.decode()
|
body = body.decode()
|
||||||
acronym, title, description, amount = proposal.split(',')
|
proj_id, self.name, self.amount = body.split(',')
|
||||||
|
|
||||||
|
self.channel.basic_publish(exchange='', routing_key='get_project',
|
||||||
filename = os.path.join(os.getcwd(), 'approved_projects', f'{acronym}.txt')
|
properties=pika.BasicProperties(reply_to="university_project"),
|
||||||
with open(filename, 'w') as f:
|
body=proj_id)
|
||||||
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)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
log('University waiting for approved projects...')
|
university = University()
|
||||||
channel.start_consuming()
|
university.channel.start_consuming()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
channel.stop_consuming()
|
university.channel.stop_consuming()
|
||||||
connection.close()
|
university.connection.close()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user