def get_context_data(self, **kwargs): context = super(ProjectView, self).get_context_data(**kwargs) context["protected"] = self.object.name == models.ACTION_PROJECT_NAME context["sort_form"] = forms.ActionlistSortForm( initial={ "return_model": self.object.pk, "sort_method": self.request.session["sort_method"], "sort_order": self.request.session["sort_order"], } ) # Sort the action list # Case sensitive fields need to be converted to insensitive if self.request.session["sort_method"] == "text": sort = Lower(self.request.session["sort_method"]) if self.request.session["sort_order"] == "-": sort = sort.desc() context["actions"] = self.object.action_list.order_by(sort) # Other fields can just be sorted regularly elif self.request.session["sort_method"] != "": context["actions"] = self.object.action_list.order_by( self.request.session["sort_order"] + self.request.session["sort_method"] ) # When no ordering is applied just get all actions else: context["actions"] = self.object.action_list.all() return context
def list_users(request): users = User.objects.all().order_by(Lower('name'), Lower('surname')) return render(request, 'users/list.html', {'users': users})
from aristotle_mdr import models as MDR from aristotle_mdr.utils import status_filter from aristotle_mdr.perms import user_can_view from aristotle_mdr.models import _concept from aristotle_mdr.contrib.favourites.models import Favourite, Tag import datetime import json paginate_sort_opts = { "mod_asc": ["modified"], "mod_desc": ["-modified"], "cre_asc": ["created"], "cre_desc": ["-created"], "name_asc": [Lower("name").asc()], "name_desc": [Lower("name").desc()], } @login_required def paginated_list(request, items, template, extra_context={}): if hasattr(items, 'select_subclasses'): items = items.select_subclasses() sort_by=request.GET.get('sort', "mod_desc") if sort_by not in paginate_sort_opts.keys(): sort_by="mod_desc" paginator = Paginator( items.order_by(*paginate_sort_opts.get(sort_by)), request.GET.get('pp', 20) # per page
def page_load(request): stickies = [] groups = [] colours = [] sp = SortingPreference.objects.get(user=request.user) iGroupHeaderColourModifier = 0.75 if request.user.is_authenticated: print('!!!!!!!!!!!!!!!!NEW PAGE LOAD!!!!!!!!!!!!!!!!!!!') print(sp.sorting_pref) if sp.sorting_pref == "TITLE": stickies = Stickynote.objects.filter( group_id__author_id=request.user.id).order_by(Lower('title')) print(stickies) groups = Group.objects.filter(author_id=request.user.id).order_by( '-cannotBeDeleted', Lower('title')) elif sp.sorting_pref == "DATE_CREATED": stickies = Stickynote.objects.filter( group_id__author_id=request.user.id).order_by('created_date') print(stickies) groups = Group.objects.filter(author_id=request.user.id).order_by( '-cannotBeDeleted', '-created_date') elif sp.sorting_pref == "DATE_EDITED": stickies = Stickynote.objects.filter( group_id__author_id=request.user.id).order_by( '-last_edit_date') print(stickies) groups = Group.objects.filter(author_id=request.user.id).order_by( '-cannotBeDeleted', '-last_edit_date') #TODO: remove the if and change the first elif to if. This will have the same functionality. else: stickies = Stickynote.objects.filter( group_id__author_id=request.user.id).order_by(Lower('title')) print(stickies) groups = Group.objects.filter(author_id=request.user.id).order_by( '-cannotBeDeleted', Lower('title')) #Get&Set the colour that most stickies in a group have. for group in groups: groupStickies = Stickynote.objects.filter(group_id=group.id) majorityColour = groupStickies.values("colour_id").annotate( Count("id")).order_by('-id__count') majorityColour = majorityColour[0] if majorityColour.count( ) > 0 else { 'colour_id': GetRandomColour().id, 'id__count': 0 } #majorityColour = Colour.objects.get(id=majorityColour.colour_id); majorityColour['r'] = round( Colour.objects.get(id=majorityColour['colour_id']).r * iGroupHeaderColourModifier) majorityColour['g'] = round( Colour.objects.get(id=majorityColour['colour_id']).b * iGroupHeaderColourModifier) majorityColour['b'] = round( Colour.objects.get(id=majorityColour['colour_id']).g * iGroupHeaderColourModifier) majorityColour['a'] = Colour.objects.get( id=majorityColour['colour_id']).a majorityColour['colour'] = Colour.objects.get( id=majorityColour['colour_id']) group.majorityColour = majorityColour print(groups) colours = Colour.objects.all().order_by('name') print(colours) return render( request, 'stickynote/index.html', { 'stickies': stickies, 'groups': groups, 'colours': colours, 'author_id': request.user.id })
def all_products(request): """ A master page for all products, will have sorting and searching for products. """ products = Product.objects.all() search = None categories = None sub_categories = None sort = None direction = None articles = None if request.GET: if 'sort' in request.GET: sortkey = request.GET['sort'] sort = sortkey if sortkey == 'name': sortkey = 'lower_name' products = products.annotate(lower_name=Lower('name')) if sortkey == 'category': sortkey == 'category__cat_name' if sortkey == 'sub_category': sortkey == 'sub_category__subcat_name' if 'direction' in request.GET: direction = request.GET['direction'] if direction == 'desc': sortkey = f'-{sortkey}' products = products.order_by(sortkey) if 'article' in request.GET: articles = request.GET['article'].split(',') products = products.filter(article_type__in=articles) if 'category' in request.GET: categories = request.GET['category'].split(',') products = products.filter(category__cat_name__in=categories) categories = Category.objects.filter(cat_name__in=categories) if 'sub_category' in request.GET: sub_categories = request.GET['sub_category'].split(',') products = products.filter(subcat__subcat_name__in=sub_categories) sub_categories = Sub_Category.objects.filter( subcat_name__in=sub_categories) if 'search' in request.GET: search = request.GET['search'] if not search: messages.error(request, "There is nothing to search on!") return redirect(reverse('products')) searches = Q(name__icontains=search) products = products.filter(searches) current_sorting = f'{sort}_{direction}' paginator = Paginator(products, 48) page_number = request.GET.get('page') page_obj = paginator.get_page(page_number) context = { 'products': products, 'search_term': search, 'current_categories': categories, 'current_sub_categories': sub_categories, 'current_articles': articles, 'current_sorting': current_sorting, 'paginator': paginator, 'page_obj': page_obj, } return render(request, 'products/products.html', context)
def get_ordering(self, request: HttpRequest) -> List[str]: return [Lower("user__username"), "-id"]
class Meta: ordering = [Lower('bookname')]
def show_dictionary(request, project, lang): prj = get_project(request, project) lang = get_object_or_404(Language, code=lang) if request.method == 'POST' and request.user.has_perm('glossary.add', prj): form = WordForm(request.POST) if form.is_valid(): Dictionary.objects.create(request.user, project=prj, language=lang, source=form.cleaned_data['source'], target=form.cleaned_data['target']) return redirect_next(request.POST.get('next'), request.get_full_path()) else: form = WordForm() uploadform = DictUploadForm() words = Dictionary.objects.filter(project=prj, language=lang).order_by(Lower('source')) letterform = LetterForm(request.GET) searchform = OneWordForm(request.GET) if searchform.is_valid() and searchform.cleaned_data['term'] != '': words = words.filter(source__icontains=searchform.cleaned_data['term']) search = searchform.cleaned_data['term'] else: search = '' if letterform.is_valid() and letterform.cleaned_data['letter'] != '': words = words.filter( source__istartswith=letterform.cleaned_data['letter']) letter = letterform.cleaned_data['letter'] else: letter = '' words = get_paginator(request, words) last_changes = Change.objects.last_changes(request.user).filter( dictionary__project=prj, dictionary__language=lang)[:10] return render( request, 'dictionary.html', { 'title': dict_title(prj, lang), 'project': prj, 'language': lang, 'page_obj': words, 'form': form, 'query_string': urlencode({ 'term': search, 'letter': letter }), 'uploadform': uploadform, 'letterform': letterform, 'searchform': searchform, 'letter': letter, 'last_changes': last_changes, 'last_changes_url': urlencode({ 'project': prj.slug, 'lang': lang.code, 'glossary': 1 }), })
def search_results(self, request, query_term): """ Display results based on search term. """ is_gene_suggest = False if request.method == "GET": client = Elasticsearch([ES_HOST], timeout=60) search_gene = Search().using(client).doc_type('genes').source( exclude=['isoforms.cds', 'isoforms.exons', 'GO']) #'isoforms.cds','GO']) if query_term == None: studies = Study.objects.all() phenotypes = Phenotype.objects.all() # Elasticsearch query cannot be made before knowing the ordering and the page number, etc as this is taken into account by elasticsearch.py else: studies = Study.objects.filter( Q(name__icontains=query_term) | Q(phenotype__trait_ontology_name__icontains=query_term) | Q(phenotype__name__icontains=query_term) | Q(phenotype__description__icontains=query_term) | Q(publication_pmid__icontains=query_term) | Q(publication_pmcid__icontains=query_term)).order_by( 'n_hits_perm').reverse() phenotypes = Phenotype.objects.filter( Q(name__icontains=query_term) | Q(description__icontains=query_term)).order_by('name') # Add chromosome position search for genomic regions try: int(query_term) isnum = True except ValueError: isnum = False import re pattern = re.compile( "(Chr|CHR|chr)+\s?([0-9]{1,2})+(-|:)?(\d*)\s*(-|:|)?\s*(\d+|)" ) if isnum: # Only a number, look for neighboring genes on all chromosomes. q = QES('range', positions={ "gte": int(query_term), 'lte': int(query_term) }) search_gene = search_gene.query(q) elif pattern.match(query_term): # Specific genomic range splitted = re.split( "(Chr|CHR|chr)+\s?([0-9]{1,2})+(-|:)?(\d*)\s*(-|:|)?\s*(\d+|)", query_term) chr = int(splitted[2]) s_p = None e_p = None if splitted[4]: s_p = int(splitted[4]) if splitted[6]: e_p = int(splitted[6]) # Need to retrieve all genes that overlap somehow with that region (all-in, right part in, left part in, etc) q = QES('match', chr='chr' + str(chr)) search_gene = search_gene.query(q) if s_p: if e_p: # Look for genes overlapping with region of interest q = QES('range', positions={ 'gte': s_p, 'lte': e_p }) | QES('range', positions={ 'gte': s_p, 'lte': s_p }) | QES('range', positions={ 'gte': e_p, 'lte': e_p }) else: q = QES('range', positions={ 'gte': s_p, 'lte': s_p }) | QES('range', positions={'gte': s_p}) search_gene = search_gene.query(q) else: # other type of request is_gene_suggest = True search_gene = search_gene.suggest('gene_suggest', query_term, completion={ 'field': 'suggest', 'size': 200 }) # custom ordering ordering = request.query_params.get('ordering', None) ordering_fields = { 'studies': ['name', 'genotype', 'phenotype', 'method', 'transformation'], 'phenotypes': ['name', 'description'], 'genes': [ 'name', 'chr', 'start', 'end', 'SNPs_count', 'association_count', 'description' ] } if ordering is not None: from django.db.models.functions import Lower inverted = False if ordering.startswith('-'): inverted = True ordering = ordering[1:] if ordering in ordering_fields['studies'] and studies: if ordering == 'phenotype' or ordering == 'genotype': # Need to reference the names and not the internal IDs for ordering ordering += '__name' studies = studies.order_by(Lower(ordering)).reverse() if inverted: studies = studies.reverse() if ordering in ordering_fields['phenotypes'] and phenotypes: phenotypes = phenotypes.order_by(Lower(ordering)) if inverted: phenotypes = phenotypes.reverse() if ordering in ordering_fields['genes']: # if ordering == 'snp' or ordering == 'study': # ordering += '__name' # genes = genes.order_by(Lower(ordering)) if ordering == 'start' or ordering == 'end': ordering += '_position' if inverted: ordering = "-" + ordering search_gene.sort(ordering) n_genes = search_gene.count() if studies: pagest = self.paginate_queryset(studies) study_serializer = StudySerializer(pagest, many=True) else: study_serializer = StudySerializer(studies, many=True) if n_genes: size = min(200, search_gene.count()) if is_gene_suggest: size = 0 results = search_gene[0:size].execute() if is_gene_suggest: genes = results.to_dict( )['suggest']['gene_suggest'][0]['options'] else: genes = results.to_dict()['hits']['hits'] genes_out = [] for gene in genes: genes_out.append(gene["_source"]) pagege = self.paginate_queryset(genes_out) else: genes = [] pagege = [] if phenotypes: pagephe = self.paginate_queryset(phenotypes) phenotype_serializer = PhenotypeListSerializer(pagephe, many=True) else: phenotype_serializer = PhenotypeListSerializer(phenotypes, many=True) counts = [len(genes), len(phenotypes), len(studies)] PAGE_SIZE = 25. import math page_counts = [ int(math.ceil(float(len(genes)) / PAGE_SIZE)), int(math.ceil(float(len(phenotypes)) / PAGE_SIZE)), int(math.ceil(float(len(studies)) / PAGE_SIZE)) ] data = { 'study_search_results': study_serializer.data, 'phenotype_search_results': phenotype_serializer.data, 'gene_search_results': pagege, 'counts': counts, 'page_counts': page_counts } if any([studies, genes, phenotypes]): return self.get_paginated_response(data) else: return Response({ 'results': {i: data[i] for i in data if i != 'counts'}, 'count': counts, 'page_count': [0, 0, 0] })
def lowercase_vendor_code(apps, schema_editor): Vendor = apps.get_model('pj', 'Vendor') Vendor.objects.update(code=Lower('code'))
def get(self, request, format=None): coop_types = CoopType.objects.all().order_by(Lower('name')) serializer = CoopTypeSerializer(coop_types, many=True) return Response(serializer.data)
class Meta: ordering = [Lower('text')]
def show_dictionary(request, project, lang): prj = get_project(request, project) lang = get_object_or_404(Language, code=lang) if request.method == 'POST' and can_add_dictionary(request.user, prj): form = WordForm(request.POST) if form.is_valid(): Dictionary.objects.create(request.user, project=prj, language=lang, source=form.cleaned_data['source'], target=form.cleaned_data['target']) return redirect_next(request.POST.get('next'), request.get_full_path()) else: form = WordForm() uploadform = DictUploadForm() words = Dictionary.objects.filter(project=prj, language=lang).order_by(Lower('source')) page, limit = get_page_limit(request, 25) letterform = LetterForm(request.GET) if letterform.is_valid() and letterform.cleaned_data['letter'] != '': words = words.filter( source__istartswith=letterform.cleaned_data['letter']) letter = letterform.cleaned_data['letter'] else: letter = '' paginator = Paginator(words, limit) try: words = paginator.page(page) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. words = paginator.page(paginator.num_pages) last_changes = Change.objects.last_changes(request.user).filter( dictionary__project=prj, dictionary__language=lang)[:10] return render( request, 'dictionary.html', { 'title': dict_title(prj, lang), 'project': prj, 'language': lang, 'page_obj': words, 'form': form, 'query_string': 'letter={}'.format(letter) if letter else '', 'uploadform': uploadform, 'letterform': letterform, 'letter': letter, 'limit': limit, 'page': page, 'last_changes': last_changes, 'last_changes_url': urlencode({ 'project': prj.slug, 'lang': lang.code, 'glossary': 1 }), })
def list(self, request, *args, **kwargs): filters = Q() now = datetime.now() queryset = self.get_queryset() if not request.user.profile.is_manager: filters.add(Q(campaign__assigned_to=request.user), Q.AND) filters.add( Q(campaign__start_date__lte=now) & Q(campaign__end_date__gte=now), Q.AND) order_status = request.query_params.get('status', 'RUNNING') filters.add(Q(status=order_status), Q.AND) page = request.query_params.get('page') if int( request.query_params.get('page', 0)) > 0 else 0 limit = None if 'limit' in self.request.query_params: limit = self.request.query_params.get('limit') # filter contact_name = request.query_params.get('contact_name', None) email = request.query_params.get('email', None) phone = request.query_params.get('phone', None) campaign = request.query_params.get('campaign', None) no_steps = request.query_params.get('no_steps', None) selecting_state = request.query_params.get('selectingState', None) created = request.query_params.get('created', None) modified = request.query_params.get('modified', None) status_fl = request.query_params.get('status_fl', None) #status_fl = 'SUPER' state = request.query_params.get('state', None) if selecting_state: filters.add( Q(contacts__state=reverse_state_hashes[selecting_state]), Q.AND) if contact_name: filters.add( Q(contacts__first_name__icontains=contact_name) | Q(contacts__last_name__icontains=contact_name) | Q(full_name__icontains=contact_name), Q.AND) if email: filters.add(Q(contacts__mail__icontains=email), Q.AND) if phone: filters.add(Q(contacts__phone__icontains=phone), Q.AND) if campaign: filters.add(Q(campaign__name__icontains=campaign), Q.AND) if no_steps: filters.add(Q(no_steps=int(no_steps)), Q.AND) if state: try: filters.add( Q(contacts__state__icontains=reverse_state_hashes[state]), Q.AND) except: pass if created: filters.add(Q(created__date=created), Q.AND) if modified: filters.add(Q(modified__date=modified), Q.AND) if status_fl: status_fl = status_fl.split(',') status_filters = Q() if 'short' in status_fl: status_filters.add( Q(modified__range=[now - timedelta(days=4), now]), Q.OR) if 'med' in status_fl: status_filters.add( Q(modified__range=[ now - timedelta(days=15), now - timedelta(days=5) ]), Q.OR) if 'long' in status_fl: status_filters.add(Q(modified__lt=now - timedelta(days=15)), Q.OR) filters.add(status_filters, Q.AND) queryset = queryset.annotate( no_steps=Count('campaign__follow_up_plan__steps')).annotate( progress=Count('step_details', filter=Q(step_details__status='COMPLETED')) / Count('campaign__follow_up_plan__steps')).annotate( full_name=Concat('contacts__first_name', V(' '), 'contacts__last_name', output_field=CharField())).filter(filters) # order contact_order = request.query_params.get('contact_order', None) email_order = request.query_params.get('email_order', None) campaign_order = request.query_params.get('campaign_order', None) no_steps_order = request.query_params.get('no_steps_order', None) progress_order = request.query_params.get('progress_order', None) created_order = request.query_params.get('created_order', None) modified_order = request.query_params.get('modified_order', None) status_order = request.query_params.get('status_order', None) if contact_order: first_name_order = Lower('contacts__first_name').desc( ) if contact_order == 'desc' else Lower( 'contacts__last_name').asc() last_name_order = Lower('contacts__last_name').desc( ) if contact_order == 'desc' else Lower( 'contacts__last_name').asc() queryset = queryset.order_by(first_name_order, last_name_order) elif email_order: email_order = Lower('contacts__mail').desc( ) if email_order == 'desc' else Lower('contacts__mail').asc() queryset = queryset.order_by(email_order) elif campaign_order: campaign_order = '-campaign__name' if campaign_order == 'desc' else 'campaign__name' queryset = queryset.order_by(campaign_order) elif no_steps_order: no_steps_order = '-no_steps' if no_steps_order == 'desc' else 'no_steps' queryset = queryset.order_by(no_steps) elif progress_order: progress_order = '-progress' if progress_order == 'desc' else 'progress' queryset = queryset.order_by(progress_order) elif created_order: created_order = '-created' if created_order == 'desc' else 'created' queryset = queryset.order_by(created_order) elif modified_order: modified_order = '-modified' if modified_order == 'desc' else 'modified' queryset = queryset.order_by(modified_order) elif status_order: status_order = '-modified' if status_order == 'desc' else 'modified' queryset = queryset.order_by(status_order) if limit: query = queryset[int(page) * int(limit):int(page) * int(limit) + int(limit)] serializer = self.get_serializer(query, many=True) else: serializer = self.get_serializer(queryset.filter(filters), many=True) new_data = {"data": serializer.data, "total": queryset.count()} # if order_status == 'COMPLETED': # import functools # total = 0 # target = serializer.data[1] # total = functools.reduce( # lambda acc, l: acc+l['package']['prices']['999999'], target['lifetime_licenses'], 0) # total += functools.reduce(lambda acc, l: acc + # l['package']['prices'][l['duration']], target['licenses'], 0) # print(render_to_string( # 'invoices/index.html', {'orders': target, 'total': total})) return Response(new_data, status=status.HTTP_200_OK)
def default_order_by(self): return self.order_by( F('priority').asc(nulls_last=True), Lower('title'), )
def sorter_username(self, queryset): return Member.objects.all().order_by(Lower('username')).values_list('username', flat=True)
class CourseRunViewSet(ValidElasticSearchQueryRequiredMixin, viewsets.ModelViewSet): """ CourseRun resource. """ filter_backends = (DjangoFilterBackend, OrderingFilter) filterset_class = filters.CourseRunFilter lookup_field = 'key' lookup_value_regex = COURSE_RUN_ID_REGEX ordering_fields = ('start', ) permission_classes = (IsAuthenticated, IsCourseRunEditorOrDjangoOrReadOnly) queryset = CourseRun.objects.all().order_by(Lower('key')) serializer_class = serializers.CourseRunWithProgramsSerializer metadata_class = MetadataWithRelatedChoices metadata_related_choices_whitelist = ('content_language', 'level_type', 'transcript_languages', 'expected_program_type', 'type') # Explicitly support PageNumberPagination and LimitOffsetPagination. Future # versions of this API should only support the system default, PageNumberPagination. pagination_class = ProxiedPagination def get_queryset(self): """ List one course run --- parameters: - name: include_deleted_programs description: Will include deleted programs in the associated programs array required: false type: integer paramType: query multiple: false """ q = self.request.query_params.get('q') partner = self.request.site.partner edit_mode = get_query_param( self.request, 'editable') or self.request.method not in SAFE_METHODS if edit_mode and q: raise EditableAndQUnsupported() if edit_mode and (not self.request.user.is_staff and not is_publisher_user(self.request.user)): raise PermissionDenied if edit_mode: queryset = CourseRun.objects.filter_drafts() queryset = CourseEditor.editable_course_runs( self.request.user, queryset) else: queryset = self.queryset if q: qs = SearchQuerySetWrapper( CourseRun.search(q).filter('term', partner=partner.short_code)) # This is necessary to avoid issues with the filter backend. qs.model = self.queryset.model return qs queryset = queryset.filter(course__partner=partner) return self.get_serializer_class().prefetch_queryset(queryset=queryset) def get_serializer_context(self): context = super().get_serializer_context() context.update({ 'exclude_utm': get_query_param(self.request, 'exclude_utm'), 'include_deleted_programs': get_query_param(self.request, 'include_deleted_programs'), 'include_unpublished_programs': get_query_param(self.request, 'include_unpublished_programs'), 'include_retired_programs': get_query_param(self.request, 'include_retired_programs'), }) return context def list(self, request, *args, **kwargs): """ List all course runs. --- parameters: - name: q description: Elasticsearch querystring query. This filter takes precedence over other filters. required: false type: string paramType: query multiple: false - name: keys description: Filter by keys (comma-separated list) required: false type: string paramType: query multiple: false - name: hidden description: Filter based on wether the course run is hidden from search. required: false type: Boolean paramType: query multiple: false - name: active description: Retrieve active course runs. A course is considered active if its end date has not passed, and it is open for enrollment. required: false type: integer paramType: query multiple: false - name: marketable description: Retrieve marketable course runs. A course run is considered marketable if it has a marketing slug. required: false type: integer paramType: query multiple: false - name: exclude_utm description: Exclude UTM parameters from marketing URLs. required: false type: integer paramType: query multiple: false - name: include_deleted_programs description: Will include deleted programs in the associated programs array required: false type: integer paramType: query multiple: false - name: include_unpublished_programs description: Will include unpublished programs in the associated programs array required: false type: integer paramType: query multiple: false - name: include_retired_programs description: Will include retired programs in the associated programs array required: false type: integer paramType: query multiple: false """ return super().list(request, *args, **kwargs) @classmethod def push_to_studio(cls, request, course_run, create=False, old_course_run_key=None): if course_run.course.partner.studio_url: api = StudioAPI(course_run.course.partner.studio_api_client) api.push_to_studio(course_run, create, old_course_run_key, user=request.user) else: log.info( 'Not pushing course run info for %s to Studio as partner %s has no studio_url set.', course_run.key, course_run.course.partner.short_code) @classmethod def update_course_run_image_in_studio(cls, course_run): if course_run.course.partner.studio_url: api = StudioAPI(course_run.course.partner.studio_api_client) api.update_course_run_image_in_studio(course_run) else: log.info( 'Not updating course run image for %s to Studio as partner %s has no studio_url set.', course_run.key, course_run.course.partner.short_code) @writable_request_wrapper def create_run_helper(self, run_data, request=None): # These are both required to be part of self because when we call self.get_serializer, it tries # to set these two variables as part of the serializer context. When the endpoint is hit directly, # self.request should exist, but when this function is called from the Course POST endpoint in courses.py # we have to manually set these values. if not hasattr(self, 'request'): self.request = request # pylint: disable=attribute-defined-outside-init if not hasattr(self, 'format_kwarg'): self.format_kwarg = None # pylint: disable=attribute-defined-outside-init # Set a pacing default when creating (studio requires this to be set, even though discovery does not) run_data.setdefault('pacing_type', 'instructor_paced') # Guard against externally setting the draft state run_data.pop('draft', None) prices = run_data.pop('prices', {}) # Grab any existing course run for this course (we'll use it when talking to studio to form basis of rerun) course_key = run_data.get('course', None) # required field if not course_key: raise ValidationError({'course': ['This field is required.']}) # Before creating the serializer we need to ensure the course has draft rows as expected # The serializer will attempt to retrieve the draft version of the Course course = Course.objects.filter_drafts().get(key=course_key) course = ensure_draft_world(course) old_course_run_key = run_data.pop('rerun', None) serializer = self.get_serializer(data=run_data) serializer.is_valid(raise_exception=True) # Save run to database course_run = serializer.save(draft=True) course_run.update_or_create_seats(course_run.type, prices) # Set canonical course run if needed (done this way to match historical behavior - but shouldn't this be # updated *each* time we make a new run?) if not course.canonical_course_run: course.canonical_course_run = course_run course.save() elif not old_course_run_key: # On a rerun, only set the old course run key to the canonical key if a rerun hasn't been provided # This will prevent a breaking change if users of this endpoint don't choose to provide a key on rerun old_course_run_key = course.canonical_course_run.key if old_course_run_key: old_course_run = CourseRun.objects.filter_drafts().get( key=old_course_run_key) course_run.language = old_course_run.language course_run.min_effort = old_course_run.min_effort course_run.max_effort = old_course_run.max_effort course_run.weeks_to_complete = old_course_run.weeks_to_complete course_run.save() course_run.staff.set(old_course_run.staff.all()) course_run.transcript_languages.set( old_course_run.transcript_languages.all()) # And finally, push run to studio self.push_to_studio(self.request, course_run, create=True, old_course_run_key=old_course_run_key) headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers) def create(self, request, *args, **kwargs): """ Create a course run object. """ response = self.create_run_helper(request.data) if response.status_code == 201: run_key = response.data.get('key') course_run = CourseRun.everything.get(key=run_key, draft=True) self.update_course_run_image_in_studio(course_run) return response @writable_request_wrapper def _update_course_run(self, course_run, draft, changed, serializer, request, prices): save_kwargs = {} # If changes are made after review and before publish, revert status to unpublished. # Unless we're just switching the status non_exempt_update = changed and course_run.status == CourseRunStatus.Reviewed if non_exempt_update: save_kwargs['status'] = CourseRunStatus.Unpublished official_run = course_run.official_version official_run.status = CourseRunStatus.Unpublished official_run.save() # When the course run is being updated and is coming from the Unpublished state, we always want to set # it's status to in legal review. If it is coming from the Reviewed state, we only want to put it # back into legal review if a non exempt field was changed (expected_program_name and expected_program_type) if not draft and (course_run.status == CourseRunStatus.Unpublished or non_exempt_update): save_kwargs['status'] = CourseRunStatus.LegalReview course_run = serializer.save(**save_kwargs) if course_run in course_run.course.active_course_runs: course_run.update_or_create_seats(course_run.type, prices) self.push_to_studio(request, course_run, create=False) # Published course runs can be re-published directly or course runs that remain in the Reviewed # state can update their official version. We want to do this even in the Reviewed case for # when an exempt field is changed and we still want to update the official even though we don't # want to completely unpublish it. if ((not draft and course_run.status == CourseRunStatus.Published) or course_run.status == CourseRunStatus.Reviewed): course_run.update_or_create_official_version() return Response(serializer.data) def handle_internal_review(self, request, serializer): # Disallow updates on non internal review fields while course is in review for key in request.data.keys(): if key not in CourseRun.INTERNAL_REVIEW_FIELDS: return Response(_( 'Can only update status, ofac restrictions, and ofac comment' ), status=status.HTTP_400_BAD_REQUEST) serializer.save() return Response(serializer.data) # pylint: disable=arguments-differ def update(self, request, **kwargs): # logging to help debug error around course url slugs incrementing log.info('The raw course run data coming from publisher is {}.'.format( request.data)) # Update one, or more, fields for a course run. course_run = self.get_object() course_run = ensure_draft_world(course_run) # always work on drafts partial = kwargs.pop('partial', False) # Sending draft=False triggers the review process for unpublished courses draft = request.data.pop( 'draft', True) # Don't let draft parameter trickle down prices = request.data.pop('prices', {}) serializer = self.get_serializer(course_run, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) # Handle staff update on course run in review with valid status transition if (request.user.is_staff and course_run.in_review and 'status' in request.data and request.data['status'] in CourseRunStatus.INTERNAL_STATUS_TRANSITIONS): return self.handle_internal_review(request, serializer) # Handle regular non-internal update request.data.pop('status', None) # Status management is handled in the model serializer.validated_data.pop( 'status', None) # Status management is handled in the model # Disallow patch or put if the course run is in review. if course_run.in_review: return Response(_('Course run is in review. Editing disabled.'), status=status.HTTP_403_FORBIDDEN) # Disallow internal review fields when course run is not in review for key in request.data.keys(): if key in CourseRun.INTERNAL_REVIEW_FIELDS: return Response(_('Invalid parameter'), status=status.HTTP_400_BAD_REQUEST) changed_fields = reviewable_data_has_changed( course_run, serializer.validated_data.items(), CourseRun.STATUS_CHANGE_EXEMPT_FIELDS) response = self._update_course_run(course_run, draft, bool(changed_fields), serializer, request, prices) self.update_course_run_image_in_studio(course_run) return response def retrieve(self, request, *args, **kwargs): """ Retrieve details for a course run. """ return super().retrieve(request, *args, **kwargs) @action(detail=False) def contains(self, request): """ Determine if course runs are found in the query results. A dictionary mapping course run keys to booleans, indicating course run presence, will be returned. --- serializer: serializers.ContainedCourseRunsSerializer parameters: - name: query description: Elasticsearch querystring query required: true type: string paramType: query multiple: false - name: course_run_ids description: Comma-separated list of course run IDs required: true type: string paramType: query multiple: true - name: partner description: Filter by partner required: false type: string paramType: query multiple: false """ query = request.GET.get('query') course_run_ids = request.GET.get('course_run_ids') partner = self.request.site.partner if query and course_run_ids: course_run_ids = course_run_ids.split(',') course_runs = (CourseRun.search(query).filter( ESDSLQ('term', partner=partner.short_code) & ESDSLQ('terms', **{'key.raw': course_run_ids})).source( ['key'])) course_runs_keys = [i.key for i in course_runs] contains = { course_run_id: course_run_id in course_runs_keys for course_run_id in course_run_ids } instance = {'course_runs': contains} serializer = serializers.ContainedCourseRunsSerializer(instance) return Response(serializer.data) return Response(status=status.HTTP_400_BAD_REQUEST) # pylint: disable=arguments-differ def destroy(self, _request, *_args, **_kwargs): """ Delete a course run. """ # Not supported return Response(status=status.HTTP_405_METHOD_NOT_ALLOWED)
def task_chooser(request): task_models = get_task_types() create_model = None can_create = False if task_permission_policy.user_has_permission(request.user, 'add'): can_create = len(task_models) != 0 if len(task_models) == 1: create_model = task_models[0] elif 'create_model' in request.GET: create_model = resolve_model_string(request.GET['create_model']) if create_model not in task_models: raise Http404 # Build task types list for "select task type" view task_types = [ (model.get_verbose_name(), model._meta.app_label, model._meta.model_name, model.get_description()) for model in task_models ] # sort by lower-cased version of verbose name task_types.sort(key=lambda task_type: task_type[0].lower()) # Build task type choices for filter on "existing task" tab task_type_choices = [ (model, model.get_verbose_name()) for model in task_models ] task_type_choices.sort(key=lambda task_type: task_type[1].lower()) if create_model: createform_class = get_task_form_class(create_model) else: createform_class = None q = None if 'q' in request.GET or 'p' in request.GET or 'task_type' in request.GET: searchform = TaskChooserSearchForm(request.GET, task_type_choices=task_type_choices) tasks = all_tasks = searchform.task_model.objects.order_by(Lower('name')) q = '' if searchform.is_searching(): # Note: I decided not to use wagtailsearch here. This is because # wagtailsearch creates a new index for each model you make # searchable and this might affect someone's quota. I doubt there # would ever be enough tasks to require using anything more than # an icontains anyway. q = searchform.cleaned_data['q'] tasks = tasks.filter(name__icontains=q) # Pagination paginator = Paginator(tasks, per_page=10) tasks = paginator.get_page(request.GET.get('p')) return TemplateResponse(request, "wagtailadmin/workflows/task_chooser/includes/results.html", { 'task_types': task_types, 'searchform': searchform, 'tasks': tasks, 'all_tasks': all_tasks, 'query_string': q, }) else: if createform_class: if request.method == 'POST': createform = createform_class(request.POST, request.FILES, prefix='create-task') if createform.is_valid(): task = createform.save() response = render_modal_workflow( request, None, None, None, json_data={'step': 'task_chosen', 'result': get_task_result_data(task)} ) # Use a different status code so we can tell the difference between validation errors and successful creations response.status_code = 201 return response else: createform = createform_class(prefix='create-task') else: if request.method == 'POST': return HttpResponseBadRequest() createform = None searchform = TaskChooserSearchForm(task_type_choices=task_type_choices) tasks = searchform.task_model.objects.order_by(Lower('name')) paginator = Paginator(tasks, per_page=10) tasks = paginator.get_page(request.GET.get('p')) return render_modal_workflow(request, 'wagtailadmin/workflows/task_chooser/chooser.html', None, { 'task_types': task_types, 'tasks': tasks, 'searchform': searchform, 'createform': createform, 'can_create': can_create, 'add_url': reverse('wagtailadmin_workflows:task_chooser') + '?' + request.GET.urlencode() if create_model else None }, json_data=get_chooser_context())
def order_by_title(request): posts = Post.objects.all().order_by(Lower('title')) posts = paginator(request, posts) return render(request, "posts/posts_list.html", {"posts":posts})
def s_home(request, slug=None): name_order = '' cut_off_order = '' last_date_order = '' form = home_search_form() if request.method == 'POST': form = home_search_form(request.POST) if form.is_valid(): str1 = form.cleaned_data.get('slug') return redirect('student:s_home_search', slug=str1) if slug == None: c_list = company.objects.all() else: slug = str(slug) try: br = str(slug) br = br.upper() c_list = company.objects.filter(branch_allowed__name__icontains=br) if c_list: messages.success( request, "Search results for company allowing branch " + str(slug) + " :") except company.DoesNotExist: c_list = None if not c_list: try: c_list = company.objects.filter(name__icontains=slug) if c_list: messages.success( request, "Search results for company name containing " + str(slug) + " :") except company.DoesNotExist: c_list = None if request.GET.get('orderby'): o = str(request.GET.get('orderby')) if o == 'cutoff': c_list = c_list.order_by('cutoff') cut_off_order = 'active' elif o == 'lastdate': c_list = c_list.order_by('closing_date') last_date_order = 'active' else: c_list = c_list.order_by(Lower('name')) name_order = 'active' else: c_list = c_list.order_by(Lower('name')) name_order = 'active' date = django_time.localdate() c_list = c_list.filter(closing_date__gte=date) b_q = False arg = { 'clist': c_list.filter(for_batch=request.user.student_profile.batch), 'name_order': name_order, 'cut_off_order': cut_off_order, 'last_date_order': last_date_order, 'date': date, 'branch_qualification': b_q, 'zero': 0 } return render(request, 's_home.html', arg)
def get_all_restaurants_from_team(self, team): return Restaurant.objects.filter(restaurant_for_team=team).order_by( Lower('restaurant_name'))
def get_ordering(self, request: HttpRequest) -> List[str]: return [Lower("name")]
def show_outcome(request): outcomes = [] onlineclass = None if request.method == 'POST': form = OutcomeForm(request.POST, user=request.user) if form.is_valid(): onlineclass = form.cleaned_data['onlineclass'] chapter = form.cleaned_data['chapter'] # Get class problems problems_all = Problem.objects.filter(chapter=chapter).order_by( 'title', 'id') problems = list(problems_all.values_list('id', flat=True)) # Get latest student outcome for every student in class students = UserLogView.objects.filter( user__userprofile__user_class=onlineclass, problem_id__in=problems, timestamp__gte=onlineclass.start_date).order_by( Lower('user__first_name').asc(), Lower('user__last_name').asc(), 'user_id', 'problem__title', 'problem_id').values('user_id', 'user__first_name', 'user__last_name', 'problem_id', 'final_outcome', 'timestamp') # For each student in class, let's organize it in a table start = time.time() if students.count(): current_student = students[0]["user_id"] student_name = "%s %s" % (students[0]["user__first_name"], students[0]["user__last_name"]) outcome_student = [(None, None)] * len(problems) for student in students: if current_student == student["user_id"]: outcome_student[problems.index( student["problem_id"])] = ( student["final_outcome"], timezone.localtime( student["timestamp"]).strftime( "%Y-%m-%d %H:%M:%S")) # Previous student is finished, lets start a new one else: only_outcomes = list(zip(*outcome_student))[0] student_row = { "name": (student_name, current_student), "outcomes": outcome_student, "total": { "P": only_outcomes.count("P"), "F": only_outcomes.count("F"), "S": only_outcomes.count("S") } } outcomes.append(student_row) # Get new student outcome_student = [(None, None)] * len(problems) current_student = student["user_id"] student_name = "%s %s" % (student["user__first_name"], student["user__last_name"]) outcome_student[problems.index( student["problem_id"])] = ( student["final_outcome"], timezone.localtime( student["timestamp"]).strftime( "%Y-%m-%d %H:%M:%S")) # Add last student outcome_student[problems.index(student["problem_id"])] = ( student["final_outcome"], timezone.localtime( student["timestamp"]).strftime("%Y-%m-%d %H:%M:%S")) only_outcomes = list(zip(*outcome_student))[0] student_row = { "name": (student_name, current_student), "outcomes": outcome_student, "total": { "P": only_outcomes.count("P"), "F": only_outcomes.count("F"), "S": only_outcomes.count("S") } } outcomes.append(student_row) end = time.time() LOGGER.info("Elapsed time: %d" % (end - start)) else: form = OutcomeForm() problems_all = [] form.fields['onlineclass'].queryset = OnlineClass.objects.filter( professor__user=request.user).order_by('name') LOGGER.info("Available classes: %s" % form.fields['onlineclass'].queryset) LOGGER.info("Showing students and outcomes: %s" % json.dumps(outcomes)) return render( request, 'questions/outcomes.html', { 'title': _('Outcomes'), 'form': form, 'problems': problems_all, 'outcomes': outcomes, 'class': onlineclass })
def get_children(self, obj): children = GoodsCategory.objects.filter(parent_id=obj.cat_id).values( id=Lower('cat_id'), label=LTrim('cat_name')).all() return children
class CourseForm(BaseForm): organization = forms.ModelChoiceField( queryset=Organization.objects.filter( organization_extension__organization_id__isnull=False ).order_by(Lower('key')), label=_('Organization Name'), required=True ) title = forms.CharField(label=_('Course Title'), required=True) number = forms.CharField( label=_('Course Number'), required=True, validators=[validate_text_count(max_length=50)] ) short_description = forms.CharField( label=_('Short Description'), widget=forms.Textarea, required=False, validators=[validate_text_count(max_length=255)] ) full_description = forms.CharField( label=_('Long Description'), widget=forms.Textarea, required=False, validators=[validate_text_count(max_length=2500)] ) prerequisites = forms.CharField( label=_('Prerequisites'), widget=forms.Textarea, required=False, validators=[validate_text_count(max_length=1000)] ) # users will be loaded through AJAX call based on organization team_admin = UserModelChoiceField( queryset=User.objects.none(), required=True, label=_('Organization Course Admin'), ) subjects = Subject.objects.all().order_by('translations__name') primary_subject = forms.ModelChoiceField( queryset=subjects, label=_('Primary'), required=False ) secondary_subject = forms.ModelChoiceField( queryset=subjects, label=_('Additional Subject (optional)'), required=False ) tertiary_subject = forms.ModelChoiceField( queryset=subjects, label=_('Additional Subject (optional)'), required=False ) level_type = forms.ModelChoiceField( queryset=LevelType.objects.all().order_by('-name'), label=_('Level'), required=False ) expected_learnings = forms.CharField( label=_('What You Will Learn'), widget=forms.Textarea, required=False, validators=[validate_text_count(max_length=2500)] ) learner_testimonial = forms.CharField( label=_('Learner Testimonial'), widget=forms.Textarea, required=False, validators=[validate_text_count(max_length=500)] ) faq = forms.CharField( label=_('FAQ'), widget=forms.Textarea, required=False, validators=[validate_text_count(max_length=2500)] ) syllabus = forms.CharField( label=_('Syllabus'), widget=forms.Textarea, required=False, validators=[validate_text_count(max_length=2500)] ) add_new_run = forms.BooleanField(required=False) class Meta: model = Course widgets = { 'image': ClearableImageInput(attrs={'accept': 'image/*'}) } fields = ( 'title', 'number', 'short_description', 'full_description', 'expected_learnings', 'primary_subject', 'secondary_subject', 'tertiary_subject', 'prerequisites', 'image', 'team_admin', 'level_type', 'organization', 'is_seo_review', 'syllabus', 'learner_testimonial', 'faq', 'video_link', ) def __init__(self, *args, **kwargs): # In case of edit mode pre-populate the drop-downs user = kwargs.pop('user', None) organization = kwargs.pop('organization', None) if organization: org_extension = OrganizationExtension.objects.get(organization=organization) self.declared_fields['team_admin'].queryset = User.objects.filter( groups__name=org_extension.group ).order_by('full_name', 'username') if user: self.declared_fields['organization'].queryset = get_user_organizations(user) self.declared_fields['team_admin'].widget.attrs = {'data-user': user.id} super(CourseForm, self).__init__(*args, **kwargs) if user and not is_internal_user(user): self.fields['video_link'].widget = forms.HiddenInput() def clean_title(self): """ Convert all named and numeric character references in the string to the corresponding unicode characters """ return html.unescape(self.cleaned_data.get('title')) def clean_number(self): """ Validate that number doesn't consist of any special characters other than period, underscore or hyphen """ number = self.cleaned_data.get('number') if not VALID_CHARS_IN_COURSE_NUM_AND_ORG_KEY.match(number): raise ValidationError(_('Please do not use any spaces or special characters other than period, ' 'underscore or hyphen.')) return number def clean(self): cleaned_data = self.cleaned_data organization = cleaned_data.get('organization') title = cleaned_data.get('title') number = cleaned_data.get('number') instance = getattr(self, 'instance', None) if not instance.pk: if Course.objects.filter(title=title, organizations__in=[organization]).exists(): raise ValidationError({'title': _('This course title already exists')}) if Course.objects.filter(number=number, organizations__in=[organization]).exists(): raise ValidationError({'number': _('This course number already exists')}) return cleaned_data
class AvisoList(generics.ListCreateAPIView): serializer_class = AvisoSerializer queryset = Aviso.objects.all() queryset = Aviso.objects.order_by(Lower('expiration_date').desc())
def test_expression(self): q = Q(name="test") self.assertIs(q.check({"name": Lower(Value("TeSt"))}), True) self.assertIs(q.check({"name": Value("other")}), False)
def get_ordering(self, request): return [Lower('name')]
def __init__(self, *args, **kwds): super(ActivityInviteAdminForm, self).__init__(*args, **kwds) self.fields['person'].queryset = Person.objects.order_by(Lower('name'))
def user_list_view(request): mentors = Profile.objects.filter(role=True).order_by(Lower('name')) mentee = Profile.objects.filter(role=False).order_by(Lower('name')) context = {'mentors': mentors, 'mentee': mentee, 'title': "Members"} return render(request, 'member-list/trainers.html', context=context)
def get_context_data(self, **kwargs): context = super(ListLogEntry, self).get_context_data(**kwargs) context['apps_list'] = models.App.objects.order_by(Lower('name')) context['users_list'] = User.objects.order_by(Lower('username')) context['q'] = self.query_params return context