From 2e4a5f239283af883532e367bdb9d81c2c311f1c Mon Sep 17 00:00:00 2001 From: ashwinishinde Date: Wed, 18 Mar 2015 14:46:34 +0530 Subject: Subject:FOSSEE forum registration and category Description: 1) Registartion 2) added fossee category 3) removed Issues 4) removed Tutorials 5) resolve MyQuestion and MyAnswer Uid problem --- forums/forms.py | 52 ++++++++++++++++++++++++++++++++- forums/settings.py | 10 +++++-- forums/urls.py | 1 + forums/views.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 145 insertions(+), 4 deletions(-) (limited to 'forums') diff --git a/forums/forms.py b/forums/forms.py index 5a93b5f..6bfb184 100644 --- a/forums/forms.py +++ b/forums/forms.py @@ -1,5 +1,18 @@ from django import forms from django.contrib.auth import login, logout, authenticate +from django.utils.translation import ugettext_lazy as _ +from django.core.validators import MinLengthValidator, MinValueValidator, \ +RegexValidator, URLValidator +from captcha.fields import ReCaptchaField +from django.contrib.auth.models import User +from captcha.fields import ReCaptchaField + +from django.utils.translation import ugettext_lazy as _ +from django.core.validators import MinLengthValidator, MinValueValidator, \ +RegexValidator, URLValidator +from django.template.defaultfilters import filesizeformat + + class UserLoginForm(forms.Form): username = forms.CharField() @@ -8,13 +21,50 @@ class UserLoginForm(forms.Form): def clean(self): cleaned_data = self.cleaned_data username = cleaned_data.get('username') + print username password = cleaned_data.get('password') - if username is None or password is None: + print password + if username is None or password is None: raise forms.ValidationError("Invalid username or password") user = authenticate(username=username, password=password) + if not user: raise forms.ValidationError("Invalid username or password") if not user.is_active: raise forms.ValidationError("User is blocked") cleaned_data['user'] = user return cleaned_data + + +class RegisterForm(forms.Form): + username = forms.CharField( + label = _("Username"), + max_length = 30, + widget = forms.TextInput(), + required = True, + validators = [ + RegexValidator( + regex = '^[a-zA-Z0-9-_+.]*$', + message = 'Username required. 30 characters or fewer. \ + Letters, digits and @/./+/-/_ only.', + code = 'invalid_username' + ), + ] + ) + password = forms.CharField( + label = _("Password"), + widget = forms.PasswordInput(render_value = False), + min_length = 8, + ) + + password_confirm = forms.CharField( + label = _("Password (again)"), + widget = forms.PasswordInput(render_value = False), + min_length = 8, + ) + email = forms.EmailField( + label = _("Email"), + widget = forms.TextInput(), + required=True + ) + captcha = ReCaptchaField() diff --git a/forums/settings.py b/forums/settings.py index 88133d2..63ec9c5 100644 --- a/forums/settings.py +++ b/forums/settings.py @@ -141,6 +141,7 @@ INSTALLED_APPS = ( #'spoken_auth', 'compressor', 'debug_toolbar', + 'captcha', #'migrate_spoken', ) @@ -179,5 +180,10 @@ TEMPLATE_CONTEXT_PROCESSORS += ( ) COMPRESS_ROOT = PROJECT_DIR + "/static/" -COMPRESS_ENABLED = True -HTML_MINIFY = True +COMPRESS_ENABLED = True # disable in production Env +HTML_MINIFY = True # disable in production Env + +HTML_MINIFY = HTML_MINIFY +RECAPTCHA_PUBLIC_KEY = '6LemngMTAAAAAAC0Fkv0CQcavkTIIJ3LTDzi9gMq' +RECAPTCHA_PRIVATE_KEY = '6LemngMTAAAAAHZsIaY-G98QegM3htdGVEWbEJYL' +RECAPTCHA_USE_SSL = True diff --git a/forums/urls.py b/forums/urls.py index 90d61fb..3f257ea 100644 --- a/forums/urls.py +++ b/forums/urls.py @@ -18,5 +18,6 @@ urlpatterns = patterns('', # User account urls url(r'^accounts/login/', 'forums.views.user_login', name='user_login'), url(r'^accounts/logout/', 'forums.views.user_logout', name='user_logout'), + url(r'^accounts/register/', 'forums.views.account_register', name='user_register'), url(r'^migrate', 'migrate_spoken.views.chenage_drupal_userid_spoken', name='chenage_drupal_userid_spoken'), ) diff --git a/forums/views.py b/forums/views.py index b2dae90..3e82a90 100644 --- a/forums/views.py +++ b/forums/views.py @@ -2,16 +2,98 @@ from django.http import HttpResponse, HttpResponseRedirect from django.contrib.auth import login, logout, authenticate from django.shortcuts import render_to_response from django.core.context_processors import csrf +from django.template import RequestContext +from django.contrib.auth.decorators import login_required +from django.contrib.auth import authenticate, login, logout +from django.core.mail import EmailMultiAlternatives +from django.contrib import messages +from django.utils import timezone -from forums.forms import UserLoginForm +from django.conf import settings + + + +import random, string + +from forums.forms import * + +def account_register(request): + context = {} + print "account_registration" + print request.method + if request.method == 'POST': + form = RegisterForm(request.POST) + print form + print form.is_valid + if form.is_valid(): + + username = request.POST['username'] + print username + password = request.POST['password'] + print password + email = request.POST['email'] + print email + user = User.objects.create_user(username, email, password) + user.is_active = True + user.save() + confirmation_code = ''.join(random.choice(string.ascii_uppercase + string.digits + string.ascii_lowercase) for x in range(33)) + #p = Profile(user=user, confirmation_code=confirmation_code) + #p.save() + #send_registration_confirmation(user) + messages.success(request, """ + Please confirm your registration by clicking on the activation link which has been sent to your registered email id. + """) + return HttpResponseRedirect('/') + context = {'form':form} + return render_to_response('forums/templates/user-register.html', context,context_instance = RequestContext(request)) + else: + form = RegisterForm() + context = { + 'form': form + } + context.update(csrf(request)) + return render_to_response('forums/templates/user-register.html', context) + +def send_registration_confirmation(user): + p = Profile.objects.get(user=user) + #user.email = "k.sanmugam2@gmail.com" + # Sending email when an answer is posted + subject = 'Account Active Notification' + message = """Dear {0}, + Thank you for registering at {1}. You may activate your account by clicking on this link or copying and pasting it in your browser + {2} + Regards, + Admin + Spoken Tutorials + IIT Bombay. + """.format( + user.username, + "http://spoken-tutorial.org", + "http://spoken-tutorial.org/accounts/confirm/" + str(p.confirmation_code) + "/" + user.username + ) + email = EmailMultiAlternatives( + subject, message, 'administrator@spoken-tutorial.org', + to = [user.email], bcc = [], cc = [], + headers={'Reply-To': 'no-replay@spoken-tutorial.org', "Content-type":"text/html;charset=iso-8859-1"} + ) + #email.attach_alternative(message, "text/html") + try: + result = email.send(fail_silently=False) + except: + pass def user_login(request): if request.user.is_anonymous(): + if request.method == 'POST': form = UserLoginForm(request.POST) + if form.is_valid(): + cleaned_data = form.cleaned_data + user = cleaned_data.get("user") + login(request, user) if 'next' in request.POST: next_url = request.POST.get('next') @@ -19,12 +101,14 @@ def user_login(request): return HttpResponseRedirect('/') else: form = UserLoginForm() + print form.errors next_url = request.GET.get('next') context = { 'form': form, 'next': next_url } + context.update(csrf(request)) return render_to_response('forums/templates/user-login.html', context) else: -- cgit