def signature(request): # Intercept all requests if we can't use signature if not request.acl.usercp.can_use_signature(): return error403(request) if request.user.signature_ban: return request.theme.render_to_response('usercp/signature_banned.html', context_instance=RequestContext(request, { 'tab': 'signature', })); siggy_text = '' message = request.messages.get_message('usercp_signature') if request.method == 'POST': form = SignatureForm(request.POST, request=request, initial={'signature': request.user.signature}) if form.is_valid(): request.user.signature = form.cleaned_data['signature'] if request.user.signature: request.user.signature_preparsed = signature_markdown(request.acl, request.user.signature) else: request.user.signature_preparsed = None request.user.save(force_update=True) request.messages.set_flash(Message(_("Your signature has been changed.")), 'success', 'usercp_signature') return redirect(reverse('usercp_signature')) else: message = Message(form.non_field_errors()[0], 'error') else: form = SignatureForm(request=request, initial={'signature': request.user.signature}) return request.theme.render_to_response('usercp/signature.html', context_instance=RequestContext(request, { 'message': message, 'tab': 'signature', 'form': FormLayout(form), }));
def avatar(request): message = request.messages.get_message('usercp_avatar') return request.theme.render_to_response('usercp/avatar.html', context_instance=RequestContext(request, { 'message': message, 'tab': 'avatar', }));
def decorator(*args, **kwargs): request = args[0] if request.user.avatar_ban: return render_to_response('usercp/avatar_banned.html', context_instance=RequestContext( request, {'tab': 'avatar'})) return f(*args, **kwargs)
def crop(request, upload=False): if upload and (not request.user.avatar_temp or not 'upload' in settings.avatars_types): return error404(request) if not upload and request.user.avatar_type != 'upload': messages.error(request, _("Crop Avatar option is avaiable only when you use uploaded image as your avatar."), 'usercp_avatar') return redirect(reverse('usercp_avatar')) message = request.messages.get_message('usercp_avatar') if request.method == 'POST': if request.csrf.request_secure(request): try: image_path = settings.MEDIA_ROOT + 'avatars/' if upload: source = Image.open(image_path + request.user.avatar_temp) else: source = Image.open(image_path + request.user.avatar_original) width, height = source.size aspect = float(width) / float(request.POST['crop_b']) crop_x = int(aspect * float(request.POST['crop_x'])) crop_y = int(aspect * float(request.POST['crop_y'])) crop_w = int(aspect * float(request.POST['crop_w'])) crop = source.crop((crop_x, crop_y, crop_x + crop_w, crop_y + crop_w)) if upload: image_name, image_extension = path(request.user.avatar_temp).splitext() else: image_name, image_extension = path(request.user.avatar_original).splitext() image_name = '%s_%s%s' % (request.user.pk, random_string(8), image_extension) resizeimage(crop, settings.AVATAR_SIZES[0], image_path + image_name, info=source.info, format=source.format) for size in settings.AVATAR_SIZES[1:]: resizeimage(crop, size, image_path + str(size) + '_' + image_name, info=source.info, format=source.format) request.user.delete_avatar_image() if upload: request.user.delete_avatar_original() request.user.avatar_type = 'upload' request.user.avatar_original = '%s_org_%s%s' % (request.user.pk, random_string(8), image_extension) source.save(image_path + request.user.avatar_original) request.user.delete_avatar_temp() request.user.avatar_image = image_name request.user.avatar_crop = [str(float(request.POST[x])) for x in ('crop_x', 'crop_y', 'crop_w')] request.user.save(force_update=True) messages.success(request, _("Your avatar has been cropped."), 'usercp_avatar') return redirect(reverse('usercp_avatar')) except Exception: message = Message(_("Form contains errors."), messages.ERROR) else: message = Message(_("Request authorisation is invalid."), messages.ERROR) return render_to_response('usercp/avatar_crop.html', context_instance=RequestContext(request, { 'message': message, 'after_upload': upload, 'avatar_size': settings.AVATAR_SIZES[0], 'avatar_crop': request.user.avatar_crop if not upload else None, 'source': 'avatars/%s' % (request.user.avatar_temp if upload else request.user.avatar_original), 'tab': 'avatar'}));
def username(request): if not request.acl.usercp.show_username_change(): return error404(request) changes_left = request.acl.usercp.changes_left(request.user) next_change = None if request.acl.usercp.changes_expire() and not changes_left: next_change = request.user.namechanges.filter( date__gte=timezone.now() - timedelta(days=request.acl.usercp.acl['changes_expire']), ).order_by('-date')[0] next_change = next_change.date + timedelta(days=request.acl.usercp.acl['changes_expire']) message = request.messages.get_message('usercp_username') if request.method == 'POST': if not changes_left: message = Message(_("You have exceeded the maximum number of name changes."), messages.ERROR) form = UsernameChangeForm(request=request) else: org_username = request.user.username form = UsernameChangeForm(request.POST, request=request) if form.is_valid(): request.user.set_username(form.cleaned_data['username']) request.user.save(force_update=True) request.user.sync_username() request.user.namechanges.create(date=timezone.now(), old_username=org_username) messages.success(request, _("Your username has been changed."), 'usercp_username') # Alert followers of namechange alert_time = timezone.now() bulk_alerts = [] alerted_users = [] for follower in request.user.follows_set.iterator(): alerted_users.append(follower.pk) alert = Alert(user=follower, message=ugettext_lazy("User that you are following, %(username)s, has changed his name to %(newname)s").message, date=alert_time) alert.strong('username', org_username) alert.profile('newname', request.user) alert.hydrate() bulk_alerts.append(alert) if bulk_alerts: Alert.objects.bulk_create(bulk_alerts) User.objects.filter(id__in=alerted_users).update(alerts=F('alerts') + 1) # Hop back return redirect(reverse('usercp_username')) message = Message(form.non_field_errors()[0], messages.ERROR) else: form = UsernameChangeForm(request=request) return render_to_response('usercp/username.html', context_instance=RequestContext(request, { 'message': message, 'changes_left': changes_left, 'form': form, 'next_change': next_change, 'changes_history': request.user.namechanges.order_by('-date')[:10], 'tab': 'username'}));
def gallery(request): if not 'gallery' in settings.avatars_types: return error404(request) allowed_avatars = [] galleries = [] for directory in path( settings.STATICFILES_DIRS[0]).joinpath('avatars').dirs(): if directory[-7:] != '_locked' and directory[-8:] != '_default': gallery = {'name': directory[-7:], 'avatars': []} avatars = directory.files('*.gif') avatars += directory.files('*.jpg') avatars += directory.files('*.jpeg') avatars += directory.files('*.png') for item in avatars: gallery['avatars'].append('/'.join(path(item).splitall()[-2:])) galleries.append(gallery) allowed_avatars += gallery['avatars'] if not allowed_avatars: messages.info(request, _("No avatar galleries are available at the moment."), 'usercp_avatar') return redirect(reverse('usercp_avatar')) message = request.messages.get_message('usercp_avatar') if request.method == 'POST': if request.csrf.request_secure(request): new_avatar = request.POST.get('avatar_image') if new_avatar in allowed_avatars: request.user.delete_avatar() request.user.avatar_type = 'gallery' request.user.avatar_image = new_avatar request.user.save(force_update=True) messages.success( request, _("Your avatar has been changed to one from gallery."), 'usercp_avatar') return redirect(reverse('usercp_avatar')) message = Message(_("Selected Avatar is incorrect."), messages.ERROR) else: message = Message(_("Request authorisation is invalid."), messages.ERROR) return render_to_response('usercp/avatar_gallery.html', context_instance=RequestContext( request, { 'message': message, 'galleries': galleries, 'tab': 'avatar' }))
def options(request): message = request.messages.get_message('usercp_options') if request.method == 'POST': form = UserForumOptionsForm(request.POST, request=request) if form.is_valid(): request.user.hide_activity = form.cleaned_data['hide_activity'] request.user.allow_pds = form.cleaned_data['allow_pds'] request.user.receive_newsletters = form.cleaned_data['newsletters'] request.user.timezone = form.cleaned_data['timezone'] request.user.subscribe_start = form.cleaned_data['subscribe_start'] request.user.subscribe_reply = form.cleaned_data['subscribe_reply'] request.user.save(force_update=True) request.messages.set_flash( Message(_("Forum options have been changed.")), 'success', 'usercp_options') return redirect(reverse('usercp')) message = Message(form.non_field_errors()[0], 'error') else: form = UserForumOptionsForm(request=request, initial={ 'newsletters': request.user.receive_newsletters, 'hide_activity': request.user.hide_activity, 'allow_pds': request.user.allow_pds, 'timezone': request.user.timezone, 'subscribe_start': request.user.subscribe_start, 'subscribe_reply': request.user.subscribe_reply, }) return request.theme.render_to_response('usercp/options.html', context_instance=RequestContext( request, { 'message': message, 'tab': 'options', 'form': FormLayout(form) }))
def credentials(request): message = request.messages.get_message('usercp_credentials') if request.method == 'POST': form = CredentialsChangeForm(request.POST, request=request) if form.is_valid(): token = random_string(12) request.user.email_user(request, 'users/new_credentials', _("Activate new Sign-In Credentials"), {'token': token}) request.session['new_credentials'] = { 'token': token, 'email_hash': request.user.email_hash, 'new_email': form.cleaned_data['new_email'], 'new_password': form.cleaned_data['new_password'], } if form.cleaned_data['new_email']: request.user.email = form.cleaned_data['new_email'] request.messages.set_flash( Message( _("We have sent e-mail message to your new e-mail address with link you have to click to confirm change of your sign-in credentials. This link will be valid only for duration of this session, do not sign out until you confirm change!" )), 'success', 'usercp_credentials') else: request.messages.set_flash( Message( _("We have sent e-mail message to your e-mail address with link you have to click to confirm change of your sign-in credentials. This link will be valid only for duration of this session, do not sign out until you confirm change!" )), 'success', 'usercp_credentials') return redirect(reverse('usercp_credentials')) message = Message(form.non_field_errors()[0], 'error') else: form = CredentialsChangeForm(request=request) return request.theme.render_to_response('usercp/credentials.html', context_instance=RequestContext( request, { 'message': message, 'form': FormLayout(form), 'tab': 'credentials', }))
def upload(request): if not 'upload' in request.settings.avatars_types: return error404(request) message = request.messages.get_message('usercp_avatar') if request.method == 'POST': form = UploadAvatarForm(request.POST, request.FILES, request=request) if form.is_valid(): request.user.delete_avatar_temp() image = form.cleaned_data['avatar_upload'] image_name, image_extension = path(smart_str(image.name.lower())).splitext() image_name = '%s_tmp_%s%s' % (request.user.pk, random_string(8), image_extension) image_path = settings.MEDIA_ROOT + 'avatars/' + image_name request.user.avatar_temp = image_name with open(image_path, 'wb+') as destination: for chunk in image.chunks(): destination.write(chunk) request.user.save() try: if is_zipfile(image_path): # Composite file upload raise ValidationError() image = Image.open(image_path) if not image.format in ['GIF', 'PNG', 'JPEG']: raise ValidationError() image.seek(0) image.save(image_path) if request.POST.get('js_check'): return redirect(reverse('usercp_avatar_upload_crop')) # Redirect to crop page didnt happen, handle avatar with old school hollywood way image_path = settings.MEDIA_ROOT + 'avatars/' source = Image.open(image_path + request.user.avatar_temp) image_name, image_extension = path(request.user.avatar_temp).splitext() image_name = '%s_%s%s' % (request.user.pk, random_string(8), image_extension) resizeimage(source, settings.AVATAR_SIZES[0], image_path + image_name, info=source.info, format=source.format) for size in settings.AVATAR_SIZES[1:]: resizeimage(source, size, image_path + str(size) + '_' + image_name, info=source.info, format=source.format) # Update user model one more time request.user.delete_avatar_image() request.user.delete_avatar_original() request.user.avatar_type = 'upload' request.user.avatar_original = '%s_org_%s%s' % (request.user.pk, random_string(8), image_extension) source.save(image_path + request.user.avatar_original) request.user.delete_avatar_temp() request.user.avatar_image = image_name request.user.save(force_update=True) # Set message and adios! request.messages.set_flash(Message(_("Your avatar has changed.")), 'success', 'usercp_avatar') return redirect(reverse('usercp_avatar')) except ValidationError: request.user.delete_avatar() request.user.default_avatar(request.settings) message = Message(_("Only gif, jpeg and png files are allowed for member avatars."), 'error') else: message = Message(form.non_field_errors()[0], 'error') else: form = UploadAvatarForm(request=request) return request.theme.render_to_response('usercp/avatar_upload.html', context_instance=RequestContext(request, { 'message': message, 'form': FormLayout(form), 'tab': 'avatar', }));