def save(self, commit=True): instance = super(BaseSubmissionForm, self).save(commit=False) user, authenticated = self.request.user, True if not self.request.user.is_authenticated(): # returns object if account alreay present user = create_new_account_internal(self.cleaned_data['email']) authenticated = False # submission author instance.created_by = user # is displayed only if authenticated instance.is_displayed = authenticated # validation hash hash_id = '' if not authenticated: hash_id = get_validation_hash(instance.title) instance.validation_hash = hash_id # description in HTML instance.description_html = compile_rest_to_html(instance.description) if commit: instance.save() return instance
def new_or_edit_submission(request, item_type, bound_form=False, submission=None): """ Users wants to submit a new link item, or continue editing a submission. There are multiple possible paths through the logic here. Careful about making changes. """ # User is going to edit their submission sub_type = None if isinstance(bound_form, models.Revision): new_item_or_edit = True sub_type = bound_form.entry.sub_type # for later on... # Cancel button, or a GET request elif request.POST.has_key('spc-cancel'): return redirect('spc-main-page') else: new_item_or_edit = False commit = False if request.POST.has_key('spc-edit'): new_item_or_edit = True bound_form = True if request.POST.has_key('spc-submit'): bound_form = True commit = True if request.POST.has_key('spc-preview'): bound_form = True buttontext_extra = '' if item_type == 'snippet' and request.method == 'GET': itemtype = 'snippet' new_item_or_edit = True elif item_type == 'package' and request.method == 'GET': itemtype = 'package' buttontext_extra = '(Upload ZIP file on next page)' new_item_or_edit = True #return not_implemented_yet(request, 48) elif item_type == 'link' and request.method == 'GET': itemtype = 'link' new_item_or_edit = True else: itemtype = request.POST.get('sub_type', sub_type) # Important: make a copy of ``field_order``, since it may be altered field_order = SUBS[itemtype].field_order[:] theform = get_form(request, form_class=SUBS[itemtype].form, field_order=field_order, bound=bound_form) # OK, having all that out of the way, lets process the user's submission # 0. Use the built-in forms checking to validate the fields. if new_item_or_edit or not(theform.is_valid()): return render_to_response('submission/new-item.html', {}, context_instance=RequestContext(request, {'item': theform, 'buttontext': 'Preview your submission', 'buttontext_extra': buttontext_extra, 'autocomplete_field': 'id_sub_tags', 'autocomplete_url': r'"spc-tagging-ajax"', 'pagetitle': 'Create a new submission'})) # 1. Create user account, if required if request.user.is_authenticated(): user = request.user authenticated = True else: user = create_new_account_internal(\ theform.cleaned_data['email']) authenticated = False # 2. Create the submission and revision or update an existing submission # with a new revision _, rev, tag_list = create_or_edit_submission_revision(request, item=theform, is_displayed=authenticated, user=user, submission=submission, commit=commit) # i.e. just previewing ... if not(commit): # 3. Create a Cancel/Edit/Submit form via a template to account for # hyperlinks and CSRF context = RequestContext(request) context['item'] = theform context['finish_button_text'] = 'Finish submission' # %% is required in below string to correctly format html = ("""<div id="spc-preview-edit-submit" class="spc-form"> <form action="{%% url spc-new-submission item_type='%s' %%}" method="POST" enctype="multipart/form-data">\n {%% csrf_token %%}\n {{item.as_hidden}} <div id="spc-preview-edit-submit-button-group"> <input class="btn btn-primary" type="submit" name="spc-cancel" value="Cancel" id="spc-item-cancel" />\n <input class="btn btn-primary" type="submit" name="spc-edit" value="Resume editing" id="spc-item-edit" />\n <input class="btn btn-success" type="submit" name="spc-submit" value="{{ finish_button_text }}" id="spc-item-submit"/>\n </div></form></div>""" % itemtype) resp = template.Template(html) extra_html = resp.render(template.Context(context)) return render_to_response('submission/item.html', {}, context_instance=RequestContext(request, {'item': rev, 'tag_list': tag_list, 'extra_html': extra_html, 'preview': True, })) else: # 4. Thank user and return with any extra messages, and send an email ctx_dict = {'user': user, 'item': rev, 'site': Site.objects.get_current() } # User is signed in if authenticated: show_url = True extra_messages = 'A confirmation email has been sent to you.' message = render_to_string('submission/email_user_thanks.txt', ctx_dict) else: show_url = False extra_messages = ('You have been sent an email to ' '<i>confirm your submission</i> and to create ' 'an account (if you do not have one ' 'already). <p>Unconfirmed submissions ' 'cannot be accepted, and <b>will be ' 'deleted</b> after %d days. Please sign in ' 'to avoid having to confirm your ' 'valuable submissions in the future.') % \ settings.SPC['unvalidated_subs_deleted_after'] # User is not signed in, but they have validated their email address if user.profile.is_validated: message = render_to_string(\ 'submission/email_validated_user_unvalidated_submission.txt', ctx_dict) else: # User is told they first need to create account before their # submission shows in the website message = render_to_string(\ 'submission/email_unvalidated_user_unvalidated_submission.txt', ctx_dict) send_email((user.email,), ("Thank you for your contribution " "to SciPy Central"), message=message) message = render_to_string('submission/email_website_admin.txt', ctx_dict) send_email((settings.SERVER_EMAIL,), ('A new/edited submission was ' 'made on SciPy Central'), message=message) return render_to_response('submission/thank-user.html', ctx_dict, context_instance=RequestContext(request, {'extra_message': extra_messages, 'show_url': show_url}))