Пример #1
0
    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
Пример #2
0
    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()