Esempio n. 1
0
def email_after_submission(instance):
    """
    Send email notifications to created user and admin
    for new/ edited submissions
    """
    if not isinstance(instance, models.Revision):
        raise TypeError('Revision object should be passed as argument')

    email_context = {
        'user': instance.created_by,
        'item': instance,
        'site': Site.objects.get_current()
    }

    # signed in users
    if instance.is_displayed:
        message = render_to_string('submission/email_user_thanks.txt',
                                   email_context)
    else:
        # if user registered but not signed in
        if instance.created_by.profile.is_validated:
            message = render_to_string(
                'submission/email_validated_user_unvalidated_submission.txt',
                email_context)

        # unknown users
        else:
            message = render_to_string(
                'submission/email_unvalidated_user_unvalidated_submission.txt',
                email_context)

    # email submitted user
    send_email((instance.created_by.email, ),
               ('Thank you for your contribution to '
                'SciPy Central'),
               message=message)

    # email admin
    message = render_to_string('submission/email_website_admin.txt')
    send_email((settings.SERVER_EMAIL, ), ('A new/edited submission '
                                           'was made on SciPy Central'),
               message=message)
Esempio n. 2
0
def email_after_submission(instance):
    """
    Send email notifications to created user and admin
    for new/ edited submissions
    """
    if not isinstance(instance, models.Revision):
        raise TypeError('Revision object should be passed as argument')

    email_context = {
        'user': instance.created_by,
        'item': instance,
        'site': Site.objects.get_current()
    }

    # signed in users
    if instance.is_displayed:
        message = render_to_string('submission/email_user_thanks.txt',
                                   email_context)
    else:
        # if user registered but not signed in
        if instance.created_by.profile.is_validated:
            message = render_to_string(
                'submission/email_validated_user_unvalidated_submission.txt',
                email_context)

        # unknown users
        else:
            message = render_to_string(
                'submission/email_unvalidated_user_unvalidated_submission.txt',
                email_context)

    # email submitted user
    send_email((instance.created_by.email,), ('Thank you for your contribution to '
                                              'SciPy Central'), message=message)

    # email admin
    message = render_to_string('submission/email_website_admin.txt')
    send_email((settings.SERVER_EMAIL,), ('A new/edited submission '
                                          'was made on SciPy Central'),
               message=message)
Esempio n. 3
0
def profile_page_edit(request, slug):
    """
    User wants to edit his/her profile page.
    """
    # First verify that request.user is the same as slug
    if request.user.profile.slug != slug:
        return page_404_error(request,
                              ('You are not authorized to edit that '
                               'profile. Only that user may edit it.'))

    if request.POST:
        form = forms.ProfileEditForm(request.POST)
        if form.is_valid():
            # Update profile information
            user = request.user

            previous_email = ''
            if form.cleaned_data['email'] != user.email:
                previous_email = user.email
                user.email = form.cleaned_data['email']
                user.save()

            user.profile.affiliation = form.cleaned_data['affiliation']
            user.profile.country = form.cleaned_data['country']
            user.profile.bio = form.cleaned_data['bio']
            user.profile.bio_html = compile_rest_to_html(
                form.cleaned_data['bio'])
            user.profile.uri = form.cleaned_data['uri']

            user.profile.save()

            tag_list = get_and_create_tags(form.cleaned_data['interests'])

            # First delete all the user's previous interests, so we can update
            # them with the new ones
            for interest in models.InterestCreation.objects.filter(user=user):
                #.profile.interests.all():
                interest.delete()

            # Add user's interests through the intermediate
            # model instance
            for tag in tag_list:
                tag_intermediate = models.InterestCreation(user=user.profile,
                                                           tag=tag)
                tag_intermediate.save()
                logger.debug('User "%s" added interest "%s" to their profile'%\
                             (user.profile.slug, str(tag)))

            # Resave the user profile to capture their interests in the search
            # results.
            user.profile.save()

            if previous_email:
                ctx_dict = {
                    'new_email': user.email,
                    'admin_email': settings.DEFAULT_FROM_EMAIL,
                    'username': user.username
                }
                message = render_to_string(\
                              'person/email_about_changed_email_address.txt',
                              ctx_dict)
                send_email((previous_email, ), ("SciPy Central: change of "
                                                "email address"),
                           message=message)

            return redirect(profile_page, user.profile.slug)

    else:
        user = request.user
        interests = ','.join(
            list(set([str(intr) for intr in user.profile.interests.all()])))
        fields = {
            'uri': user.profile.uri,
            'email': user.email,
            'bio': user.profile.bio,
            'interests': interests,
            'country': user.profile.country,
            'affiliation': user.profile.affiliation,
            'pk': user.id,
        }
        form = forms.ProfileEditForm(fields)

    return render_to_response('submission/new-item.html', {},
                              context_instance=RequestContext(
                                  request, {
                                      'form': form,
                                      'buttontext': 'Update your profile',
                                      'pagetitle': 'Update your profile',
                                  }))
Esempio n. 4
0
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}))
Esempio n. 5
0
def profile_page_edit(request, slug):
    """
    User wants to edit his/her profile page.
    """
    # First verify that request.user is the same as slug
    if request.user.profile.slug != slug:
        return page_404_error(request, ('You are not authorized to edit that '
                                        'profile. Only that user may edit it.'))


    if request.POST:
        form = forms.ProfileEditForm(request.POST)
        if form.is_valid():
            # Update profile information
            user = request.user

            previous_email = ''
            if form.cleaned_data['email'] != user.email:
                previous_email = user.email
                user.email = form.cleaned_data['email']
                user.save()

            user.profile.affiliation = form.cleaned_data['affiliation']
            user.profile.country = form.cleaned_data['country']
            user.profile.bio = form.cleaned_data['bio']
            user.profile.bio_html = compile_rest_to_html(form.cleaned_data['bio'])
            user.profile.uri = form.cleaned_data['uri']

            user.profile.save()

            tag_list = get_and_create_tags(form.cleaned_data['interests'])

            # First delete all the user's previous interests, so we can update
            # them with the new ones
            for interest in models.InterestCreation.objects.filter(user=user):
                #.profile.interests.all():
                interest.delete()

            # Add user's interests through the intermediate
            # model instance
            for tag in tag_list:
                tag_intermediate = models.InterestCreation(user=user.profile,
                                                           tag=tag)
                tag_intermediate.save()
                logger.debug('User "%s" added interest "%s" to their profile'%\
                             (user.profile.slug, str(tag)))

            # Resave the user profile to capture their interests in the search
            # results.
            user.profile.save()

            if previous_email:
                ctx_dict = {'new_email': user.email,
                            'admin_email': settings.DEFAULT_FROM_EMAIL,
                            'username': user.username}
                message = render_to_string(\
                              'person/email_about_changed_email_address.txt',
                              ctx_dict)
                send_email((previous_email,), ("SciPy Central: change of "
                                        "email address"), message=message)


            return redirect(profile_page, user.profile.slug)

    else:
        user = request.user
        interests = ','.join(list(set([str(intr) for intr in user.profile.interests.all()])))
        fields =  {'uri': user.profile.uri,
                   'email': user.email,
                   'bio': user.profile.bio,
                   'interests': interests,
                   'country': user.profile.country,
                   'affiliation': user.profile.affiliation,
                   'pk': user.id,
                   }
        form = forms.ProfileEditForm(fields)

    return render_to_response('submission/new-item.html', {},
                          context_instance=RequestContext(request,
                                {'form': form,
                                 'buttontext': 'Update your profile',
                                 'pagetitle': 'Update your profile',
                                }))