def post_log_entry(): data = json.loads(request.data) or {} if not data: return jsonify(error='Empty request'), 400 data['user_id'] = g.current_user.id data['organization_id'] = data.get('organization_id') or g.current_user.organization_id if 'entry_time' in data: del data['entry_time'] try: entry = LogEntry.from_dict(data) except Exception as e: return jsonify(error=e.message), 400 db.session.add(entry) db.session.commit() return jsonify(entry.to_json())
def post_log_entry(): data = json.loads(request.data) or {} if not data: return jsonify(error='Empty request'), 400 data['user_id'] = g.current_user.id data['organization_id'] = data.get( 'organization_id') or g.current_user.organization_id if 'entry_time' in data: del data['entry_time'] try: entry = LogEntry.from_dict(data) except Exception as e: return jsonify(error=e.message), 400 db.session.add(entry) db.session.commit() return jsonify(entry.to_json())
def test_entries_and_comments(self): headers = self.get_api_headers('admin', 'test') org = Organization.query.first() org_id = org.id admin_user = User.query.first() org = Organization.query.first() log_entry = LogEntry() log_entry.log_entry_type = 'INFO' log_entry.entry_title = 'First Entry' log_entry.entry_description = 'This is where the description goes' log_entry.user_id = admin_user.id log_entry.organization_id = org.id db.session.add(log_entry) db.session.commit() # GET /log_entry response = self.client.get('/log_entry') self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry = data['log_entries'][0] self.assertEquals(entry['entry_title'], log_entry.entry_title) # GET /log_entry/:id response = self.client.get('/log_entry/%s' % log_entry.id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry = data self.assertEquals(entry['entry_title'], log_entry.entry_title) # GET /log_entry?organization_id=:organization_id response = self.client.get('/log_entry?organization_id=%s' % org.id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry = data['log_entries'][0] self.assertEquals(entry['entry_title'], log_entry.entry_title) # GET /log_entry?organization_id=90 (404) response = self.client.get('/log_entry?organization_id=90') self.assertEquals(response.status_code, 204) # POST /log_entry data = { 'log_entry_type' : 'INFO', 'entry_title' : 'Another Entry', 'entry_description' : "I'm Scotty P, knaw I'm sayin'", 'organization_id' : org.id } response = self.client.post('/log_entry', data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry_id = data.get('id') # GET /log_entry/:id response = self.client.get('/log_entry/%s' % entry_id) self.assertEquals(response.status_code, 200) entry = json.loads(response.data) self.assertEquals(entry['entry_title'], data['entry_title']) # PUT /log_entry data = { 'log_entry_type' : 'INFO', 'entry_title' : 'Changed Entry', 'entry_description' : "I'm Scotty P, knaw I'm sayin'", 'organization_id' : org.id } response = self.client.put('/log_entry/%s' % entry_id, data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) entry = json.loads(response.data) self.assertEquals(entry['entry_title'], data['entry_title']) # GET /log_entry/:id response = self.client.get('/log_entry/%s' % entry_id) self.assertEquals(response.status_code, 200) entry = json.loads(response.data) self.assertEquals(entry['entry_title'], data['entry_title']) # DELETE /log_entry/:id response = self.client.delete('/log_entry/%s' % entry_id, headers=headers) self.assertEquals(response.status_code, 204) response = self.client.get('/log_entry/%s' % entry_id) self.assertEquals(response.status_code, 204) # GET /log_entry_comment?log_entry_id=:id response = self.client.get('/log_entry_comment?log_entry_id=%s' % entry_id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(data['log_entry_comments'], []) # POST /log_entry_comment data = { 'comment' : u'これもいつか過ぎ去るものです', 'log_entry_id' : entry_id } response = self.client.post('/log_entry_comment', data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) comment = json.loads(response.data) self.assertEquals(comment['comment'], data['comment']) log_entry_comment_id = comment['id'] # GET /log_entry_comment?log_entry_id=:id response = self.client.get('/log_entry_comment?log_entry_id=%s' % entry_id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(len(data['log_entry_comments']), 1) # PUT /log_entry_comment/:id data = { 'comment' : u'This too shall pass.', 'log_entry_id' : entry_id } response = self.client.put('/log_entry_comment/%s' % log_entry_comment_id, data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) # GET /log_entry_comment/:id response = self.client.get('/log_entry_comment/%s' % log_entry_comment_id) self.assertEquals(response.status_code, 200) comment = json.loads(response.data) self.assertEquals(comment['comment'], data['comment']) # DELETE /log_entry_comment/:id response = self.client.delete('/log_entry_comment/%s' % log_entry_comment_id, headers=headers) self.assertEquals(response.status_code, 204) # GET /log_entry_comment/:id response = self.client.get('/log_entry_comment/%s' % log_entry_comment_id) self.assertEquals(response.status_code, 204)
def test_entries_and_comments(self): headers = self.get_api_headers("admin", "test") org = Organization.query.first() org_id = org.id admin_user = User.query.first() org = Organization.query.first() log_entry = LogEntry() log_entry.log_entry_type = "INFO" log_entry.entry_title = "First Entry" log_entry.entry_description = "This is where the description goes" log_entry.user_id = admin_user.id log_entry.organization_id = org.id db.session.add(log_entry) db.session.commit() # GET /log_entry response = self.client.get("/log_entry") self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry = data["log_entries"][0] self.assertEquals(entry["entry_title"], log_entry.entry_title) # GET /log_entry/:id response = self.client.get("/log_entry/%s" % log_entry.id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry = data self.assertEquals(entry["entry_title"], log_entry.entry_title) # GET /log_entry?organization_id=:organization_id response = self.client.get("/log_entry?organization_id=%s" % org.id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry = data["log_entries"][0] self.assertEquals(entry["entry_title"], log_entry.entry_title) # GET /log_entry?organization_id=90 (404) response = self.client.get("/log_entry?organization_id=90") self.assertEquals(response.status_code, 204) # POST /log_entry data = { "log_entry_type": "INFO", "entry_title": "Another Entry", "entry_description": "I'm Scotty P, knaw I'm sayin'", "organization_id": org.id, } response = self.client.post("/log_entry", data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry_id = data.get("id") # GET /log_entry/:id response = self.client.get("/log_entry/%s" % entry_id) self.assertEquals(response.status_code, 200) entry = json.loads(response.data) self.assertEquals(entry["entry_title"], data["entry_title"]) # PUT /log_entry data = { "log_entry_type": "INFO", "entry_title": "Changed Entry", "entry_description": "I'm Scotty P, knaw I'm sayin'", "organization_id": org.id, } response = self.client.put("/log_entry/%s" % entry_id, data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) entry = json.loads(response.data) self.assertEquals(entry["entry_title"], data["entry_title"]) # GET /log_entry/:id response = self.client.get("/log_entry/%s" % entry_id) self.assertEquals(response.status_code, 200) entry = json.loads(response.data) self.assertEquals(entry["entry_title"], data["entry_title"]) # DELETE /log_entry/:id response = self.client.delete("/log_entry/%s" % entry_id, headers=headers) self.assertEquals(response.status_code, 204) response = self.client.get("/log_entry/%s" % entry_id) self.assertEquals(response.status_code, 204) # GET /log_entry_comment?log_entry_id=:id response = self.client.get("/log_entry_comment?log_entry_id=%s" % entry_id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(data["log_entry_comments"], []) # POST /log_entry_comment data = {"comment": u"これもいつか過ぎ去るものです", "log_entry_id": entry_id} response = self.client.post("/log_entry_comment", data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) comment = json.loads(response.data) self.assertEquals(comment["comment"], data["comment"]) log_entry_comment_id = comment["id"] # GET /log_entry_comment?log_entry_id=:id response = self.client.get("/log_entry_comment?log_entry_id=%s" % entry_id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(len(data["log_entry_comments"]), 1) # PUT /log_entry_comment/:id data = {"comment": u"This too shall pass.", "log_entry_id": entry_id} response = self.client.put( "/log_entry_comment/%s" % log_entry_comment_id, data=json.dumps(data), headers=headers ) self.assertEquals(response.status_code, 200) # GET /log_entry_comment/:id response = self.client.get("/log_entry_comment/%s" % log_entry_comment_id) self.assertEquals(response.status_code, 200) comment = json.loads(response.data) self.assertEquals(comment["comment"], data["comment"]) # DELETE /log_entry_comment/:id response = self.client.delete("/log_entry_comment/%s" % log_entry_comment_id, headers=headers) self.assertEquals(response.status_code, 204) # GET /log_entry_comment/:id response = self.client.get("/log_entry_comment/%s" % log_entry_comment_id) self.assertEquals(response.status_code, 204)
def test_entries_and_comments(self): headers = self.get_api_headers('admin', 'test') org = Organization.query.first() org_id = org.id admin_user = User.query.first() org = Organization.query.first() log_entry = LogEntry() log_entry.log_entry_type = 'INFO' log_entry.entry_title = 'First Entry' log_entry.entry_description = 'This is where the description goes' log_entry.user_id = admin_user.id log_entry.organization_id = org.id db.session.add(log_entry) db.session.commit() # GET /log_entry response = self.client.get('/log_entry') self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry = data['log_entries'][0] self.assertEquals(entry['entry_title'], log_entry.entry_title) # GET /log_entry/:id response = self.client.get('/log_entry/%s' % log_entry.id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry = data self.assertEquals(entry['entry_title'], log_entry.entry_title) # GET /log_entry?organization_id=:organization_id response = self.client.get('/log_entry?organization_id=%s' % org.id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry = data['log_entries'][0] self.assertEquals(entry['entry_title'], log_entry.entry_title) # GET /log_entry?organization_id=90 (404) response = self.client.get('/log_entry?organization_id=90') self.assertEquals(response.status_code, 204) # POST /log_entry data = { 'log_entry_type': 'INFO', 'entry_title': 'Another Entry', 'entry_description': "I'm Scotty P, knaw I'm sayin'", 'organization_id': org.id } response = self.client.post('/log_entry', data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) data = json.loads(response.data) entry_id = data.get('id') # GET /log_entry/:id response = self.client.get('/log_entry/%s' % entry_id) self.assertEquals(response.status_code, 200) entry = json.loads(response.data) self.assertEquals(entry['entry_title'], data['entry_title']) # PUT /log_entry data = { 'log_entry_type': 'INFO', 'entry_title': 'Changed Entry', 'entry_description': "I'm Scotty P, knaw I'm sayin'", 'organization_id': org.id } response = self.client.put('/log_entry/%s' % entry_id, data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) entry = json.loads(response.data) self.assertEquals(entry['entry_title'], data['entry_title']) # GET /log_entry/:id response = self.client.get('/log_entry/%s' % entry_id) self.assertEquals(response.status_code, 200) entry = json.loads(response.data) self.assertEquals(entry['entry_title'], data['entry_title']) # DELETE /log_entry/:id response = self.client.delete('/log_entry/%s' % entry_id, headers=headers) self.assertEquals(response.status_code, 204) response = self.client.get('/log_entry/%s' % entry_id) self.assertEquals(response.status_code, 204) # GET /log_entry_comment?log_entry_id=:id response = self.client.get('/log_entry_comment?log_entry_id=%s' % entry_id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(data['log_entry_comments'], []) # POST /log_entry_comment data = {'comment': u'これもいつか過ぎ去るものです', 'log_entry_id': entry_id} response = self.client.post('/log_entry_comment', data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) comment = json.loads(response.data) self.assertEquals(comment['comment'], data['comment']) log_entry_comment_id = comment['id'] # GET /log_entry_comment?log_entry_id=:id response = self.client.get('/log_entry_comment?log_entry_id=%s' % entry_id) self.assertEquals(response.status_code, 200) data = json.loads(response.data) self.assertEquals(len(data['log_entry_comments']), 1) # PUT /log_entry_comment/:id data = {'comment': u'This too shall pass.', 'log_entry_id': entry_id} response = self.client.put('/log_entry_comment/%s' % log_entry_comment_id, data=json.dumps(data), headers=headers) self.assertEquals(response.status_code, 200) # GET /log_entry_comment/:id response = self.client.get('/log_entry_comment/%s' % log_entry_comment_id) self.assertEquals(response.status_code, 200) comment = json.loads(response.data) self.assertEquals(comment['comment'], data['comment']) # DELETE /log_entry_comment/:id response = self.client.delete('/log_entry_comment/%s' % log_entry_comment_id, headers=headers) self.assertEquals(response.status_code, 204) # GET /log_entry_comment/:id response = self.client.get('/log_entry_comment/%s' % log_entry_comment_id) self.assertEquals(response.status_code, 204)