def post(self, request): """ Procesa el formulario :param request: objeto HttpRequest :return: HttpResponse con la respuesta """ publication = Publication() publication.publisher = request.user form = PublicationForm(request.POST, request.FILES, instance=publication) if form.is_valid(): # Guardar el anuncio new_publication = form.save() # Limpiar el formulario form = PublicationForm() # Devolvemos un mensaje de OK messages.success(request, 'Publicación creada correctamente') context = {'form': form, 'navbar': "publication-form"} return render(request, 'publications/publication_form.html', context)
def profile_add_publication(request, username): current_user = get_object_or_404(User, username=username) if request.method == 'POST': form = PublicationForm(request.POST) if form.is_valid(): publication = form.save() publication.title_ru = form.cleaned_data['title'] publication.save() authors = form.cleaned_data['authors_order'] conference = form.cleaned_data['conference_author'] unique_authors = [] unique_authors_obj = [] for obj in authors: try: if not obj['author'] in unique_authors: unique_authors.append(obj['author']) unique_authors_obj.append(obj) except KeyError: pass for obj in unique_authors_obj: try: if not obj['DELETE']: AuthorPublication.objects.create( publication=publication, author=obj['author'], order_by=obj['order_by']) except KeyError: break if conference: try: conference = conference[0] if not conference['DELETE']: form = conference['form'] author = conference['author'] Conference.objects.create(publication=publication, author=author, form=form) except (KeyError, IndexError): pass return redirect(profile, current_user) else: form = PublicationForm() form.fields['authors_order'].template_context = { "profile": current_user } form.fields['conference_author'].template_context = { "profile": current_user } context = {'form': form, 'profile': current_user} return render(request, 'lmnad/profile_add_publication.html', context)
def parse_bibtex(self, entry, bibtex_str, instance): authors = [] for a in entry.get('author', '').split(' and '): parts = a.split(' ') fields = ['given', 'family'] authors.append({k: v for k, v in zip(fields, parts)}) entry['author'] = authors entry['type'] = entry.get('type', entry['ENTRYTYPE']) entry['bibtex'] = bibtex_str if entry.get('owner'): del entry['owner'] pubform = PublicationForm(entry, instance=instance) if pubform.is_valid(): pubform.save() return pubform.instance.pk else: print('Submission invalid') print(pubform.errors)
def save_from_crossref(self, request, doi, instance=None, update=False): qs = self.get_queryset(request).filter(DOI=doi) if not qs.exists() or update: try: response = cr.works(doi) except Exception as e: print(e) return None entry = { k.replace('-', '_'): v for k, v in response['message'].items() if k.replace('-', '_') in [f.name for f in Publication._meta.fields] + ['author'] } entry['bibtex'] = self.get_bibtex(doi) pubform = PublicationForm(entry, instance=instance) if pubform.is_valid(): pubform.save() return pubform.instance.pk else: print('Submission invalid') print(pubform.errors)
def create(request): """ Creates a new publication and marks the user as having read it. """ form = PublicationForm(request.POST or None) if form.is_valid(): publication = form.save(commit=False) publication.owner = request.user publication.save() reading = Reading(user=request.user, publication=publication, rating=form.cleaned_data.get('rating', '1')) reading.save() request.user.message_set.create(message='You have created and read %s' % publication.title) return HttpResponseRedirect(request.POST.get('next', reverse('pub_latest'))) return render_to_response('publications/create.html', {'form': form}, context_instance=RequestContext(request) )