Skip to content

Commit

Permalink
Merge pull request #130 from diggyk/master
Browse files Browse the repository at this point in the history
Add ability to update target time of quest to API
  • Loading branch information
jathanism committed Feb 5, 2016
2 parents 8be1eeb + 776d644 commit 1a1d23b
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
27 changes: 22 additions & 5 deletions hermes/handlers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ def post(self):
else:
self.created(data={"hosts": hosts, "totalHosts": len(hosts)})

print hostnames
log.info("HOST: Created {}".format(", ".join(hostnames)))
log.info("HOST: Created {}".format(", ".join(
[host["hostname"] for host in hostnames]
)))

def get(self):
"""**Get all Hosts**
Expand Down Expand Up @@ -939,7 +940,7 @@ def post(self):
:reqjson string hostname: (*optional*) The hostname of the Host of this Event
:regjson string hostnames: (*optional*) The list of hostnames for which we want to throw this Event
:reqjson string hostQuery: (*optional*) The external query to run to get Hosts for which to create Events
:regjson int queryId: (*optional*) The Quest ID which has hosts for which we want to create Events
:regjson int questId: (*optional*) The Quest ID which has hosts for which we want to create Events
:regjson string user: The user responsible for throwing this Event
:regjson int eventTypeId: The id of the EventType
:regjson string category: the category to use for the event
Expand Down Expand Up @@ -2373,6 +2374,7 @@ def put(self, id):
:reqjson string description: the new description of the Quest
:reqjson string creator: The new username of the creator (owner)
:regjson timestamp targetTime: Set a new targetTime
:reqheader Content-Type: The server expects a json body specified with
this header.
Expand All @@ -2394,13 +2396,23 @@ def put(self, id):

new_desc = None
new_creator = None
new_target_time = None
try:
if "description" in self.jbody:
new_desc = self.jbody["description"]
if "creator" in self.jbody:
new_creator = self.jbody['creator']
if not EMAIL_REGEX.match(new_creator):
new_creator += "@" + self.domain
if "targetTime" in self.jbody:
new_target_time = parser.parse(
self.jbody["targetTime"]
)
new_target_time = new_target_time.replace(tzinfo=None)
if new_target_time <= datetime.utcnow():
raise exc.BadRequest(
"Quest target date must be in future"
)
except KeyError as err:
raise exc.BadRequest(
"Missing Required Argument: {}".format(err.message))
Expand All @@ -2414,6 +2426,10 @@ def put(self, id):
quest = quest.update(
creator=new_creator
)
if new_target_time:
quest = quest.update(
target_time=new_target_time
)

except IntegrityError as err:
raise exc.Conflict(str(err.orig))
Expand All @@ -2422,10 +2438,11 @@ def put(self, id):

self.success(json)

log.info("QUEST [{}]: Update {}: {} {}".format(
log.info("QUEST [{}]: Updated: {} {} {}".format(
id,
"new description {}".format(new_desc) if new_desc else "",
"new creator {}".format(new_creator) if new_creator else ""
"new creator {}".format(new_creator) if new_creator else "",
"new target time {}".format(new_target_time) if new_target_time else ""
))

def delete(self, id):
Expand Down
8 changes: 6 additions & 2 deletions hermes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ def create(
def check_for_victory(self):
"""Test to see if all the Labors are completed.
Called when an associated Achievment is completed.
Called when a labor is completed.
"""
labors = self.session.query(Labor).filter(
and_(
Expand Down Expand Up @@ -1233,6 +1233,11 @@ def check_for_victory(self):
len(self.labors)
)

# if we aren't going to be sending email notifications, we
# can just stop here
if not settings.email_notifications:
return

# Get all the hosts that were in this labor
all_hosts = []
for labor in self.labors:
Expand Down Expand Up @@ -1276,7 +1281,6 @@ def check_for_victory(self):
msg, cc=owners
)


@classmethod
def get_open_quests(cls, session):
"""Get a list of open Quests
Expand Down
2 changes: 1 addition & 1 deletion hermes/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.7.10"
__version__ = "0.7.11"
3 changes: 2 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ pathtools==0.1.2
sphinx-autobuild==0.4.0
watchdog==0.8.2
sphinxcontrib-httpdomain==1.3.0
sphinx-rtd-theme==0.1.6
sphinx-rtd-theme==0.1.6
pytest-capturelog==0.7
40 changes: 37 additions & 3 deletions tests/api_tests/test_quests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import pytest
import requests
import logging

from .fixtures import tornado_server, tornado_app, sample_data1_server

Expand Down Expand Up @@ -102,6 +103,7 @@ def test_update(sample_data1_server):
client = sample_data1_server

target_time = datetime.utcnow() + timedelta(days=7)
target_time2 = datetime.utcnow() + timedelta(days=12)

# Create a quest
assert_created(
Expand Down Expand Up @@ -204,8 +206,38 @@ def test_update(sample_data1_server):
strip=["embarkTime", "labors"]
)

# Update the target time
assert_success(
client.update(
"/quests/1",
targetTime=str(target_time2)
),
{
"id": 1,
"creator": "tommy@example.com",
"targetTime": str(target_time2),
"description": "Newer desc",
"completionTime": None
},
strip=["embarkTime", "labors"]
)

# Verify target time updated
assert_success(
client.get("/quests/1"),
{
"id": 1,
"creator": "tommy@example.com",
"targetTime": str(target_time2),
"description": "Newer desc",
"completionTime": None
},
strip=["embarkTime", "labors"]
)


def test_quest_lifecycle(sample_data1_server):
def test_quest_lifecycle(sample_data1_server, caplog):
caplog.setLevel(logging.INFO)
client = sample_data1_server

# We start with 2 events in the test data
Expand Down Expand Up @@ -318,7 +350,8 @@ def test_quest_lifecycle(sample_data1_server):
)

# Ensure that the quest doesn't have a completion time yet
# Also, test two meta features: progress info and filtering to show only open labors
# Also, test two meta features: progress info and
# filtering to show only open labors
assert_success(
client.get("/quests/1?progressInfo=true"),
{
Expand Down Expand Up @@ -445,7 +478,8 @@ def test_quest_lifecycle(sample_data1_server):
)

# Ensure that the quest doesn't have a completion time yet
# Also, test two meta features: progress info and filtering to show only open labors
# Also, test two meta features: progress info and
# filtering to show only open labors
assert_success(
client.get("/quests/1?progressInfo=true&onlyOpenLabors=true&expand=labors&expand=eventtypes&expand=fates"),
{
Expand Down

0 comments on commit 1a1d23b

Please sign in to comment.