diff options
author | holyantony | 2015-05-08 11:59:21 +0530 |
---|---|---|
committer | holyantony | 2015-05-08 11:59:21 +0530 |
commit | f691ef58933bf33033389a1a468f655af19c1e16 (patch) | |
tree | 44d9812876286b2a855af4673c2f620e671a35af /forums | |
parent | 20998fcb52d55b25da2f63965c98e7b171d4f1ed (diff) | |
download | FOSSEE-Forum-f691ef58933bf33033389a1a468f655af19c1e16.tar.gz FOSSEE-Forum-f691ef58933bf33033389a1a468f655af19c1e16.tar.bz2 FOSSEE-Forum-f691ef58933bf33033389a1a468f655af19c1e16.zip |
Subject: Profile updation and Email notification
Description:
1. Confirmation link on registration via email
2. Profile updation on registration confirmation
3. Email notification posting question and answer
Diffstat (limited to 'forums')
-rw-r--r-- | forums/forms.py | 29 | ||||
-rw-r--r-- | forums/urls.py | 3 | ||||
-rw-r--r-- | forums/views.py | 112 |
3 files changed, 131 insertions, 13 deletions
diff --git a/forums/forms.py b/forums/forms.py index 6bfb184..3404ac2 100644 --- a/forums/forms.py +++ b/forums/forms.py @@ -11,7 +11,7 @@ from django.utils.translation import ugettext_lazy as _ from django.core.validators import MinLengthValidator, MinValueValidator, \ RegexValidator, URLValidator from django.template.defaultfilters import filesizeformat - +from website.models import Profile class UserLoginForm(forms.Form): @@ -35,6 +35,33 @@ class UserLoginForm(forms.Form): cleaned_data['user'] = user return cleaned_data +class ProfileForm(forms.ModelForm): + class Meta: + model = Profile + exclude = ['user', 'confirmation_code'] + + # def clean_picture(self): + # if 'picture' in self.cleaned_data and not \ + # self.cleaned_data['picture']: + # raise forms.ValidationError("Profile picture required!") + + first_name = forms.CharField() + last_name = forms.CharField() + + + + def __init__(self, user, *args, **kwargs): + initial = '' + if 'instance' in kwargs: + initial = kwargs["instance"] + if 'user' in kwargs: + user = kwargs["user"] + del kwargs["user"] + + super(ProfileForm, self).__init__(*args, **kwargs) + self.fields['first_name'].initial = user.first_name + self.fields['last_name'].initial = user.last_name + class RegisterForm(forms.Form): username = forms.CharField( diff --git a/forums/urls.py b/forums/urls.py index 3f257ea..31c679b 100644 --- a/forums/urls.py +++ b/forums/urls.py @@ -20,4 +20,7 @@ urlpatterns = patterns('', 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'), + url(r"^accounts/confirm/(?P<confirmation_code>\w+)/(?P<username>[\w. @-]+)/$", 'forums.views.confirm', name='confirm'), + url(r"^accounts/profile/(?P<username>[\w. @-]+)/$", 'forums.views.account_profile', name='profile'), + url(r"^accounts/view-profile/(?P<username>[\w. @-]+)/$", 'forums.views.account_view_profile', name='view_profile'), ) diff --git a/forums/views.py b/forums/views.py index e757ab9..f602a55 100644 --- a/forums/views.py +++ b/forums/views.py @@ -1,6 +1,6 @@ from django.http import HttpResponse, HttpResponseRedirect from django.contrib.auth import login, logout, authenticate -from django.shortcuts import render_to_response +from django.shortcuts import render_to_response , render from django.core.context_processors import csrf from django.template import RequestContext from django.contrib.auth.decorators import login_required @@ -8,15 +8,10 @@ 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 django.conf import settings - - - import random, string - from forums.forms import * +from website.models import * def account_register(request): context = {} @@ -38,9 +33,9 @@ def account_register(request): 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) + 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. """) @@ -54,6 +49,98 @@ def account_register(request): } context.update(csrf(request)) return render_to_response('forums/templates/user-register.html', context) + +def confirm(request, confirmation_code, username): + try: + user = User.objects.get(username=username) + profile = Profile.objects.get(user=user) + #if profile.confirmation_code == confirmation_code and user.date_joined > (timezone.now()-timezone.timedelta(days=1)): + if profile.confirmation_code == confirmation_code: + user.is_active = True + user.save() + user.backend='django.contrib.auth.backends.ModelBackend' + login(request,user) + print "In if" + messages.success(request, "Your account has been activated!. Please update your profile to complete your registration") + return HttpResponseRedirect('/accounts/profile/'+user.username) + else: + print "In else" + messages.success(request, "Something went wrong!. Please try again!") + return HttpResponseRedirect('/') + except Exception, e: + print "In excepw" + messages.success(request, "Your account not activated!. Please try again!") + return HttpResponseRedirect('/') + +@login_required +def account_logout(request): + context = RequestContext(request) + logout(request) + return HttpResponseRedirect('/') + +@login_required +def account_profile(request, username): + user = request.user + profile = Profile.objects.get(user_id=user.id) + #old_file_path = settings.MEDIA_ROOT + str(profile.picture) + #new_file_path = None + if request.method == 'POST': + form = ProfileForm(user, request.POST) + if form.is_valid(): + user.first_name = request.POST['first_name'] + user.last_name = request.POST['last_name'] + user.save() + form_data = form.save(commit=False) + form_data.user_id = user.id + + # if 'picture-clear' in request.POST and request.POST['picture-clear']: + # #if not old_file == new_file: + # if os.path.isfile(old_file_path): + # os.remove(old_file_path) + # + # if 'picture' in request.FILES: + # form_data.picture = request.FILES['picture'] + + form_data.save() + + """if 'picture' in request.FILES: + size = 128, 128 + filename = str(request.FILES['picture']) + ext = os.path.splitext(filename)[1] + if ext != '.pdf' and ext != '': + im = Image.open(settings.MEDIA_ROOT + str(form_data.picture)) + im.thumbnail(size, Image.ANTIALIAS) + ext = ext[1:] + + mimeType = ext.upper() + if mimeType == 'JPG': + mimeType = 'JPEG' + im.save(settings.MEDIA_ROOT + "user/" + str(user.id) + "/" + str(user.id) + "-thumb." + ext, mimeType) + form_data.thumb = 'user/' + str(user.id)+ '/' + str(user.id) + '-thumb.' + ext + form_data.save()""" + messages.success(request, "Your profile has been updated!") + return HttpResponseRedirect("/accounts/view-profile/" + user.username) + + context = {'form':form} + return render(request, 'forums/templates/profile.html', context) + else: + context = {} + context.update(csrf(request)) + instance = Profile.objects.get(user_id=user.id) + context['form'] = ProfileForm(user, instance = instance) + return render(request, 'forums/templates/profile.html', context) + +@login_required +def account_view_profile(request, username): + + user = User.objects.get(username = username) + profile = Profile.objects.filter(user = user)[0] + context = { + 'profile' : profile, + 'media_url' : settings.MEDIA_URL, + } + return render(request, 'forums/templates/view-profile.html', context) + def send_registration_confirmation(user): p = Profile.objects.get(user=user) @@ -69,14 +156,15 @@ def send_registration_confirmation(user): IIT Bombay. """.format( user.username, - "http://spoken-tutorial.org", - "http://spoken-tutorial.org/accounts/confirm/" + str(p.confirmation_code) + "/" + user.username + "http://fossee.in", + "http://localhost:8000/accounts/confirm/" + str(p.confirmation_code) + "/" + user.username ) email = EmailMultiAlternatives( subject, message, 'sysads@fossee.in', to = [user.email], bcc = [], cc = [], headers={'Reply-To': 'no-replay@spoken-tutorial.org', "Content-type":"text/html;charset=iso-8859-1"} ) + print message email.attach_alternative(message, "text/html") try: result = email.send(fail_silently=False) |