def import_user(request): """ View in the admin """ if request.method == 'POST': form = ImportForm(request.POST, request.FILES) if form.is_valid(): try: imported_user = User.import_user(form.files['file']) request.user.message_set.create(message="The import was successfull. %i users imported." % imported_user.count()) if form.cleaned_data['require_reactivation']: for user in [user for user in imported_user if user.is_active]: user.is_active = False user.set_new_activation_key() user.save() if form.cleaned_data['send_reactivation_email']: # Send activation email t = Template(form.cleaned_data['meassagetext']) c = { 'email': user.email, 'domain': RequestSite(request).domain, 'site_name': settings.SITE_NAME, 'uid': int_to_base36(user.id), 'user': user, 'protocol': request.is_secure() and 'https' or 'http', 'activation_key': user.activation_key, 'expiration_days': get_settings().acount_activation_days, } send_mail(_("Account activation on %s") % settings.SITE_NAME, t.render(Context(c)), None, [user.email]) return HttpResponseRedirect(urlresolvers.reverse('admin:accounts_user_changelist')) except: raise from django.forms.util import ErrorList msg = "An Error occured. The import file was propably malformed." form._errors["file"] = ErrorList([msg]) else: form = ImportForm() return render_to_response('admin/accounts/user/import.html', {'form': form, 'title':"Import User" }, RequestContext(request))
def import_user(request): """ View in the admin """ if request.method == 'POST': form = ImportForm(request.POST, request.FILES) if form.is_valid(): try: imported_user = User.import_user(form.files['file']) messages.success( request, "The import was successfull. %i users imported." % imported_user.count()) if form.cleaned_data['require_reactivation']: for user in [ user for user in imported_user if user.is_active ]: user.is_active = False user.set_new_activation_key() user.save() if form.cleaned_data['send_reactivation_email']: # Send activation email t = Template(form.cleaned_data['meassagetext']) c = { 'email': user.email, 'domain': RequestSite(request).domain, 'site_name': settings.SITE_NAME, 'uid': int_to_base36(user.id), 'user': user, 'protocol': request.is_secure() and 'https' or 'http', 'activation_key': user.activation_key, 'expiration_days': get_settings().acount_activation_days, } send_mail( _("Account activation on %s") % settings.SITE_NAME, t.render(c), None, [user.email]) return HttpResponseRedirect( reverse('admin:accounts_user_changelist')) except: raise from django.forms.utils import ErrorList msg = "An Error occured. The import file was probably malformed." form._errors["file"] = ErrorList([msg]) else: form = ImportForm() return render(request, 'admin/accounts/user/import.html', { 'form': form, 'title': "Import User" })