예제 #1
0
def profile_edit_view(request, slug=None):    
    user_profile = request.user.get_profile()
    if not slug or (not request.user.is_superuser and user_profile.slug != slug):
        return HttpResponseRedirect(reverse('django_neve_profile_edit', kwargs={'slug': user_profile.slug}))
        
    profile = get_object_or_404(Profile, slug=slug)
    user = profile.user

    template_name = 'django_neve/profile_edit_full.html'
    if request.is_ajax():
        template_name = 'django_neve/profile_edit.html'
 
    if request.method == "GET":
        if not request.is_ajax():
            try:
                medic = user.personalprofile
            except:
                messages.warning(request, 'Medical / Allergy profile missing.')

        
        form = ProfileEditForm(initial={
                'first_name': user.first_name,
                'last_name': user.last_name,
                'display_name': profile.display_name,
                'email': user.email,
                'user_id': user.username,
            })
                
        return render_to_response(template_name, {'form': form, 'slug': profile.slug }, context_instance=RequestContext(request))
    
    if request.method == "POST":
        form = ProfileEditForm(request.POST)
        if form.is_valid():
        
            user.email = form.cleaned_data['email']
            user.first_name = form.cleaned_data['first_name']
            user.last_name = form.cleaned_data['last_name']
            user.save()

            profile = user.get_profile()
            profile.display_name = form.cleaned_data['display_name']
            profile.save()
            saved = False

            messages.success(request, 'Profile details updated.')

            return HttpResponseRedirect(reverse('django_neve_profile_edit', kwargs={'slug': profile.slug}) )
  
        return render_to_response(template_name, {'form': form, 'slug': profile.slug }, context_instance=RequestContext(request))
    
    
    
    log = ActivityLog()
    log.ip = request.META['REMOTE_ADDR']
    log.user_agent = request.META['HTTP_USER_AGENT']
    log.type='alert'
    log.user = request.user
    log.action = 'Edit profile: Bad Method'
    log.save()
    
    response = HttpResponse("Error: Method not allowed")
    response.status_code=405
    return response
예제 #2
0
def register_view(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('django_neve_profile_edit', kwargs={'slug': request.user.get_profile().slug}))

    if request.method == "GET":
        form = RegisterForm()
        return TemplateResponse(request, 'django_neve/register.html', {'form': form})
    
    if request.method == "POST":
        form = RegisterForm(request.POST)
        
        if form.is_valid():
            user = User()
            
            ## set username to a random unique string
            username = gen_username()
            #ensure there is no collision
            while User._default_manager.filter(username=username).count() > 0:
                username = gen_username()
            
            user.username = username
            user.email = form.cleaned_data['email']
            user.set_password(form.cleaned_data['password'])
            
            user.first_name = form.cleaned_data['first_name']
            user.last_name = form.cleaned_data['last_name']
    
            user.is_active = False
            user.save()
            
            if AKISMET_KEY:
                akismet = Akismet(agent='django/1.3')
                akismet.key = AKISMET_KEY
                akismet.blog_url = "http://{host}/".format(host=request.META['HTTP_HOST'])
                if not akismet.verify_key():
                    log = ActivityLog()
                    log.ip = request.META['REMOTE_ADDR']
                    log.user = user
                    log.user_agent = request.META['HTTP_USER_AGENT']
                    log.type='alert'
                    log.action = 'Registration: Akistmet key not valid'
                    log.save()
                try:
                    spam = akismet.comment_check(None, {
                        'user_ip': request.META['REMOTE_ADDR'],
                        'user_agent': request.META['HTTP_USER_AGENT'],
                        'referrer': request.META['HTTP_REFERER'],
                        'comment_type': 'registration',
                        'comment_author_email': user.email,
                        'comment_author': '{first_name} {last_name}'.format(first_name=user.first_name, last_name=user.last_name),
                        }, build_data=False)
                
                except:
                    spam = False

                if spam:
                    log = ActivityLog()
                    log.ip = request.META['REMOTE_ADDR']
                    log.user = user
                    log.user_agent = request.META['HTTP_USER_AGENT']
                    log.type='alert'
                    log.action = 'Registration: Akismet reported as potential spammer'
                    log.save()

                    user.is_active = False
                    
            if 'HTTP_CF_IPCOUNTRY' in request.META:
                if request.META['HTTP_CF_IPCOUNTRY'] not in ('US', 'MX', 'CA',):
                    log = ActivityLog()
                    log.ip = request.META['REMOTE_ADDR']
                    log.user = user
                    log.user_agent = request.META['HTTP_USER_AGENT']
                    log.type='alert'
                    log.action = 'Registration: International registration'
                    log.save()

                    user.is_active = False
            
            user.save()
            
            log = ActivityLog()
            log.ip = request.META['REMOTE_ADDR']
            log.user = user
            log.user_agent = request.META['HTTP_USER_AGENT']
            log.type='info'
            log.action = 'Registration: User Created'
            log.save()
            
            profile = user.get_profile()
            profile.display_name = form.cleaned_data['display_name']
            profile.save()
            
            if user.is_active:
                login(request, authenticate(username=user.email, password=form.cleaned_data['password']))
                return HttpResponseRedirect(reverse('django_neve_profile_edit', kwargs={'slug': profile.slug}))
            else:

                sender = settings.EMAIL_FROM
                message = render_to_string('django_neve/email/activation.txt', {'new_user': user}, context_instance=RequestContext(request))
                
                send_mail('{site} account activation'.format(site=Site.objects.get_current().name),
                            message, 
                            sender,
                            [user.email], fail_silently=False)
                messages.info(request, 'You should be receiving an email shortly with instructions on how to activate your account.')

                return HttpResponseRedirect('/')
            
        else:
            ### form not valid
            return TemplateResponse(request, 'django_neve/register.html', {'form': form})


    
    log = ActivityLog()
    log.ip = request.META['REMOTE_ADDR']
    log.user_agent = request.META['HTTP_USER_AGENT']
    log.type='alert'
    log.action = 'Registration: Bad Method'
    log.save()
    
    response = HttpResponse("Error: Method not allowed")
    response.status_code=405
    return response