diff --git a/services/events/project/api/events.py b/services/events/project/api/events.py index 7cff506c..d6b2bc16 100644 --- a/services/events/project/api/events.py +++ b/services/events/project/api/events.py @@ -26,6 +26,14 @@ def extract_enteries(entries): return [Entry(type=str(entry)) for entry in entries if entry] +def existing_match_from_name(name, start): + return Event.query.filter_by(name=name, start=start).first() + + +def existing_match_from_source_id(source, source_id): + return Event.query.filter_by(source=source, source_id=source_id).first() + + @events_blueprint.route("/", methods=["GET", "POST"]) def index(): if request.method == "POST": @@ -40,6 +48,7 @@ def index(): entry = request.form["entry"] category = request.form["category"] source = request.form["source"] + source_id = request.form["source_id"] topic_list = extract_topics(topics) @@ -55,6 +64,7 @@ def index(): location=location, category=category, source=source, + source_id=source_id, ) db.session.add(event) @@ -88,36 +98,42 @@ def add_event(): entries = post_data.get("entry") category = post_data.get("category") source = post_data.get("source") + source_id = post_data.get("source_id") topics_list = extract_topics(topics) entry_list = extract_enteries(entries) + if existing_match_from_name(name, start): + response_object["message"] = "Event with name and start already exists." + return jsonify(response_object), 202 + + if source_id and existing_match_from_source_id(source, source_id): + response_object["message"] = "Event from with source id already exists." + return jsonify(response_object), 202 + try: - event = Event.query.filter_by(name=name, start=start).first() - if not event: - event = Event( - name=name, - description=description, - url=url, - start=start, - end=end, - duration=duration, - topics=topics_list, - entry=entry_list, - location=location, - category=category, - source=source, - ) - db.session.add(event) - db.session.commit() + event = Event( + name=name, + description=description, + url=url, + start=start, + end=end, + duration=duration, + topics=topics_list, + entry=entry_list, + location=location, + category=category, + source=source, + source_id=source_id, + ) + db.session.add(event) + db.session.commit() - response_object["status"] = "success" - response_object["message"] = f"{name} was added!" + response_object["status"] = "success" + response_object["message"] = f"{name} was added!" + + return jsonify(response_object), 201 - return jsonify(response_object), 201 - else: - response_object["message"] = "Sorry. That id already exists." - return jsonify(response_object), 202 except (exc.IntegrityError, ValueError): db.session.rollback() return jsonify(response_object), 400 diff --git a/services/events/project/api/models.py b/services/events/project/api/models.py index 5ea55ddc..9aae0fcb 100644 --- a/services/events/project/api/models.py +++ b/services/events/project/api/models.py @@ -170,6 +170,7 @@ class Event(OutputMixin, db.Model): updated = Column(db.DateTime, default=datetime.utcnow, nullable=False) deleted = Column(db.DateTime, nullable=True) source = Column(db.String(50), nullable=False) + source_id = Column(db.String(256), nullable=True) def __init__( self, @@ -183,6 +184,7 @@ def __init__( entry, category, source, + source_id, location, ): self.name = name @@ -195,6 +197,7 @@ def __init__( self.entry = entry self.category = category self.source = source + self.source_id = source_id self.location = location diff --git a/services/events/project/tests/models/test_meetup_model.py b/services/events/project/tests/models/test_meetup_model.py index 79fb80ab..6984597b 100644 --- a/services/events/project/tests/models/test_meetup_model.py +++ b/services/events/project/tests/models/test_meetup_model.py @@ -22,6 +22,7 @@ def test_add(self): entry=[entry], category="test category", source="test", + source_id=None, location="belfast", ) channel = Channel( diff --git a/services/events/project/tests/test_event_model.py b/services/events/project/tests/test_event_model.py index 65abad6d..81c3b934 100644 --- a/services/events/project/tests/test_event_model.py +++ b/services/events/project/tests/test_event_model.py @@ -25,6 +25,7 @@ def test_add_event(self): entry=[entry], category="test category", source="test", + source_id=None, location="belfast", ) db.session.add(event) diff --git a/services/events/project/tests/utils.py b/services/events/project/tests/utils.py index 65a9e0cc..a4b3fa41 100644 --- a/services/events/project/tests/utils.py +++ b/services/events/project/tests/utils.py @@ -18,6 +18,7 @@ def add_event( channel, category, source, + source_id, ): event = Event( name=name, @@ -32,6 +33,7 @@ def add_event( channel=channel, category=category, source=source, + source_id=source_id, ) db.session.add(event) db.session.commit()