def bulk_edit(request, formset_class=forms.VideoFormSet): if ('just_the_author_field' in request.GET and 'video_id' in request.GET): # generate just the particular form that the user wants template_data = {} form_prefix = request.GET['just_the_author_field'] video = get_object_or_404(Video, pk=int(request.GET['video_id'])) form = forms.BulkEditVideoForm(instance=video, prefix=form_prefix) template_data['form'] = form template = 'localtv/admin/bulk_edit_author_widget.html' return render_to_response(template, template_data, context_instance=RequestContext(request)) site_settings = SiteSettings.objects.get_current() videos = Video.objects.filter(status=Video.ACTIVE, site=site_settings.site) if 'filter' in request.GET: filter_type = request.GET['filter'] if filter_type == 'featured': videos = videos.exclude(last_featured=None) elif filter_type == 'rejected': videos = Video.objects.filter(status=Video.REJECTED) elif filter_type == 'no-attribution': videos = videos.filter(authors=None) elif filter_type == 'no-category': videos = videos.filter(categories=None) elif filter_type == 'unapproved': videos = Video.objects.filter(status=Video.UNAPPROVED) videos = videos.select_related('feed', 'search', 'site') category = request.GET.get('category', '') try: category = int(category) except ValueError: category = '' if category != '': videos = videos.filter(categories__pk=category).distinct() author = request.GET.get('author', '') try: author = int(author) except ValueError: author = '' if author != '': videos = videos.filter(authors__pk=author).distinct() search_string = request.GET.get('q', '') if search_string != '': videos = videos.filter( Q(description__icontains=search_string) | Q(name__icontains=search_string) | Q(categories__name__icontains=search_string) | Q(user__username__icontains=search_string) | Q(user__first_name__icontains=search_string) | Q(user__last_name__icontains=search_string) | Q(video_service_user__icontains=search_string) | Q(feed__name__icontains=search_string)).distinct() headers = SortHeaders(request, ( ('Video Title', 'name'), ('Source', 'source'), ('Categories', None), ('Date Published', '-when_published'), ('Date Imported', '-when_submitted'), )) sort = headers.order_by() if sort.endswith('source'): videos = videos.extra(select={ 'name_lower':'LOWER(localtv_video.name)'}) videos = videos.order_by(sort.replace('source', 'calculated_source_type')) elif sort.endswith('name'): videos = videos.extra(select={ 'name_lower':'LOWER(localtv_video.name)'}).order_by( sort.replace('name', 'name_lower')) else: videos = videos.order_by(sort) video_paginator = Paginator(videos, 30) try: page = video_paginator.page(int(request.GET.get('page', 1))) except ValueError: return HttpResponseBadRequest('Not a page number') except EmptyPage: page = video_paginator.page(video_paginator.num_pages) if request.method == 'POST': formset = formset_class(request.POST, request.FILES, queryset=page.object_list) if formset.is_valid(): formset.save() if 'successful' in request.GET: path_with_success = request.get_full_path() else: path = request.get_full_path() if '?' in path: path_with_success = path + '&successful' else: path_with_success = path + '?successful' return HttpResponseRedirect(path_with_success) else: formset = formset_class(queryset=page.object_list) return render_to_response('localtv/admin/bulk_edit.html', {'formset': formset, 'headers': headers, 'search_string': search_string, 'page': page, 'categories': formset._qs_cache['categories'], 'users': formset._qs_cache['authors']}, context_instance=RequestContext(request))
def bulk_edit(request): if ('just_the_author_field' in request.GET and 'video_id' in request.GET): # generate just the particular form that the user wants template_data = {} form_prefix = request.GET['just_the_author_field'] video = get_object_or_404(Video, pk=int(request.GET['video_id'])) cache_for_form_optimization = {} form = forms.BulkEditVideoForm( instance=video, prefix=form_prefix, cache_for_form_optimization=cache_for_form_optimization) template_data['form'] = form template = 'localtv/admin/bulk_edit_author_widget.html' return render_to_response(template, template_data, context_instance=RequestContext(request)) sitelocation = SiteLocation.objects.get_current() videos = Video.objects.filter(status=Video.ACTIVE, site=sitelocation.site) if 'filter' in request.GET: filter_type = request.GET['filter'] if filter_type == 'featured': videos = videos.exclude(last_featured=None) elif filter_type == 'rejected': videos = Video.objects.filter(status=Video.REJECTED) elif filter_type == 'no-attribution': videos = videos.filter(authors=None) elif filter_type == 'no-category': videos = videos.filter(categories=None) elif filter_type == 'unapproved': videos = Video.objects.filter(status=Video.UNAPPROVED) videos = videos.select_related('feed', 'search', 'site') category = request.GET.get('category', '') try: category = int(category) except ValueError: category = '' if category != '': videos = videos.filter(categories__pk=category).distinct() author = request.GET.get('author', '') try: author = int(author) except ValueError: author = '' if author != '': videos = videos.filter(authors__pk=author).distinct() search_string = request.GET.get('q', '') if search_string != '': videos = videos.filter( Q(description__icontains=search_string) | Q(name__icontains=search_string) | Q(categories__name__icontains=search_string) | Q(user__username__icontains=search_string) | Q(user__first_name__icontains=search_string) | Q(user__last_name__icontains=search_string) | Q(video_service_user__icontains=search_string) | Q(feed__name__icontains=search_string)).distinct() headers = SortHeaders(request, ( ('Video Title', 'name'), ('Source', 'source'), ('Categories', None), ('Date Published', '-when_published'), ('Date Imported', '-when_submitted'), )) sort = headers.order_by() if sort.endswith('source'): reverse = sort.startswith('-') videos = videos.extra( select={'name_lower': 'LOWER(localtv_video.name)'}) videos = videos.order_by( sort.replace('source', 'calculated_source_type')) elif sort.endswith('name'): videos = videos.extra(select={ 'name_lower': 'LOWER(localtv_video.name)' }).order_by(sort.replace('name', 'name_lower')) else: videos = videos.order_by(sort) video_paginator = Paginator(videos, 30) try: page = video_paginator.page(int(request.GET.get('page', 1))) except ValueError: return HttpResponseBadRequest('Not a page number') except EmptyPage: page = video_paginator.page(video_paginator.num_pages) if request.method == 'POST': formset = forms.VideoFormSet(request.POST, request.FILES, queryset=page.object_list) if formset.is_valid(): tier_prevented_some_action = False tier = sitelocation.get_tier() videos_approved_so_far = 0 for form in list(formset.deleted_forms): form.cleaned_data[DELETION_FIELD_NAME] = False form.instance.status = Video.REJECTED form.instance.save() bulk_edits = formset.extra_forms[0].cleaned_data for key in list( bulk_edits.keys()): # get the list because we'll be # changing the dictionary if not bulk_edits[key]: del bulk_edits[key] bulk_action = request.POST.get('bulk_action', '') if bulk_action: bulk_edits['action'] = bulk_action if bulk_edits: for form in formset.initial_forms: if not form.cleaned_data['BULK']: continue for key, value in bulk_edits.items(): if key == 'action': # do something to the video if value == 'delete': form.instance.status = Video.REJECTED elif value == 'approve': if (sitelocation.enforce_tiers() and tier.remaining_videos() <= videos_approved_so_far): tier_prevented_some_action = True else: form.instance.status = Video.ACTIVE videos_approved_so_far += 1 elif value == 'unapprove': form.instance.status = Video.UNAPPROVED elif value == 'feature': if not form.instance.status == Video.ACTIVE: if (sitelocation.enforce_tiers() and tier.remaining_videos() <= videos_approved_so_far): tier_prevented_some_action = True else: form.instance.status = Video.ACTIVE if form.instance.status == Video.ACTIVE: form.instance.last_featured = datetime.now( ) elif value == 'unfeature': form.instance.last_featured = None elif key == 'tags': form.cleaned_data[key] = value elif key == 'categories': # categories append, not replace form.cleaned_data[key] = ( list(form.cleaned_data[key]) + list(value)) elif key == 'authors': form.cleaned_data[key] = value else: setattr(form.instance, key, value) formset.forms = formset.initial_forms # get rid of the extra bulk # edit form formset.can_delete = False formset.save() path_with_success = None if 'successful' in request.GET: path_with_success = request.get_full_path() else: path = request.get_full_path() if '?' in path: path_with_success = path + '&successful' else: path_with_success = path + '?successful' if tier_prevented_some_action: path = path_with_success + '¬_all_actions_done' else: path = path_with_success return HttpResponseRedirect(path) else: formset = forms.VideoFormSet(queryset=page.object_list) return render_to_response( 'localtv/admin/bulk_edit.html', { 'formset': formset, 'headers': headers, 'search_string': search_string, 'page': page, 'categories': Category.objects.filter(site=sitelocation.site), 'users': User.objects.order_by('username') }, context_instance=RequestContext(request))