def create_status(activity): ''' unfortunately, it's not QUITE as simple as deserialiing it ''' # render the json into an activity object serializer = activitypub.activity_objects[activity['type']] activity = serializer(**activity) try: model = models.activity_models[activity.type] except KeyError: # not a type of status we are prepared to deserialize return None # ignore notes that aren't replies to known statuses if activity.type == 'Note': reply = models.Status.objects.filter( remote_id=activity.inReplyTo ).first() if not reply: return None # look up books book_urls = [] if hasattr(activity, 'inReplyToBook'): book_urls.append(activity.inReplyToBook) if hasattr(activity, 'tag'): book_urls += [t['href'] for t in activity.tag if t['type'] == 'Book'] for remote_id in book_urls: books_manager.get_or_create_book(remote_id) return activity.to_model(model)
def handle_tag(activity): ''' someone is tagging a book ''' user = get_or_create_remote_user(activity['actor']) if not user.local: # ordered collection weirndess so we can't just to_model book = books_manager.get_or_create_book(activity['object']['id']) name = activity['object']['target'].split('/')[-1] name = unquote_plus(name) models.Tag.objects.get_or_create(user=user, book=book, name=name)
def get_book_from_isbn(self): ''' search by isbn ''' search_result = books_manager.first_search_result( self.isbn, min_confidence=0.999 ) if search_result: # raises ConnectorException return books_manager.get_or_create_book(search_result.key) return None
def get_book_from_isbn(self): ''' search by isbn ''' search_result = books_manager.first_search_result(self.isbn, min_confidence=0.995) if search_result: try: # don't crash the import when the connector fails return books_manager.get_or_create_book(search_result.key) except ConnectorException: pass return None
def get_book_from_title_author(self): ''' search by title and author ''' search_term = construct_search_term(self.data['Title'], self.data['Author']) search_result = books_manager.first_search_result(search_term, min_confidence=0.995) if search_result: try: return books_manager.get_or_create_book(search_result.key) except ConnectorException: pass return None
def handle_shelve(activity): ''' putting a book on a shelf ''' user = get_or_create_remote_user(activity['actor']) book = books_manager.get_or_create_book(activity['object']) try: shelf = models.Shelf.objects.get(remote_id=activity['target']) except models.Shelf.DoesNotExist: return if shelf.user != user: # this doesn't add up. return shelf.books.add(book) shelf.save()
def resolve_foreign_key(model, remote_id): ''' look up the remote_id on an activity json field ''' if model in [models.Edition, models.Work]: return books_manager.get_or_create_book(remote_id) result = model.objects if hasattr(model.objects, 'select_subclasses'): result = result.select_subclasses() result = result.filter(remote_id=remote_id).first() if not result: raise ValueError('Could not resolve remote_id in %s model: %s' % \ (model.__name__, remote_id)) return result
def resolve_book(request): ''' figure out the local path to a book from a remote_id ''' remote_id = request.POST.get('remote_id') book = books_manager.get_or_create_book(remote_id) return redirect('/book/%d' % book.id)