def import_view(request): context = { 'search_form': SearchForm(), 'next': request.get_full_path(), } if request.method == 'POST': import_form = ImportForm(request.POST, request.FILES) if import_form.is_valid(): glossary = import_form.save() try: with transaction.atomic(): import_uploaded_file(request.FILES['imported_file'], glossary) except Exception as e: glossary.delete() import_error_message = _("The import process failed:\n\n") import_error_message += force_text(e.args[0]) context['import_error_message'] = import_error_message else: import_message = _("TBX file succesfully imported.") context['import_message'] = import_message context['glossary'] = glossary import_form = ImportForm() LogEntry.objects.log_action( user_id=request.user.pk, content_type_id=ContentType.objects.get_for_model( glossary).pk, object_id=glossary.pk, object_repr=force_text(glossary), action_flag=ADDITION, ) else: import_form = ImportForm() context['import_form'] = import_form return render(request, 'import.html', context)
def export(request): #exporting_message = ""#TODO show export confirmation message if request.method == 'GET' and 'from_glossaries' in request.GET: export_form = ExportForm(request.GET) elif request.method == 'POST' and 'from_glossaries' in request.POST: export_form = ExportForm(request.POST) if export_form.is_valid(): glossaries = export_form.cleaned_data['from_glossaries'] desired_languages = export_form.cleaned_data['for_languages'] export_all_definitions = export_form.cleaned_data[ 'export_not_finalized_definitions'] export_terms = export_form.cleaned_data['export_terms'] #exporting_message = "Exported succesfully."#TODO show export confirmation message return export_glossaries_to_TBX(glossaries, desired_languages, export_all_definitions, export_terms) else: export_form = ExportForm() context = { 'search_form': SearchForm(), 'export_form': export_form, #'exporting_message': exporting_message,#TODO show export confirmation message 'next': request.get_full_path(), } return render(request, 'export.html', context)
def terminator_profile_detail(request, username): user = get_object_or_404(User, username=username) cil_ctype = ContentType.objects.get_for_model(ConceptInLanguage) user_comments = Comment.objects.filter( user=user, content_type=cil_ctype, is_public=True, is_removed=False, ).order_by('-submit_date') paginator = Paginator(user_comments, 10) # Make sure page request is an int. If not, deliver first page. try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 # If page request (9999) is out of range, deliver last page of results. try: comments = paginator.page(page) except (EmptyPage, InvalidPage): comments = paginator.page(paginator.num_pages) prefetch_related_objects( comments.object_list, 'content_object__concept', 'content_object__concept__glossary', ) glossary_list = list(Glossary.objects.all()) user_glossaries = [] checker = ObjectPermissionChecker(user) checker.prefetch_perms(glossary_list) for glossary in glossary_list: if checker.has_perm('owner', glossary): user_glossaries.append({'glossary': glossary, 'role': _(u"owner")}) elif checker.has_perm('terminologist', glossary): user_glossaries.append({ 'glossary': glossary, 'role': _(u"terminologist") }) elif checker.has_perm('specialist', glossary): user_glossaries.append({ 'glossary': glossary, 'role': _(u"specialist") }) ctypes = ContentType.objects.get_for_models(Translation, Definition, ExternalResource).values() recent_changes = process_recent_changes( LogEntry.objects.filter( content_type__in=ctypes, user=user, ).order_by("-action_time")[:10]) context = { 'thisuser': user, 'glossaries': user_glossaries, 'comments': comments, 'search_form': SearchForm(), 'next': request.get_full_path(), 'recent_changes': recent_changes, } return render(request, "profiles/profile_detail.html", context)
def get_context_data(self, **kwargs): # Call the base implementation first to get a context context = super(TerminatorListView, self).get_context_data(**kwargs) # Add the breadcrumbs search form to context context['search_form'] = SearchForm() # Add the request path to correctly render the "log in" or "log out" # link in template. context['next'] = self.request.get_full_path() return context
def search(request): search_results = None if request.method == 'GET' and 'search_string' in request.GET: if "advanced" in request.path: search_form = AdvancedSearchForm(request.GET) else: search_form = SearchForm(request.GET) if search_form.is_valid(): qs = Translation.objects.all() data = search_form.cleaned_data search_string = data['search_string'] if "advanced" in request.path: glossary_filter = data['filter_by_glossary'] language_filter = data['filter_by_language'] part_of_speech_filter = data['filter_by_part_of_speech'] admin_status_filter = data['filter_by_administrative_status'] if glossary_filter: qs = qs.filter(concept__glossary=glossary_filter) if language_filter: qs = qs.filter(language=language_filter) #TODO add filter by is_finalized if part_of_speech_filter: qs = qs.filter(part_of_speech=part_of_speech_filter) if admin_status_filter: qs = qs.filter(administrative_status=admin_status_filter) if data['also_show_partial_matches']: qs = qs.filter(translation_text__icontains=search_string) else: qs = qs.filter(translation_text__iexact=search_string) else: qs = qs.filter(translation_text__iexact=search_string) # Limit for better worst-case performance. Consider pager. limit = 20 if request.user.is_authenticated: limit = 100 definition = Definition.objects.filter( concept=OuterRef('concept'), language=OuterRef('language'), ) qs = qs.annotate(definition=Subquery(definition.values('text'))) qs = qs.select_related( 'concept', 'concept__glossary', 'administrative_status', )[:limit] deferred_fields = ( 'administrative_status_reason', 'administrative_status__description', 'administrative_status__allows_reason', 'part_of_speech', 'grammatical_gender', 'grammatical_number', 'note', 'concept__repr_cache', 'concept__broader_concept', 'concept__subject_field', 'concept__glossary__description', 'concept__glossary__source_language', ) inner_qs = Translation.objects.defer() qs = qs.prefetch_related( Prefetch('concept__translation_set', queryset=inner_qs, to_attr="others")) qs = qs.defer(*deferred_fields) previous_concept = None search_results = [] for trans in qs: # Translations are ordered by (concept, language) # If this is the first translation for this concept if previous_concept != trans.concept_id: is_first = True previous_concept = trans.concept_id others = trans.concept.others others = islice((c for c in others if c.pk != trans.pk), 7) else: others = None is_first = False search_results.append({ "translation": trans, "definition": trans.definition, "other_translations": others, "is_first": is_first, }) elif "advanced" in request.path: search_form = AdvancedSearchForm() else: search_form = SearchForm() context = { 'search_form': search_form, 'search_results': search_results, 'next': request.get_full_path(), } template_name = 'search.html' if "advanced" in request.path: template_name = 'advanced_search.html' return render(request, template_name, context)
def terminator_index(request): new_proposal_message = "" if request.method == 'POST': proposal_form = ProposalForm(request.POST) if proposal_form.is_valid(): new_proposal = proposal_form.save(commit=False) new_proposal.user = request.user new_proposal.save() #TODO send a mail or notify in any way to the glossary owners in # order to get them manage the proposal. Maybe do this in the # save() method in the model. # Log the addition using LogEntry from admin contrib app LogEntry.objects.log_action( user_id=request.user.pk, content_type_id=ContentType.objects.get_for_model( new_proposal).pk, object_id=new_proposal.pk, object_repr=force_text(new_proposal), action_flag=ADDITION) new_proposal_message = _("Thank you for sending a new proposal. " "You may send more!") proposal_form = ProposalForm() else: proposal_form = ProposalForm() glossary_ctype = ContentType.objects.get_for_model(Glossary) concept_ctype = ContentType.objects.get_for_model(Concept) cil_ctype = ContentType.objects.get_for_model(ConceptInLanguage) language_ctypes = ContentType.objects.get_for_models( Translation, Definition, ExternalResource, ConceptInLanguage, ).values() recent_changes = LogEntry.objects.filter( content_type__in=language_ctypes, ).order_by("-action_time")[:8] def latest_changes(): # A callable to ensure that it is called as late as possible return process_recent_changes(recent_changes) context = { 'search_form': SearchForm(), 'proposal_form': proposal_form, 'new_proposal_message': new_proposal_message, 'next': request.get_full_path(), 'glossaries': Glossary.objects.all(), 'latest_proposals': Proposal.objects.order_by("-id").select_related( "language", "for_glossary")[:8], 'latest_comments': Comment.objects.order_by("-id"). # this filter should be unnecessary, but will become required # if we ever have comments on other content types. filter( content_type=cil_ctype, is_public=True, is_removed=False, ).select_related("user").prefetch_related( "content_object", "content_object__concept")[:8], 'latest_glossary_changes': LogEntry.objects.filter( content_type=glossary_ctype).order_by("-action_time")[:8], 'latest_concept_changes': LogEntry.objects.filter( content_type=concept_ctype).order_by("-action_time")[:8], 'latest_changes': latest_changes, } return render(request, 'index.html', context)