def add_journal(request, journal_id=None): """ Handles new and existing journals """ if journal_id is None: journal = models.Journal() else: journal = get_object_or_404(models.Journal, id=journal_id) form_hash = None JournalTitleFormSet = inlineformset_factory(models.Journal, models.JournalTitle, form=JournalTitleForm, extra=1, can_delete=True) JournalMissionFormSet = inlineformset_factory(models.Journal, models.JournalMission, form=JournalMissionForm, extra=1, can_delete=True) if request.method == "POST": journalform = JournalForm(request.POST, request.FILES, instance=journal, prefix='journal') titleformset = JournalTitleFormSet(request.POST, instance=journal, prefix='title') missionformset = JournalMissionFormSet(request.POST, instance=journal, prefix='mission') if 'pend' in request.POST: journal_form_hash = PendingPostData(request.POST).pend( resolve(request.get_full_path()).url_name, request.user) form_hash = journal_form_hash messages.info(request, MSG_FORM_SAVED_PARTIALLY) else: if journalform.is_valid() and titleformset.is_valid( ) and missionformset.is_valid(): saved_journal = journalform.save_all(creator=request.user) if not journal_id: saved_journal.join( user_request_context. get_current_user_active_collection(), request.user) titleformset.save() missionformset.save() messages.info(request, MSG_FORM_SAVED) if request.POST.get( 'form_hash', None) and request.POST['form_hash'] != 'None': models.PendedForm.objects.get( form_hash=request.POST['form_hash']).delete() # record the event models.DataChangeEvent.objects.create( user=request.user, content_object=saved_journal, collection=models.Collection.objects.get_default_by_user( request.user), event_type='updated' if journal_id else 'added') return HttpResponseRedirect( reverse('journal.dash', args=[saved_journal.id])) else: messages.error(request, MSG_FORM_MISSING) else: if request.GET.get('resume', None): pended_post_data = PendingPostData.resume( request.GET.get('resume')) journalform = JournalForm(pended_post_data, request.FILES, instance=journal, prefix='journal') titleformset = JournalTitleFormSet(pended_post_data, instance=journal, prefix='title') missionformset = JournalMissionFormSet(pended_post_data, instance=journal, prefix='mission') else: journalform = JournalForm(instance=journal, prefix='journal') titleformset = JournalTitleFormSet(instance=journal, prefix='title') missionformset = JournalMissionFormSet(instance=journal, prefix='mission') # Recovering Journal Cover url. try: has_cover_url = journal.cover.url except ValueError: has_cover_url = False # Recovering Journal Logo url. try: has_logo_url = journal.logo.url except ValueError: has_logo_url = False return render_to_response('journalmanager/add_journal.html', { 'journal': journal, 'add_form': journalform, 'titleformset': titleformset, 'missionformset': missionformset, 'has_cover_url': has_cover_url, 'has_logo_url': has_logo_url, 'form_hash': form_hash if form_hash else request.GET.get('resume', None), 'is_new': False if journal_id else True, }, context_instance=RequestContext(request))
def add_journal(request, journal_id=None): """ Handles new and existing journals """ user_collections = models.get_user_collections(request.user.id) if journal_id is None: journal = models.Journal() else: journal = get_object_or_404(models.Journal, id=journal_id) form_hash = None JournalTitleFormSet = inlineformset_factory(models.Journal, models.JournalTitle, form=JournalTitleForm, extra=1, can_delete=True) JournalMissionFormSet = inlineformset_factory(models.Journal, models.JournalMission, form=JournalMissionForm, extra=1, can_delete=True) if request.method == "POST": journalform = JournalForm(request.POST, request.FILES, instance=journal, prefix='journal', collections_qset=user_collections) titleformset = JournalTitleFormSet(request.POST, instance=journal, prefix='title') missionformset = JournalMissionFormSet(request.POST, instance=journal, prefix='mission') if 'pend' in request.POST: journal_form_hash = PendingPostData(request.POST).pend(resolve(request.get_full_path()).url_name, request.user) form_hash = journal_form_hash messages.info(request, MSG_FORM_SAVED_PARTIALLY) else: if journalform.is_valid() and titleformset.is_valid() and missionformset.is_valid(): saved_journal = journalform.save_all(creator=request.user) titleformset.save() missionformset.save() messages.info(request, MSG_FORM_SAVED) if request.POST.get('form_hash', None) and request.POST['form_hash'] != 'None': models.PendedForm.objects.get(form_hash=request.POST['form_hash']).delete() # record the event models.DataChangeEvent.objects.create( user=request.user, content_object=saved_journal, collection=models.Collection.objects.get_default_by_user(request.user), event_type='updated' if journal_id else 'added' ) return HttpResponseRedirect(reverse('journal.dash', args=[saved_journal.id])) else: messages.error(request, MSG_FORM_MISSING) else: if request.GET.get('resume', None): pended_post_data = PendingPostData.resume(request.GET.get('resume')) journalform = JournalForm(pended_post_data, request.FILES, instance=journal, prefix='journal', collections_qset=user_collections) titleformset = JournalTitleFormSet(pended_post_data, instance=journal, prefix='title') missionformset = JournalMissionFormSet(pended_post_data, instance=journal, prefix='mission') else: journalform = JournalForm(instance=journal, prefix='journal', collections_qset=user_collections) titleformset = JournalTitleFormSet(instance=journal, prefix='title') missionformset = JournalMissionFormSet(instance=journal, prefix='mission') # Recovering Journal Cover url. try: has_cover_url = journal.cover.url except ValueError: has_cover_url = False # Recovering Journal Logo url. try: has_logo_url = journal.logo.url except ValueError: has_logo_url = False return render_to_response('journalmanager/add_journal.html', { 'journal': journal, 'add_form': journalform, 'titleformset': titleformset, 'missionformset': missionformset, 'has_cover_url': has_cover_url, 'has_logo_url': has_logo_url, 'form_hash': form_hash if form_hash else request.GET.get('resume', None), 'is_new': False if journal_id else True, }, context_instance=RequestContext(request))