示例#1
0
 def form_valid(self, form):
     self.object.refresh_from_db()
     action = self.request.POST.get('action', 'missing')
     if form.cleaned_data.get('marvel_id') is not None:
         self.object.marvel_id = form.cleaned_data['marvel_id']
     for field in self.object._meta.get_fields():
         if not field.concrete or field.auto_created:
             continue
         old_value = getattr(self.object, field.name, None)
         new_value = form.cleaned_data.get(field.name, None)
         if (old_value in ('', None) or action == 'all') and new_value not in (None, ''):
             setattr(self.object, field.name, new_value)
     utils.update_field_from_marvel(
         book=self.object,
         marvel_result=utils.get_marvel_book(self.object),
         field='author_artist')
     self.object.save()
     return super().form_valid(form)
示例#2
0
def search_marvel(request, pk, field):
    book = get_object_or_404(Book, id=int(pk))
    provider = MarvelBookInfoProvider()
    if book.marvel_id:
        provider.get(book.marvel_id)
    if provider.total < 1 and book.isbn:
        provider.query(book.isbn)
    if provider.total < 1:
        provider.query(book.title.split('(')[0].strip())
    if provider.total < 1:
        messages.add_message(request, messages.ERROR, 'Failed to "%s" on Marvel API' % book.title)
    else:
        marvel_result = list(provider.mapping.values())[0]
        succeeded = utils.update_field_from_marvel(book, marvel_result, field)
        if not succeeded:
            level = messages.INFO if succeeded else messages.ERROR
            messages.add_message(request, level, 'Failed to update %s from Marvel' % field)
    return redirect(
        request.META.get('HTTP_REFERER', reverse('admin:books_book_changelist')))