Skip to content
This repository has been archived by the owner on Apr 17, 2021. It is now read-only.

Commit

Permalink
Allow events to be deduped on source id
Browse files Browse the repository at this point in the history
  • Loading branch information
alistairjcbrown committed Dec 23, 2018
1 parent b984f17 commit e61ef16
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
62 changes: 39 additions & 23 deletions services/events/project/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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)

Expand All @@ -55,6 +64,7 @@ def index():
location=location,
category=category,
source=source,
source_id=source_id,
)

db.session.add(event)
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions services/events/project/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -183,6 +184,7 @@ def __init__(
entry,
category,
source,
source_id,
location,
):
self.name = name
Expand All @@ -195,6 +197,7 @@ def __init__(
self.entry = entry
self.category = category
self.source = source
self.source_id = source_id
self.location = location


Expand Down
1 change: 1 addition & 0 deletions services/events/project/tests/models/test_meetup_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_add(self):
entry=[entry],
category="test category",
source="test",
source_id=None,
location="belfast",
)
channel = Channel(
Expand Down
1 change: 1 addition & 0 deletions services/events/project/tests/test_event_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions services/events/project/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def add_event(
channel,
category,
source,
source_id,
):
event = Event(
name=name,
Expand All @@ -32,6 +33,7 @@ def add_event(
channel=channel,
category=category,
source=source,
source_id=source_id,
)
db.session.add(event)
db.session.commit()
Expand Down

0 comments on commit e61ef16

Please sign in to comment.