@@ -50,27 +50,37 @@ def subscribe():
5050 if not email or not topic or (not techteams and not individuals and not communities ):
5151 return jsonify ({"status" : "error" , "message" : "Missing email or topic or publisher"
5252 }), 400
53- conn = app .db .get_connection ()
54-
55- if techteams :
56- techteams = [team .lower ().strip () for team in techteams .split (',' )]
53+ try :
54+ conn = app .db .get_connection ()
5755
58- for team in techteams :
59- publishers = app .db .get_publisher_by_name (conn , team )
60- if not publishers :
61- return jsonify ({"status" : "error" , "message" : f"Publisher '{ team } ' not found."
62- }), 404
63-
64- publisher = publishers [0 ]
56+ if techteams :
57+ techteams = [team .lower ().strip () for team in techteams .split (',' )]
6558
66- existing_subscriptions = app .db .get_subscriptions_by_email (conn , email )
67- if not any (sub ["publisher" ]["id" ] == publisher ["id" ] and sub ["topic" ] == topic for sub in existing_subscriptions ):
68- app .db .add_subscription (conn , email , topic , publisher ['id' ])
69-
70- return jsonify ({
71- "status" : "success" ,
72- "message" : "Subscription updated."
73- })
59+ for team in techteams :
60+ publishers = app .db .get_publisher_by_name (conn , team )
61+ if not publishers :
62+ return jsonify ({"status" : "error" , "message" : f"Publisher '{ team } ' not found."
63+ }), 404
64+
65+ publisher = publishers [0 ]
66+
67+ existing_subscriptions = app .db .get_subscriptions_by_email (conn , email )
68+ if not any (sub ["publisher" ]["id" ] == publisher ["id" ] and sub ["topic" ] == topic for sub in existing_subscriptions ):
69+ app .db .add_subscription (conn , email , topic , publisher ['id' ])
70+
71+ conn .commit ()
72+ return jsonify ({
73+ "status" : "success" ,
74+ "message" : "Subscription updated."
75+ })
76+ except :
77+ conn .rollback ()
78+ return jsonify ({
79+ "status" : "failed" ,
80+ "message" : "Unable to add Subscription at this time, Could you please try again."
81+ }, 500 )
82+ finally :
83+ conn .close ()
7484
7585@app .route ("/subscriptions_for_email" )
7686def subscriptions_for_email ():
@@ -85,7 +95,7 @@ def subscriptions_for_email():
8595 grouped = {}
8696 for entry in subscriptions :
8797 topic = entry ["topic" ]
88- publisher = entry ["publisher" ]["name " ]
98+ publisher = entry ["publisher" ]["publisher_name " ]
8999 if topic not in grouped :
90100 grouped [topic ] = set ()
91101 grouped [topic ].add (publisher )
0 commit comments