def get(self, olid): book = Book(edition_id=olid) if 'M' in olid else Book(work_id=olid) return { "observations": [ r.dict() for r in Observation.query.filter( Observation.book_id == book.id).all() ] }
def post(self): topic = request.form.get('topic') winner = request.form.get('winner') candidates = request.form.get('candidates') description = request.form.get('description') source = request.form.get('source') username = session.get('username') if source: description += " (%s)" % source if not username: raise Exception('Login required') observations = request.form.getlist('observations[]') or [] for o in observations: observation = loads(o) observation['username'] = username Observations.persist_observation(observation) candidates = candidates or ' '.join( request.form.getlist('candidates[]')) rec = Recommendation.add( topic, winner, [Book.clean_olid(c) for c in candidates.strip().split(' ')], username, description) return rec.dict()
def post(self): topic = request.form.get('topic') winner = request.form.get('winner') candidates = request.form.get('candidates') description = request.form.get('description') source = request.form.get('source') username = session.get('username') if source: description += " (%s)" % source if not username: raise Exception('Login required') rec = Recommendation.add( topic, winner, [Book.clean_olid(c) for c in candidates.split(' ')], username, description) return rec.dict()
def persist_observation(cls, data): work_id = Book.clean_olid(data['work_id']) edition_id = data.get('edition_id') and Book.clean_olid( data.get('edition_id')) try: book = Book.get(work_olid=work_id, edition_olid=edition_id) except RexException: book = Book(work_olid=Book.clean_olid(data['work_id'])) if 'edition_id' in data: book.edition_olid = edition_id book.create() all_observations = {} for elem in data["observations"]: key, value = list(elem.items())[0] if key in all_observations: all_observations[key] += cls.MULTI_CHOICE_DELIMITER + value else: all_observations[key] = value for k, v in all_observations.items(): aspect = Aspect.get(label=k) try: observation = Observation.get(username=data["username"], aspect_id=aspect.id, book_id=book.id) observation.response = v observation.modified = datetime.utcnow() observation.update() except RexException: observation = Observation(username=data["username"], aspect_id=aspect.id, book_id=book.id, response=v).create()
def post(self): # Ensure that data was sent if not request.data: return "Bad Request", 400 data = loads(request.data) if not all(data.get(x) for x in ("username", "work_id", "observations")): return "Bad Request", 400 try: book = Book.get( work_olid=data['work_id'], edition_olid=data.get('edition_id') ) except RexException: book = Book(work_olid=data['work_id']) if 'edition_id' in data: book.edition_olid = data['edition_id'] book.create() all_observations = {} for elem in data["observations"]: key, value = list(elem.items())[0] if key in all_observations: all_observations[key] += self.MULTI_CHOICE_DELIMITER + value else: all_observations[key] = value for k, v in all_observations.items(): aspect = Aspect.get(label=k) try: observation = Observation.get(username=data["username"], aspect_id=aspect.id, book_id=book.id) observation.response = v observation.modified = datetime.utcnow() observation.update() except RexException: observation = Observation(username=data["username"], aspect_id=aspect.id, book_id=book.id, response=v).create() return "OK", 200