def save_and_login(self, request): 'Create this user in the database, log that user in, and return the Profile' first_name = self.cleaned_data['first_name'] last_name = self.cleaned_data['last_name'] #username = defaultfilters.slugify(self.cleaned_data['email'])[:30] username = Profile.objects.unique_username(first_name, last_name) email = self.cleaned_data['email'] password = self.cleaned_data['password'] new_user = User.objects.create_user(username, email, password) new_user.first_name = first_name new_user.last_name = last_name new_user.save() geuser = Profile(user = new_user, timezone=self.cleaned_data['timezone']) referer = None referer_id = None if 'f' in request.GET: referer_id = request.GET['f'] elif 'f' in request.COOKIES: referer_id = request.COOKIES['f'] if referer_id: try: referer = Profile.objects.get(pk = long(referer_id)) referer.add_participation_points(points=5) referer.save() geuser.referer = referer except (Profile.DoesNotExist, ValueError): pass geuser.save() if self.cleaned_data['picture']: relative_pic_name = save_picture(geuser, self.cleaned_data['picture']) geuser.avatar = relative_pic_name #else: # relative_pic_name = save_gravatar(geuser) geuser.save() if self.cleaned_data['group']: group_id = self.cleaned_data['group'] group = Group.objects.get(pk = group_id) geuser.groups.add(group) user = authenticate(username=new_user.username, password=password) login(request, user) return geuser
def crop(request, campaign_slug): 'Allow the user to select how to crop their profile picture, and then actually crop it' import Image base_width = 288 base_height = 235 response_map = {} geuser = request.user.get_profile() if 'picture' in request.FILES: # Check uploaded image is acutally an image if not request.FILES['picture'].content_type.startswith('image'): request.user.message_set.create( message='Invalid file type - please select a jpg, png or gif file') return render_to_response('profile/avatar_crop.html', response_map, context_instance=RequestContext(request)) has_pic = False if geuser.has_pic(): current_pic = geuser.profile_pic_filename(abspath=True, variant='full') if os.path.exists(current_pic): has_pic = True response_map['has_pic'] = has_pic image = Image.open( current_pic ) if request.method == 'POST': if 'picture' in request.FILES: # Uploaded new picture, save and go back to crop # Delete current picture try: current_full = geuser.profile_pic_filename(abspath=True, variant='full') os.unlink(current_full) current_dark = geuser.profile_pic_filename(abspath=True, variant='dark') os.unlink(current_dark) except (OSError, IOError): # IGNORE:W0704 # No previous picture - no problem pass # Save new pic new_profile_pic = request.FILES['picture'] relative_pic_name = save_picture(geuser, new_profile_pic) geuser.avatar = relative_pic_name geuser.save() crop_url = reverse('profile_crop', kwargs={'campaign_slug': campaign_slug}) return HttpResponseRedirect(crop_url) else: # Cropped picture, save and continue to home page left = int(request.POST['left']) top = int(request.POST['top']) width = int(request.POST['width']) height = int(request.POST['height']) cropped = image.crop( (left, top, width + left, height + top) ) resized = cropped.resize( (base_width, base_height) ) resized.save( geuser.profile_pic_filename(abspath=True) ) geuser.avatar = geuser.profile_pic_filename(abspath=False) geuser.save() # Clear status dashboard from cache so new pic will show for campaign in geuser.campaign_set.all(): cache.delete( Entry.objects.dashboard_cache_key(campaign, geuser) ) home_url = reverse('campaign_home', kwargs={'campaign_slug': campaign_slug}) return HttpResponseRedirect(home_url) if has_pic: (width, height) = image.size # The sizes we need to fill the picture box on the dashboard response_map['base_width'] = base_width response_map['base_height'] = base_height response_map['aspect_ratio'] = float(base_width) / float(base_height) response_map['crop_img_width'] = width response_map['crop_img_height'] = height response_map['start_width'] = min(width, base_width) response_map['start_height'] = min( height, response_map['start_width'] / response_map['aspect_ratio']) response_map['crop_img'] = settings.MEDIA_URL + \ geuser.profile_pic_filename(abspath=False, variant='full') +\ '?no_cache='+ str(random.randint(0, 1000)) response_map['crop_img_dark'] = \ settings.MEDIA_URL + \ geuser.profile_pic_filename(abspath=False, variant='dark') +\ '?no_cache='+ str(random.randint(0, 1000)) return render_to_response('profile/avatar_crop.html', response_map, context_instance=RequestContext(request))