def language_update(request, language_code = None, template_name = 'lanpro/language/form.html'): """ Create a new language definition or update an existing one """ # Check if this is a new language or not if language_code: # Check permissions if not request.user.has_perm('lanpro.change_language'): return page_forbidden(request, _('You are not allowed to change languages.')) # We know the language id, so this is an update language = get_object_or_404(Language, code = language_code) # Create the form LanguageFormClass = forms.form_for_instance(language, form = BaseForm) url = language.get_absolute_url() else: # Check permissions if not request.user.has_perm('lanpro.add_language'): return page_forbidden(request, _('You are not allowed to add languages.')) # This is a new one LanguageFormClass = forms.form_for_model(Language, form = BaseForm) url = Language.objects.get_absolute_url() # Customize the css class for item in ['code', 'name', 'description', 'plurals', 'equation']: LanguageFormClass.base_fields[item].widget.attrs['class'] = 'textbox' # Check the request if request.POST: # Posting data if 'submit' in request.POST: # Fill the form with posted data form = LanguageFormClass(request.POST) # Check the data validity if form.is_valid(): # Save the form and, implicit, the model form.save() # Redirect to language page return HttpResponseRedirect(url) elif 'cancel' in request.POST: # No data, redirect to language page return HttpResponseRedirect(url) else: # No data, just display the form form = LanguageFormClass() # The absolute url to add a new language url_add = Language.objects.get_absolute_add_url() # Return the response return render_to_response( template_name, { 'form': form, 'url_add': url_add, }, context_instance = RequestContext(request) )
def group_update(request, group_id = None, template_name = 'accounts/group/form.html'): """ Edit the group's details """ if group_id: # Check permissions if not request.user.has_perm('accounts.change_group'): return page_forbidden(request, _('You are not allowed to change group details.')) # We know the group id, so this is an update try: group = Group.objects.select_related().get(id = group_id) except Group.DoesNotExist: return page_not_found(request, _('There is no such group.')) else: # Check permissions if not request.user.has_perm('accounts.add_group'): return page_forbidden(request, _('You are not allowed to add a group.')) # This is a new one group = None # The absolute URL of group list # TODO DNRY url = '/accounts/groups/' # Check the request if request.method == "POST": # Posting data if 'submit' in request.POST and 'group_form' in request.POST: # Fill the form with posted data form = GroupForm(request.POST, group = group) # Check the data validity if form.is_valid(): # Save the form and, implicit, the model form.save() # Redirect to users page return HttpResponseRedirect(url) elif 'cancel' in request.POST: # No data, redirect to users page return HttpResponseRedirect(url) else: # No submitted data, just display the form form = GroupForm(group = group) # Return the response return render_to_response( template_name, { 'form': form, 'object': group, }, context_instance = RequestContext(request) )
def process_catalog_upload_form(request, catalogfile = None, form_class = CatalogFileUploadForm): """ Create a new catalog file or update an existing one """ # Check permissions if not request.user.has_perm('impex.upload_catalog'): return page_forbidden(request, _('You are not allowed to upload catalogs.')) # Check the request if request.method == "POST": # Posting data if 'upload' in request.POST and 'catalog_upload_form' in request.POST: # Check whether there is any file posted if 'catfile' in request.FILES: # There is a file posted new_data = request.POST.copy() new_data.update(request.FILES) form = form_class(new_data, catalogfile = catalogfile) del new_data else: # No file posted, just edit the other parameters form = form_class(request.POST, catalogfile = catalogfile) # Check the data validity if form.is_valid(): # Save the form and the model form.save(user = request.user) # Remove this form form = None elif 'cancel' in request.POST: # No data, remove this form form = None else: # No form form = None # Return the form in context return {'catalog_upload_form': form,}
def contact(request, template_name = 'accounts/contact/form.html'): """ Contact form """ # First, get the user and the user profile user = request.user if not user.is_authenticated: return page_forbidden(request, _('You are not identifying yourself.')) # Check the request if request.method == 'POST': if 'contact_form' in request.POST: if 'submit' in request.POST: form = ContactForm(user, request.POST) if form.is_valid(): # Form is valid, save it (send it) if form.save(): request.user.alerts.create(type = 'success', message = _('Your message has been sent successfully.')) return HttpResponseRedirect('/') else: request.user.alerts.create(type = 'error', message = _('Your message could not be sent.')) elif 'cancel' in request.POST: # No data, redirect to root return HttpResponseRedirect('/') else: # Initialize the form form = ContactForm(user) # Return the response return render_to_response(template_name, { 'form': form, }, context_instance = RequestContext(request))
def user_list(request, filter = None, template_name = 'accounts/user/list.html'): """ Prepare a list of all users """ # Check permissions if not request.user.has_perm('accounts.view_user'): return page_forbidden(request, _('You are not allowed to view the users list.')) # Last 5 minutes oldest = datetime.now() - timedelta(minutes = 5) # Create the users list list = User.objects.select_related() # Filter if filter == "active": list = list.filter(is_active = True) elif filter == "inactive": list = list.filter(is_active = False) elif filter == "online": list = list.filter(is_active = True).filter(userprofiles__last_seen__gt = oldest) elif filter == "offline": list = list.filter(is_active = True).filter(userprofiles__last_seen__lt = oldest) return render_to_response(template_name, { 'user_list': list, 'users_filters': users_filters, 'filter': filter, 'list_url': '/accounts/users/', }, context_instance = RequestContext(request) )
def create_tickets(request, template_name = 'accounts/tickets/tickets.html'): """ Create and submit registration tickets """ # Check permissions if not request.user.has_perm('accounts.add_registrationticket'): return page_forbidden(request, _('You are not allowed to create Registration tickets.')) # Check the request if request.method == "POST": # Posting data if 'submit' in request.POST and 'registrationtickets_form' in request.POST: # Fill the form with posted data reg_tickets_form = CreateRegistrationTicketsForm(request.POST) # Check the data validity if reg_tickets_form.is_valid(): # Save the form reg_tickets_form.save() # Store a message request.user.alerts.create(type = 'success', message = gettext('The tickets have been sent.')) # Create an empty registration tickets form reg_tickets_form = CreateRegistrationTicketsForm() return render_to_response(template_name, { 'reg_tickets_form': reg_tickets_form, }, context_instance = RequestContext(request) )
def internal_transfer(request, proofread_code = None, project_code = None, catalog_code = None, language_code = None): """ Transfer internally the specified reviewed catalog to the other languages """ # Check permissions if not request.user.has_perm('impex.internal_transfer'): return page_forbidden(request, _('You are not allowed to transfer the reviewed catalogs.')) # Check the request if request.POST: # Language try: language_code = request.POST['internal-transfer-language'] except: request.user.alerts.create(type = 'error', message = gettext('The target language could not have been determined.')) language_code = None if language_code == '_all_': language_code = None # Proofread try: proofread_code = request.POST['internal-transfer-proofread'] except: request.user.alerts.create(type = 'error', message = gettext('The proofread language could not have been determined.')) proofread_code = None # Project try: project_code = request.POST['internal-transfer-project'] except: request.user.alerts.create(type = 'error', message = gettext('The project could not have been determined.')) project_code = None # Catalog try: catalog_code = request.POST['internal-transfer-catalog'] except: request.user.alerts.create(type = 'error', message = gettext('The catalog could not have been determined.')) catalog_code = None # The address to land to url = request.META.get('HTTP_REFERER', '/translate/%s/%s/%s/' % (proofread_code, project_code, catalog_code)) # Create the internal transfer catalog file entry catfile = CatalogFile.objects.create_internal_transfer(language = language_code, project = project_code, catalog = catalog_code, user = request.user) if catfile is not None: request.user.alerts.create(type = 'success', message = gettext('The internal transfer catalog file has been created and queued for import.')) else: request.user.alerts.create(type = 'error', message = gettext('The internal transfer catalog file could not be created.')) # Redirect to catalog page return HttpResponseRedirect(url)
def build(self, format, **kwargs): """ Create the report in the specified format """ # Check permissions if not self.request.user.has_perm("statistics.download_statistics"): return page_forbidden(self.request, _("You are not allowed to download the statistics.")) # Select the format if format == "ods": return self.to_ods(**kwargs) elif format == "pdf": return self.to_pdf(**kwargs) else: return self.to_csv(**kwargs)
def register(request, ticket = None, form_class = RegistrationFormUniqueEmail, profile_callback = None, template_name = 'accounts/register/register.html'): """ Allow a new user to register an account By default, 'registration.forms.RegistrationForm' will be used as the registration form; to change this, pass a different form class as the 'form_class' keyword argument. The form class you specify must have a method 'save' which will create and return the new 'User', and that method must accept the keyword argument 'profile_callback' (see below). To enable creation of a site-specific user profile object for the new user, pass a function which will create the profile object as the keyword argument 'profile_callback'. See 'RegistrationManager.create_inactive_user' in the file 'models.py' for details on how to write this function. """ # Sleep for a moment if settings.TICKET_CHECK_SLEEP is not None: sleep(settings.TICKET_CHECK_SLEEP) # Check the Registration Ticket if not RegistrationTicket.objects.check_ticket(ticket): return page_forbidden(request, message = gettext('You need a valid Registration Ticket to access the Registration page')) # Check the request if request.method == 'POST': # The user has submitted registration data form = form_class(request.POST) # Check the data validity if form.is_valid(): # Create the user new_user = form.save(profile_callback = profile_callback) # Do not display the form again form = None else: # The data is not valid, new captcha #request.session['captcha'] = captcha.gen_captcha_key() #form.clean_data['captchadigest'] = captcha.captcha_digest(request.session['captcha']) #form.clean_data['captcha'] = '' pass else: # Display the registration form request.session['captcha'] = captcha.gen_captcha_key() form_class.base_fields['captchadigest'].initial = captcha.captcha_digest(request.session['captcha']) form = form_class(ticket = ticket) return render_to_response(template_name, { 'form': form, 'site': Site.objects.get_current(), }, context_instance = RequestContext(request) )
def user_update(request, username = None, template_name = 'accounts/user/form.html'): """ Edit the user's details and profile """ # Check permissions if not request.user.has_perm('accounts.change_user'): return page_forbidden(request, _('You are not allowed to change users details.')) # Try to get the user try: user = User.objects.select_related().get(username = username) except User.DoesNotExist: return page_not_found(request, _('There is no such user.')) # The absolute URL of users list # TODO DNRY url = '/accounts/users/' # Check the request if request.POST: # Posting data if 'submit' in request.POST: # Fill the form with posted data form = UserForm(user, request.POST) # Check the data validity if form.is_valid(): # Save the form and, implicit, the model form.save() # Redirect to users page return HttpResponseRedirect(url) elif 'cancel' in request.POST: # No data, redirect to users page return HttpResponseRedirect(url) else: # No submitted data, just display the form form = UserForm(user) # Return the response return render_to_response( template_name, { 'form': form, 'object': user, }, context_instance = RequestContext(request) )
def group_list(request, template_name = 'accounts/group/list.html'): """ Prepare a list of all groups """ # Check permissions if not request.user.has_perm('accounts.view_group'): return page_forbidden(request, _('You are not allowed to view the groups list.')) # Create the users list groups = [] for g in Group.objects.select_related(): g.active_users = g.user_set.filter(is_active = True) groups.append(g) return render_to_response(template_name, { 'groups': groups, }, context_instance = RequestContext(request) )
def xmlrpc_remove(request, username = None): """ Remove the XML-RPC shared secret """ # Set the next url next_url = request.META.get('HTTP_REFERER', '/') # Prepare the response response = HttpResponseRedirect(next_url) # Check permissions if not request.user.has_perm('accounts.remove_xmlrpc_key'): return page_forbidden(request, _('You are not allowed to remove a XML-RPC key.')) # Try to get the user try: user = User.objects.select_related().get(username = username) except User.DoesNotExist: return page_not_found(request, _('There is no such user.')) # Get the user profile user_profile = user.get_profile() # Remove the key user_profile.remove_xmlrpc_key() # Send a message to the creator request.user.alerts.create(type = 'delete', message = _('The XML-RPC key of %(user)s has been removed.') % {'user': user.get_full_name()}) # Return the response return response
def catalogfile_list(request, page = 1, template_name = 'impex/catalogfile_list.html'): """ Prepare a list of all catalog files """ # Check permissions if not request.user.has_perm('impex.upload_catalog'): return page_forbidden(request, _('You are not allowed to upload catalogs.')) # Create the catalog file list catalog_files = CatalogFile.objects.select_related() # Define the paginator try: user = request.user except AttributeError: page_size = 10 else: page_size = int(user.get_profile().page_size) paginator = Paginator(request, catalog_files, current_page = page, page_size = page_size) # The absolute url to add a new catalog file url = CatalogFile.objects.get_absolute_add_url() # If the page number is over total pages or null, go to first page if page is None: page = '1' if int(page) > paginator.pages or int(page) <= 0: return HttpResponseRedirect(paginator.path) else: return render_to_response(template_name, { 'paginator': paginator, 'object_list': paginator.get_page(), 'url_add': url, }, context_instance = RequestContext(request) )
def exporter(request, language_code = None, project_code = None, catalog_code = None, filename = None, filter = None, untranslated = None, proofread = None, format = 'xliff', testpacktrans = False, **kwargs): """ Wrapper for the exporter """ # Check the request if request.POST: try: format = request.POST['export-catalog-format'] except: request.user.alerts.create(type = 'error', message = 'Unknown format.') format = None try: language_code = request.POST['export-catalog-language'] except: request.user.alerts.create(type = 'error', message = 'Unknown language.') language_code = None try: project_code = request.POST['export-catalog-project'] except: request.user.alerts.create(type = 'error', message = 'Unknown project.') project_code = None try: catalog_code = request.POST['export-catalog-catalog'] except: request.user.alerts.create(type = 'error', message = 'Unknown catalog.') catalog_code = None try: modifier = request.POST['export-catalog-modifier'] except: untranslated = None proofread = None else: if modifier == 'proofread': proofread = modifier elif modifier == 'untranslated': untranslated = modifier try: filename = request.POST['export-catalog-filename'] except: filename = None try: filter = request.POST['export-catalog-filter'] except: filter = None testpacktrans = request.POST.get('export-catalog-testpacktrans', 'yes') == 'yes' try: # The exporter object exporter = Exporter( language_code = language_code, project_code = project_code, catalog_code = catalog_code, user = request.user, filter = filter, untranslated = untranslated, proofread = proofread, testpacktrans = testpacktrans) except ExporterSourceIterationError: # Cannot find the iteration of the source catalog response = page_not_found(request, _('Cannot find the iteration of the reviewed catalog.')) else: try: # Get the content content = exporter.export(format, **kwargs) except ExporterUnsupportedFormat: # The format is not supported response = page_not_found(request, _('Unsupported format: %s' % format)) except ExporterForbidden: # The user is not allowed to export response = page_forbidden(request, _('You are not allowed to export to the %s format.' % format)) except ExporterSourceIterationError: # Cannot find the iteration of the source catalog response = page_not_found(request, _('The catalog needs to be transfered again. Please contact the system administrator.')) else: # Create the HttpResponse object with the appropriate header if filename is None: filename = exporter.create_file_name(CFT.get_extension(format)) response = HttpResponse(content.getvalue(), mimetype = CFT.get_mime(format)) response['Content-Disposition'] = 'attachment; filename="%s"' % filename # Return the response return response