def view_audio_file(request, pk, _): """Using the ID, return the oral argument page. We also test if the item is a favorite and send data as such. """ af = get_object_or_404(Audio, pk=pk) title = "Oral Argument for " + trunc(af.case_name, 100) get_string = search_utils.make_get_string(request) try: fave = Favorite.objects.get(audio_id=af.pk, users__user=request.user) favorite_form = FavoriteForm(instance=fave) except (ObjectDoesNotExist, TypeError): # Not favorited or anonymous user favorite_form = FavoriteForm(initial={ 'audio_id': af.pk, 'name': af.docket.case_name }) return render_to_response( 'audio/oral_argument.html', { 'title': title, 'af': af, 'favorite_form': favorite_form, 'get_string': get_string, 'private': af.blocked, }, RequestContext(request))
def view_favorites(request): favorites = Favorite.objects.filter(users__user=request.user).order_by( 'pk') favorite_forms = [] for favorite in favorites: favorite_forms.append(FavoriteForm(instance=favorite)) return render_to_response('profile/favorites.html', {'private': True, 'favorite_forms': favorite_forms, 'blank_favorite_form': FavoriteForm()}, RequestContext(request))
def view_opinion(request, pk, _): """Using the ID, return the document. We also test if the document ID is a favorite for the user, and send data as such. If it's a favorite, we send the bound form for the favorite so it can populate the form on the page. If it is not a favorite, we send the unbound form. """ # Look up the court, document, title and favorite information doc = get_object_or_404(Document, pk=pk) citation_string = make_citation_string(doc) title = '%s, %s' % (trunc(doc.citation.case_name, 100), citation_string) get_string = search_utils.make_get_string(request) try: fave = Favorite.objects.get(doc_id=doc.pk, users__user=request.user) favorite_form = FavoriteForm(instance=fave) except (ObjectDoesNotExist, TypeError): # Not favorited or anonymous user favorite_form = FavoriteForm( initial={ 'doc_id': doc.pk, 'name': trunc(doc.citation.case_name, 100, ellipsis='...'), } ) # get most influential opinions that cite this opinion cited_by_trunc = doc.citation.citing_opinions.select_related( 'citation').order_by('-citation_count', '-date_filed')[:5] authorities_trunc = doc.cases_cited.all().select_related( 'document').order_by('case_name')[:5] authorities_count = doc.cases_cited.all().count() return render_to_response( 'casepage/view_opinion.html', {'title': title, 'citation_string': citation_string, 'doc': doc, 'favorite_form': favorite_form, 'get_string': get_string, 'private': doc.blocked, 'cited_by_trunc': cited_by_trunc, 'authorities_trunc': authorities_trunc, 'authorities_count': authorities_count}, RequestContext(request) )
def save_or_update_favorite(request): """Uses ajax to save or update a favorite. Receives a request as an argument, and then uses that plus POST data to create or update a favorite in the database for a specific user. If the user already has the document favorited, it updates the favorite with the new information. If not, it creates a new favorite. """ if request.is_ajax(): # If it's an ajax request, gather the data from the form, save it to # the DB, and then return a success code. audio_pk = request.POST.get('audio_id') doc_pk = request.POST.get('doc_id') if audio_pk and audio_pk != 'undefined': af = Audio.objects.get(pk=audio_pk) try: fave = Favorite.objects.get(audio_id=af, users__user=request.user) except ObjectDoesNotExist: fave = Favorite() elif doc_pk and doc_pk != 'undefined': doc = Document.objects.get(pk=doc_pk) try: fave = Favorite.objects.get(doc_id=doc, users__user=request.user) except ObjectDoesNotExist: fave = Favorite() else: return Http404("Unknown document or audio id") f = FavoriteForm(request.POST, instance=fave) if f.is_valid(): new_fave = f.save() up = request.user.profile up.favorite.add(new_fave) up.save() else: # Validation errors fail silently. Probably could be better. return HttpResponseServerError("Failure. Form invalid") return HttpResponse("It worked") else: return HttpResponseNotAllowed( permitted_methods={'POST'}, content="Not an ajax request." )
def save_or_update_favorite(request): """Uses ajax to save or update a favorite. Receives a request as an argument, and then uses that plus POST data to create or update a favorite in the database for a specific user. If the user already has the document favorited, it updates the favorite with the new information. If not, it creates a new favorite. """ if request.is_ajax(): # If it's an ajax request, gather the data from the form, save it to # the DB, and then return a success code. audio_pk = request.POST.get('audio_id') doc_pk = request.POST.get('doc_id') if audio_pk and audio_pk != 'undefined': af = Audio.objects.get(pk=audio_pk) try: fave = Favorite.objects.get(audio_id=af, users__user=request.user) except ObjectDoesNotExist: fave = Favorite() elif doc_pk and doc_pk != 'undefined': doc = Document.objects.get(pk=doc_pk) try: fave = Favorite.objects.get(doc_id=doc, users__user=request.user) except ObjectDoesNotExist: fave = Favorite() else: return Http404("Unknown document or audio id") f = FavoriteForm(request.POST, instance=fave) if f.is_valid(): new_fave = f.save() up = request.user.profile up.favorite.add(new_fave) up.save() else: # Validation errors fail silently. Probably could be better. return HttpResponseServerError("Failure. Form invalid") return HttpResponse("It worked") else: return HttpResponseNotAllowed(permitted_methods={'POST'}, content="Not an ajax request.")
def edit_favorite(request, fave_id): """Provide a form for the user to update alerts, or do so if submitted via POST """ try: fave_id = int(fave_id) except: return HttpResponseRedirect('/') try: fave = Favorite.objects.get(id=fave_id, users__user=request.user) doc = fave.doc_id citation_string = make_citation_string(doc) except ObjectDoesNotExist: # User lacks access to this fave or it doesn't exist. return HttpResponseRedirect('/') if request.method == 'POST': form = FavoriteForm(request.POST, instance=fave) if form.is_valid(): form.save() messages.add_message(request, messages.SUCCESS, 'Your favorite was saved successfully.') # redirect to the alerts page return HttpResponseRedirect('/profile/favorites/') else: # the form is loading for the first time form = FavoriteForm(instance=fave) return render_to_response('profile/edit_favorite.html', {'favorite_form': form, 'doc': doc, 'citation_string': citation_string, 'private': False}, RequestContext(request))
def save_or_update_favorite(request): """Uses ajax to save or update a favorite. Receives a request as an argument, and then uses that plus POST data to create or update a favorite in the database for a specific user. If the user already has the document favorited, it updates the favorite with the new information. If not, it creates a new favorite. """ if request.is_ajax(): # If it's an ajax request, gather the data from the form, save it to # the DB, and then return a success code. try: doc_id = request.POST['doc_id'] except: return HttpResponse("Unknown doc_id") doc = Document.objects.get(pk=doc_id) try: fave = Favorite.objects.get(doc_id=doc, users__user=request.user) except ObjectDoesNotExist: fave = Favorite() f = FavoriteForm(request.POST, instance=fave) if f.is_valid(): new_fave = f.save() up = request.user.profile up.favorite.add(new_fave) up.save() else: # Validation errors fail silently. Probably could be better. HttpResponse("Failure. Form invalid") return HttpResponse("It worked") else: return HttpResponse("Not an ajax request.")