def shared_category(request, id): try: cat = Category.objects.get(share_url=id) words = list(Word.objects.filter(category_id=cat.id)) if not request.user.is_authenticated(): return render(request, "phrasebook/shared-category.html", context={"logged_in": False, "category": cat, "words": words, "category__len": words.__len__()}) else: if cat.user == request.user: return redirect("phrasebook:get_category", cat.id) else: return render(request, "phrasebook/shared-category.html", context=get_sidebar_args(request, {"logged_in": True, "category": cat})) except ObjectDoesNotExist: if request.user.is_authenticated(): return render(request, "phrasebook/category-not-exist.html", context=get_sidebar_args(request, {"logged_in": True})) else: return render(request, "phrasebook/category-not-exist.html", context={"logged_in": False})
def profile(request): ctx = get_sidebar_args(request, {}) ctx.update({ 'extra_langs': ctx['languages_len'] - 5, 'last_name': request.user.last_name }) return render(request, 'phrasebook/profile.html', context=ctx)
def all(request): ctx = get_sidebar_args(request, {}) words = list( Word.objects.filter(category__user=request.user, category__language=ctx['current_language'])) ctx.update({ 'words': words, 'words__len': words.__len__(), "all_words": "active" }) return render(request, 'phrasebook/all-words.html', context=ctx)
def app(request): words = Word.objects.filter( category__user=request.user).order_by("-created_on")[:10] return render(request, 'phrasebook/app.html', context=get_sidebar_args( request, { "words": words, "words__len": words.__len__(), "page_app": "active" }))
def new_category(request): if request.method == "GET": return render(request, "phrasebook/newcategory.html", context=get_sidebar_args(request, {"new_category": "active"})) elif request.method == "POST": name = request.POST.get('category') shortened = request.POST.get('shortened')[:3] current_language = Language.objects.get(flag_name=request.session['current_language']) new_cat = Category(name=name, shortened=shortened, language=current_language, user=request.user) new_cat.save() return redirect("phrasebook:all_categories") else: return redirect("phrasebook:app")
def category_notes(request, id): try: cat = Category.objects.get(id=id) except ObjectDoesNotExist: cat = None words = Word.objects.filter(category=cat).order_by('-starred', 'foreign') if cat is not None and cat.user == request.user: return render(request, "phrasebook/categorynotes.html", context=get_sidebar_args(request, {"category": cat, "words": words, "category__len": words.count, "current_category_id": cat.id, "notes": cat.description})) else: return redirect("phrasebook:all_categories")
def all_categories(request): if request.method == "GET": cats = list( Category.objects.filter(user=request.user, language__flag_name=request.session['current_language']).order_by( '-pinned', 'name').annotate(num_words=Count('word'))) return render(request, "phrasebook/allcategories.html", context=get_sidebar_args(request, {"categories": cats, "all_categories": "active"})) elif request.method == "POST": user_categories = list( Category.objects.filter(user=request.user, language__flag_name=request.session['current_language'])) word_id = request.POST.get('word_id') try: word = Word.objects.get(id=word_id) return render(request, "phrasebook/categorylist.html", context={"categories": user_categories, "word_id": word_id, "word": word}) except ObjectDoesNotExist: return JsonResponse({"status": "error"})
def new_word(request, cid): category = Category.objects.get(id=cid) if category.user != request.user: return redirect('phrasebook:app') if request.method == "GET": return render(request, 'phrasebook/newword.html', context=get_sidebar_args(request, {"current_category": category})) elif request.method == "POST": foreign = request.POST.get('foreign') english = request.POST.get('english') if foreign is not "" and english is not "": word = Word(foreign=foreign, english=english, category=category) word.save() return redirect('phrasebook:get_category', cid) else: return redirect('phrasebook:app') else: return redirect('phrasebook:app')
def app(request): return render(request, 'phrasebook/app.html', context=get_sidebar_args(request))
def lang_pick(request): return render(request, 'phrasebook/changelanguage.html', context=get_sidebar_args(request, {}))