def contact_form_submit(request, content_id, contact_form_id): content = get_object_or_404(BaseContent, pk=content_id).get_real_instance() contact_form = get_object_or_404(ContactForm, pk=contact_form_id, content__pk=content_id) redirect = None if request.method == 'POST': form = contact_form.get_form(request) if form.is_valid(): sentcontactform = form.save(request, content, contact_form) if not sentcontactform: return errors_to_session(request, content, form, contact_form_id) if contact_form.redirect_to: redirect = contact_form.redirect_to if contact_form.redirect_to_with_params: redirect = "%s?content_id=%s&contact_form_id=%s&sentcontactform=%s" % ( redirect, content.pk, contact_form_id, sentcontactform.pk) else: redirect = request.META.get('HTTP_REFERER', content.public_link()) if contact_form.sent_msg: send_info(request, contact_form.sent_msg) else: return errors_to_session(request, content, form, contact_form_id) if not redirect: raise Http404 return HttpResponseRedirect(redirect)
def subscription_form(request, basecontent_id): content = BaseContent.objects.get(id=basecontent_id) subscribable = content.subscribable_set.actives() if not subscribable: return HttpResponseRedirect(content.get_absolute_url()) subscribable = subscribable[0] app_label, module_name = subscribable.class_name.split('.') content_type = ContentType.objects.get(app_label=app_label, model=module_name) model_class = content_type.model_class() data = None if request.POST: data = request.POST form = model_class.class_form()(data) if form.is_valid(): subscription = form.save(commit=False) subscription.subscribable = subscribable subscription.save() subscriber_listing_url = reverse('subscriber_listing', args=(basecontent_id, )) send_info( request, _('Request send successfully. See <a href="%(subscriber_listing)s">suscriber list</a>') % { 'subscriber_listing': subscriber_listing_url, }, ) url_redirect = content.get_absolute_url() invalidate_cache_for_path(url_redirect) return HttpResponseRedirect(url_redirect) return render_to_response('subscription/subscription_form.html', {'form': form, 'content': content, }, context_instance=RequestContext(request))
def merengue_logout_service(request): """Do the logout and then send a message to the user""" response = logout_service(request, config_loader_path='plugins.saml2.saml_config_loader.merengue_config_loader', next_page='/') if 'SAMLResponse' in request.GET: # we started the logout send_info(request, _('Thank you for your visit')) return response
def confirm_password_recovery(request, username, recovery_hash): user = get_object_or_404(User, username=username) if recovery_hash != hashlib.md5( '%s-%s-%s' % (user.username, user.password, settings.SECRET_KEY)).hexdigest(): raise Http404 if request.method == 'POST': password_form = PasswordForm(request.POST) if password_form.is_valid(): password = password_form.get_password() user.set_password(password) user.save() auth_user = authenticate(username=user.username, password=password) auth_login(request, auth_user) send_info( request, _('Your password has been changed. Welcome back %s') % user) return HttpResponseRedirect('/') else: password_form = PasswordForm() return render_to_response('registration/change_password.html', { 'password_form': password_form, 'user': user }, context_instance=RequestContext(request))
def createdir(request, repository_name, path='', base_template=FILEBROWSER_BASE_TEMPLATE, url_prefix=None): repository = get_object_or_404(Repository, name=repository_name) if request.POST: dirname = request.POST.get('dirname', None) dirname = dirname.strip() errornote = None if not dirname: errornote = _('You must introduce the name of the new folder') elif repository.check_dir(join(path, dirname)): errornote = _(u'Folder %(dirname)s already exists') % {'dirname': dirname} if errornote: # si hay algun tipo de error return render_to_response('filebrowser/createdir.html', {'repository': repository, 'base_template': base_template, 'path': path, 'errornote': errornote, 'url_prefix': url_prefix}, context_instance=RequestContext(request)) repository.create_dir(join(path, dirname)) message = _('Folder created successfully') send_info(request, message) return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={'repository_name': repository.name, 'path': path}, url_prefix=url_prefix)) else: dirs, files, parents, dirname = repository.list_directory(path) return render_to_response('filebrowser/createdir.html', {'repository': repository, 'base_template': base_template, 'path': path, 'parents': parents, 'url_prefix': url_prefix}, context_instance=RequestContext(request))
def subscription_form(request, basecontent_id): content = BaseContent.objects.get(id=basecontent_id) subscribable = content.subscribable_set.actives() if not subscribable: return HttpResponseRedirect(content.get_absolute_url()) subscribable = subscribable[0] app_label, module_name = subscribable.class_name.split('.') content_type = ContentType.objects.get(app_label=app_label, model=module_name) model_class = content_type.model_class() data = None if request.POST: data = request.POST form = model_class.class_form()(data) if form.is_valid(): subscription = form.save(commit=False) subscription.subscribable = subscribable subscription.save() subscriber_listing_url = reverse('subscriber_listing', args=(basecontent_id, )) send_info( request, _('Request send successfully. See <a href="%(subscriber_listing)s">suscriber list</a>' ) % { 'subscriber_listing': subscriber_listing_url, }, ) url_redirect = content.get_absolute_url() invalidate_cache_for_path(url_redirect) return HttpResponseRedirect(url_redirect) return render_to_response('subscription/subscription_form.html', { 'form': form, 'content': content, }, context_instance=RequestContext(request))
def contact_form_submit(request, content_id, contact_form_id): content = get_object_or_404(BaseContent, pk=content_id).get_real_instance() contact_form = get_object_or_404(ContactForm, pk=contact_form_id, content__pk=content_id) redirect = None if request.method == 'POST': form = contact_form.get_form(request) if form.is_valid(): sentcontactform = form.save(request, content, contact_form) if not sentcontactform: return errors_to_session(request, content, form, contact_form_id) if contact_form.redirect_to: redirect = contact_form.redirect_to if contact_form.redirect_to_with_params: redirect = "%s?content_id=%s&contact_form_id=%s&sentcontactform=%s" % (redirect, content.pk, contact_form_id, sentcontactform.pk) else: redirect = request.META.get('HTTP_REFERER', content.public_link()) if contact_form.sent_msg: send_info(request, contact_form.sent_msg) else: return errors_to_session(request, content, form, contact_form_id) if not redirect: raise Http404 return HttpResponseRedirect(redirect)
def can_create_new_thread(request, content): user = request.user if not user: login_url = '%s?next=%s' % (get_login_url(), request.get_full_path()) return HttpResponseRedirect(login_url) elif not has_permission(content, user, 'edit'): send_info(request, ugettext('You don\'t have permission to create a new thread')) return HttpResponseRedirect(content.get_absolute_url()) return None
def merengue_logout_service(request): """Do the logout and then send a message to the user""" response = logout_service( request, config_loader_path= 'plugins.saml2.saml_config_loader.merengue_config_loader', next_page='/') if 'SAMLResponse' in request.GET: # we started the logout send_info(request, _('Thank you for your visit')) return response
def can_create_new_thread(request, content): user = request.user if not user: login_url = '%s?next=%s' % (get_login_url(), request.get_full_path()) return HttpResponseRedirect(login_url) elif not has_permission(content, user, 'edit'): send_info( request, ugettext('You don\'t have permission to create a new thread')) return HttpResponseRedirect(content.get_absolute_url()) return None
def upload(request, repository_name, path='', base_template=FILEBROWSER_BASE_TEMPLATE, url_prefix=None): repository = get_object_or_404(Repository, name=repository_name) if request.method == 'POST': for k, f in request.FILES.items(): if k.startswith('file_'): idx = k[5:] title = request.POST.get('title_' + idx).encode('utf8') description = request.POST.get('description_' + idx).encode('utf8') file_name = unicodedata.normalize('NFKD', force_unicode( f.name)).encode('ascii', 'ignore') fout = repository.create_file(join(path, file_name)) for chunk in f.chunks(): fout.write(chunk) fout.close() metadata_name = join(path, file_name + '.metadata') metadata_path = repository.encode_path( repository.get_absolute_path(metadata_name)) mout = open(metadata_path, 'w') mout.write(title) mout.write('\n\n') mout.write(description) mout.write('\n') mout.close() send_info(request, _('Files uploaded successfully')) return HttpResponseRedirect( filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={ 'repository_name': repository.name, 'path': path }, url_prefix=url_prefix)) else: dirs, files, parents, dirname = repository.list_directory(path) return render_to_response('filebrowser/upload.html', { 'repository': repository, 'base_template': base_template, 'path': path, 'parents': parents, 'url_prefix': url_prefix }, context_instance=RequestContext(request))
def createdir(request, repository_name, path='', base_template=FILEBROWSER_BASE_TEMPLATE, url_prefix=None): repository = get_object_or_404(Repository, name=repository_name) if request.POST: dirname = request.POST.get('dirname', None) dirname = dirname.strip() errornote = None if not dirname: errornote = _('You must introduce the name of the new folder') elif repository.check_dir(join(path, dirname)): errornote = _(u'Folder %(dirname)s already exists') % { 'dirname': dirname } if errornote: # si hay algun tipo de error return render_to_response('filebrowser/createdir.html', { 'repository': repository, 'base_template': base_template, 'path': path, 'errornote': errornote, 'url_prefix': url_prefix }, context_instance=RequestContext(request)) repository.create_dir(join(path, dirname)) message = _('Folder created successfully') send_info(request, message) return HttpResponseRedirect( filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={ 'repository_name': repository.name, 'path': path }, url_prefix=url_prefix)) else: dirs, files, parents, dirname = repository.list_directory(path) return render_to_response('filebrowser/createdir.html', { 'repository': repository, 'base_template': base_template, 'path': path, 'parents': parents, 'url_prefix': url_prefix }, context_instance=RequestContext(request))
def confirm_password_recovery(request, username, recovery_hash): user = get_object_or_404(User, username=username) if recovery_hash != hashlib.md5('%s-%s-%s' % (user.username, user.password, settings.SECRET_KEY)).hexdigest(): raise Http404 if request.method == 'POST': password_form = PasswordForm(request.POST) if password_form.is_valid(): password = password_form.get_password() user.set_password(password) user.save() auth_user = authenticate(username=user.username, password=password) auth_login(request, auth_user) send_info(request, _('Your password has been changed. Welcome back %s') % user) return HttpResponseRedirect('/') else: password_form = PasswordForm() return render_to_response('registration/change_password.html', {'password_form': password_form, 'user': user}, context_instance=RequestContext(request))
def confirm_register_view(request, username, registration_hash): registration = Registration.objects.actives().filter(username=username, registration_hash=registration_hash) if not registration.count(): raise Http404 registration = registration[0] profileform_class = get_profile_form() if request.method == 'POST': password_form = PasswordForm(request.POST) profileform = profileform_class(request.POST, registration=registration) if password_form.is_valid() and profileform.is_valid(): password = password_form.get_password() user = profileform.save(password) auth_user = authenticate(username=user.username, password=password) auth_login(request, auth_user) registration.delete() send_info(request, _('Welcome %s') % user) return HttpResponseRedirect('/') else: password_form = PasswordForm() return render_to_response('registration/confirm_register.html', {'password_form': password_form}, context_instance=RequestContext(request))
def access_request(request, exception=None, base_template='base.html'): data = None admin_index = reverse('admin_index') is_admin = request.get_full_path().startswith(admin_index) if request.method == 'GET' and not exception: return HttpResponseRedirect(settings.ACCESS_REQUEST_REDIRECT) elif request.method == 'POST': data = request.POST is_admin = is_admin or request.POST.get('url', '').startswith(admin_index) form = AccessRequestForm(request=request, data=data, exception=exception) if is_admin: base_template = 'admin/base.html' if form.is_valid(): form.save() send_info(request, _('Access request successfully')) return HttpResponseRedirect(settings.ACCESS_REQUEST_REDIRECT) return render_to_response('perms/access_request.html', {'form': form, 'base_template': base_template}, context_instance=RequestContext(request))
def upload(request, repository_name, path='', base_template=FILEBROWSER_BASE_TEMPLATE, url_prefix=None): repository = get_object_or_404(Repository, name=repository_name) if request.method == 'POST': for k, f in request.FILES.items(): if k.startswith('file_'): idx = k[5:] title = request.POST.get('title_' + idx).encode('utf8') description = request.POST.get('description_' + idx).encode('utf8') file_name = unicodedata.normalize('NFKD', force_unicode(f.name)).encode('ascii', 'ignore') fout = repository.create_file(join(path, file_name)) for chunk in f.chunks(): fout.write(chunk) fout.close() metadata_name = join(path, file_name + '.metadata') metadata_path = repository.encode_path(repository.get_absolute_path(metadata_name)) mout = open(metadata_path, 'w') mout.write(title) mout.write('\n\n') mout.write(description) mout.write('\n') mout.close() send_info(request, _('Files uploaded successfully')) return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={'repository_name': repository.name, 'path': path}, url_prefix=url_prefix)) else: dirs, files, parents, dirname = repository.list_directory(path) return render_to_response('filebrowser/upload.html', {'repository': repository, 'base_template': base_template, 'path': path, 'parents': parents, 'url_prefix': url_prefix}, context_instance=RequestContext(request))
def confirm_register_view(request, username, registration_hash): registration = Registration.objects.actives().filter( username=username, registration_hash=registration_hash) if not registration.count(): raise Http404 registration = registration[0] profileform_class = get_profile_form() if request.method == 'POST': password_form = PasswordForm(request.POST) profileform = profileform_class(request.POST, registration=registration) if password_form.is_valid() and profileform.is_valid(): password = password_form.get_password() user = profileform.save(password) auth_user = authenticate(username=user.username, password=password) auth_login(request, auth_user) registration.delete() send_info(request, _('Welcome %s') % user) return HttpResponseRedirect('/') else: password_form = PasswordForm() return render_to_response('registration/confirm_register.html', {'password_form': password_form}, context_instance=RequestContext(request))
def action(request, repository_name, path, base_template=FILEBROWSER_BASE_TEMPLATE, url_prefix=None): """ process rename, delete, etc. actions in files and directories """ repository = get_object_or_404(Repository, name=repository_name) if 'renameform' in request.POST: action = 'confirmrename' elif 'deleteform' in request.POST: action = 'confirmdelete' elif 'rename' in request.POST: action = 'rename' old_elems = [] else: action = 'delete' elems = [] for k, v in request.POST.items(): if (k.startswith('dir_') or k.startswith('file_') or k.startswith('elem_') or k.startswith('doc_')): if k.startswith('doc_'): try: title = Document.objects.get(slug=v).title except: title = v elems.append({'id': v, 'title': title}) elif k.startswith('file_'): post_id = k[len('file_'):] new_file = {} new_file['id'] = v new_file['name'] = v new_file['title'] = request.POST.get('title_%s' % post_id, '').encode('utf8') new_file['description'] = request.POST.get( 'description_%s' % post_id, '').encode('utf8') elems.append(new_file) else: elems.append({'id': v, 'title': v}) if action == 'rename': if k.startswith('dir_'): old_elems.append(request.POST['olddir_%s' % k[len('dir_'):]]) elif k.startswith('file_'): old_elems.append(request.POST['oldfile_%s' % k[len('file_'):]]) else: old_elems.append(request.POST['olddoc_%s' % k[len('doc_'):]]) if not elems: send_error(request, _("You didn't selected any element")) return HttpResponseRedirect( filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={ 'repository_name': repository.name, 'path': path }, url_prefix=url_prefix)) if action in ['confirmrename', 'confirmdelete']: if action == 'confirmrename': template = 'filebrowser/rename.html' else: template = 'filebrowser/delete.html' basepath = repository.get_absolute_path(path) basepath = basepath.encode('utf-8').decode('utf-8') files = [] dirs = [] docs = [] for e in elems: file = join(basepath, e['id']) if isdir(file): dirs.append(e) elif isfile(file): mfile = file + '.metadata' if exists(mfile): mf = open(mfile, 'r') mdata = mf.read() mf.close() mdata = mdata.split('\n\n') e['title'] = mdata[0] e['description'] = '\n\n'.join(mdata[1:]) files.append(e) else: docs.append(e) pathdirs, pathfiles, parents, dirname = repository.list_directory(path) return render_to_response(template, { 'repository': repository, 'base_template': base_template, 'path': path, 'files': files, 'parents': parents, 'dirs': dirs, 'docs': docs, 'url_prefix': url_prefix, }, context_instance=RequestContext(request)) elif action == 'rename': repository.rename_elems(path, elems, old_elems) send_info(request, _('Elements renamed successfully')) return HttpResponseRedirect( filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={ 'repository_name': repository.name, 'path': path }, url_prefix=url_prefix)) elif action == 'delete': repository.delete_elems(path, elems) send_info(request, _('Elements deleted successfully')) return HttpResponseRedirect( filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={ 'repository_name': repository.name, 'path': path }, url_prefix=url_prefix)) else: return Http404
def action(request, repository_name, path, base_template=FILEBROWSER_BASE_TEMPLATE, url_prefix=None): """ process rename, delete, etc. actions in files and directories """ repository = get_object_or_404(Repository, name=repository_name) if 'renameform' in request.POST: action = 'confirmrename' elif 'deleteform' in request.POST: action = 'confirmdelete' elif 'rename' in request.POST: action = 'rename' old_elems = [] else: action = 'delete' elems = [] for k, v in request.POST.items(): if (k.startswith('dir_') or k.startswith('file_') or k.startswith('elem_') or k.startswith('doc_')): if k.startswith('doc_'): try: title = Document.objects.get(slug=v).title except: title = v elems.append({'id': v, 'title': title}) elif k.startswith('file_'): post_id = k[len('file_'):] new_file = {} new_file['id'] = v new_file['name'] = v new_file['title'] = request.POST.get('title_%s' % post_id, '').encode('utf8') new_file['description'] = request.POST.get('description_%s' % post_id, '').encode('utf8') elems.append(new_file) else: elems.append({'id': v, 'title': v}) if action == 'rename': if k.startswith('dir_'): old_elems.append(request.POST['olddir_%s' % k[len('dir_'):]]) elif k.startswith('file_'): old_elems.append(request.POST['oldfile_%s' % k[len('file_'):]]) else: old_elems.append(request.POST['olddoc_%s' % k[len('doc_'):]]) if not elems: send_error(request, _("You didn't selected any element")) return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={'repository_name': repository.name, 'path': path}, url_prefix=url_prefix)) if action in ['confirmrename', 'confirmdelete']: if action == 'confirmrename': template = 'filebrowser/rename.html' else: template = 'filebrowser/delete.html' basepath = repository.get_absolute_path(path) basepath = basepath.encode('utf-8').decode('utf-8') files = [] dirs = [] docs = [] for e in elems: file = join(basepath, e['id']) if isdir(file): dirs.append(e) elif isfile(file): mfile = file + '.metadata' if exists(mfile): mf = open(mfile, 'r') mdata = mf.read() mf.close() mdata = mdata.split('\n\n') e['title'] = mdata[0] e['description'] = '\n\n'.join(mdata[1:]) files.append(e) else: docs.append(e) pathdirs, pathfiles, parents, dirname = repository.list_directory(path) return render_to_response(template, {'repository': repository, 'base_template': base_template, 'path': path, 'files': files, 'parents': parents, 'dirs': dirs, 'docs': docs, 'url_prefix': url_prefix, }, context_instance=RequestContext(request)) elif action == 'rename': repository.rename_elems(path, elems, old_elems) send_info(request, _('Elements renamed successfully')) return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={'repository_name': repository.name, 'path': path}, url_prefix=url_prefix)) elif action == 'delete': repository.delete_elems(path, elems) send_info(request, _('Elements deleted successfully')) return HttpResponseRedirect(filebrowser_reverse(request, "filebrowser_dir_listing", kwargs={'repository_name': repository.name, 'path': path}, url_prefix=url_prefix)) else: return Http404
def logout(request, template_name='registration/logged_out.html'): next_page = settings.LOGOUT_REDIRECT_URL or '/' response = auth_logout(request, next_page=next_page) send_info(request, _("Thanks for your visit")) return response
def register_new_plugins(self, request, extra_context=None): check_plugins(force_detect=True, force_broken_detect=True) send_info(request, _('Plugins detections done')) return HttpResponseRedirect('..')
def login(request, redirect_field_name='next'): response = auth_login(request, redirect_field_name=redirect_field_name) if request.user.is_authenticated(): send_info(request, _('Welcome %s') % request.user) return response
def register_new_plugins(self, request, extra_context=None): check_plugins(force_detect=True, force_broken_detect=True) send_info(request, _("Plugins detections done")) return HttpResponseRedirect("..")