def login_register_view(request, register=False, modal=False): csrf_tk = {} csrf_tk.update(csrf(request)) login_error = False if request.method == 'POST' and not register: login_form = LoginForm(request.POST) if login_form.is_valid(): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: login(request, user) request.session['get_token'] = generate_token() if not 'remember' in request.POST: request.session.set_expiry(0) if 'next' in request.POST: return redirect(request.POST['next'] or '/') else: return redirect('/') else: login_error = 'Bad user/password' else: if request.POST['username'] and request.POST['password']: login_error = 'Form invalid' else: login_error = 'Please enter username and password' login_form = LoginForm() register_form = RegisterForm() if request.method == 'POST' and register: register_form = RegisterForm(request.POST) if register_form.is_valid(): data = register_form.data user = User.objects.create_user( data['username'], data['email'], data['password']) profile = Profile(user=user) profile.save() user.backend = 'django.contrib.auth.backends.ModelBackend' login(request, user) return redirect('/user/register/success') csrf_tk['register_form'] = register_form csrf_tk['login_error'] = login_error csrf_tk['login_form'] = login_form csrf_tk['register'] = register if 'next' in request.GET: csrf_tk['next'] = request.GET.get('next') if not modal: return render_template('user/login_register.html', csrf_tk) else: return render_template('user/login_register_modal.html', csrf_tk)
def search(request, tags=None, operand=None, excludes=None, lang_filter=[1]): tags_csv = cache.get("tags_csv") if tags_csv is None: from taggit.models import Tag tags_csv = '","'.join(Tag.objects.all().values_list("name", flat=True)) tags_csv = '"%s"' % tags_csv cache.set("tags_csv", tags_csv, 60 * 15) if operand is not None: return render_template("pages/search.html", {"tags": tags_csv, "stags": tags, "sop": operand, "sex": excludes}) else: return render_template("pages/search.html", {"tags": tags_csv})
def register_view(request): csrf_tk = {} csrf_tk.update(csrf(request)) login_error = False login_form = LoginForm() register_form = RegisterForm() if request.method == "POST": register_form = RegisterForm(request.POST) if register_form.is_valid(): data = register_form.data user = User.objects.create_user(data["username"], data["email"], data["password"]) profile = Profile(user=user) profile.save() user.backend = "django.contrib.auth.backends.ModelBackend" login(request, user) return redirect("/user/register/success") csrf_tk["register_form"] = register_form csrf_tk["login_error"] = login_error csrf_tk["login_form"] = login_form if "next" in request.GET: csrf_tk["next"] = request.GET.get("next") return render_template("user/register.html", csrf_tk)
def links_page(request): from resrc.vote.models import Vote lang_filter = [1] if request.user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=request.user) lang_filter = profile.languages.all().order_by('name').values_list( 'pk', flat=True) latest = Vote.objects.latest_links(limit=25, days=7, lang_filter=lang_filter) hottest = Vote.objects.hottest_links(limit=15, days=10, lang_filter=lang_filter) most_voted = Vote.objects.hottest_links(limit=10, days=30, lang_filter=lang_filter) if request.user.is_authenticated(): user_upvoted = Vote.objects.my_upvoted_links(request.user) user_upvoted_pk = [x['link__pk'] for x in user_upvoted] else: user_upvoted = [] user_upvoted_pk = [] return render_template( 'links/links.html', { 'latest': latest, 'hottest': hottest, 'most_voted': most_voted, 'upvoted': user_upvoted, 'upvoted_pk': user_upvoted_pk, })
def my_lists(request, user_name): upvoted_count = 0 from django.db.models import Count if request.user.username == user_name: user = request.user only_public = False owner = True from resrc.vote.models import Vote upvoted_count = len( list(Vote.objects.exclude(link=None).filter(user=user).values_list('pk'))) else: user = get_object_or_404(User, username=user_name) only_public = True owner = False lists = List.objects.user_lists(user, only_public=only_public) \ .annotate(c=Count('links')) return render_template('lists/lists_list.html', { 'lists': lists, 'owner': owner, 'username': user_name, 'upvoted_count': upvoted_count, })
def new(request): from resrc.vote.models import Vote from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger lang_filter = [1] if request.user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=request.user) lang_filter = profile.languages.all().order_by('name').values_list( 'pk', flat=True) links = Vote.objects.latest_links(limit=None, days=60, lang_filter=lang_filter) paginator = Paginator(links, 15) page = request.GET.get('page') try: links = paginator.page(page) except PageNotAnInteger: links = paginator.page(1) except EmptyPage: links = paginator.page(paginator.num_pages) user = request.user if user.is_authenticated(): user_upvoted_links = Vote.objects.my_upvoted_links(user) user_upvoted_links = [x['link__pk'] for x in user_upvoted_links] else: user_upvoted_links = [] return render_template('pages/new.html', { 'links': links, 'upvoted_links': user_upvoted_links, })
def links_page(request): from resrc.vote.models import Vote lang_filter = [1] if request.user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=request.user) lang_filter = profile.languages.all().order_by('name').values_list('pk', flat=True) latest = Vote.objects.latest_links(limit=25, days=7, lang_filter=lang_filter) hottest = Vote.objects.hottest_links(limit=15, days=10, lang_filter=lang_filter) most_voted = Vote.objects.hottest_links(limit=10, days=30, lang_filter=lang_filter) if request.user.is_authenticated(): user_upvoted = Vote.objects.my_upvoted_links(request.user) user_upvoted_pk = [x['link__pk'] for x in user_upvoted] else: user_upvoted = [] user_upvoted_pk = [] return render_template('links/links.html', { 'latest': latest, 'hottest': hottest, 'most_voted': most_voted, 'upvoted': user_upvoted, 'upvoted_pk': user_upvoted_pk, })
def register_view(request): csrf_tk = {} csrf_tk.update(csrf(request)) login_error = False register_form = RegisterForm() if request.method == 'POST': register_form = RegisterForm(request.POST) if register_form.is_valid(): data = register_form.data user = User.objects.create_user( data['username'], data['email'], data['password']) profile = Profile(user=user) profile.save() user.backend = 'django.contrib.auth.backends.ModelBackend' login(request, user) return redirect('/user/register/success') csrf_tk['register_form'] = register_form csrf_tk['login_error'] = login_error if 'next' in request.GET: csrf_tk['next'] = request.GET.get('next') return render_template('user/register.html', csrf_tk)
def lists_page(request): from resrc.vote.models import Vote lang_filter = [1] if request.user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=request.user) lang_filter = profile.languages.all().order_by("name").values_list("pk", flat=True) latest = List.objects.latest(limit=25, lang_filter=lang_filter) most_viewed = List.objects.most_viewed(limit=25, lang_filter=lang_filter) hottest = Vote.objects.hottest_lists(limit=25, days=7, lang_filter=lang_filter) most_voted = Vote.objects.hottest_lists(limit=25, days=30, lang_filter=lang_filter) if request.user.is_authenticated(): user_upvoted = Vote.objects.my_upvoted_lists(request.user) user_upvoted_pk = [x["alist__pk"] for x in user_upvoted] else: user_upvoted = [] user_upvoted_pk = [] return render_template( "lists/lists.html", { "latest": latest, "most_viewed": most_viewed, "hottest": hottest, "most_voted": most_voted, "upvoted": user_upvoted, "upvoted_pk": user_upvoted_pk, }, )
def my_lists(request, user_name): upvoted_count = 0 from django.db.models import Count if request.user.username == user_name: user = request.user only_public = False owner = True from resrc.vote.models import Vote upvoted_count = len( list( Vote.objects.exclude(link=None).filter( user=user).values_list('pk'))) else: user = get_object_or_404(User, username=user_name) only_public = True owner = False lists = List.objects.user_lists(user, only_public=only_public) \ .annotate(c=Count('links')) return render_template( 'lists/lists_list.html', { 'lists': lists, 'owner': owner, 'username': user_name, 'upvoted_count': upvoted_count, })
def new(request): from resrc.vote.models import Vote from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger lang_filter = [1] if request.user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=request.user) lang_filter = profile.languages.all().order_by("name").values_list("pk", flat=True) links = Vote.objects.latest_links(limit=None, days=60, lang_filter=lang_filter) paginator = Paginator(links, 15) page = request.GET.get("page") try: links = paginator.page(page) except PageNotAnInteger: links = paginator.page(1) except EmptyPage: links = paginator.page(paginator.num_pages) user = request.user if user.is_authenticated(): user_upvoted_links = Vote.objects.my_upvoted_links(user) user_upvoted_links = [x["link__pk"] for x in user_upvoted_links] else: user_upvoted_links = [] return render_template("pages/new.html", {"links": links, "upvoted_links": user_upvoted_links})
def search(request, tags=None, operand=None, excludes=None, lang_filter=[1]): from taggit.models import Tag all_tags = Tag.objects.all().values_list('name', flat=True) tags_json = json.dumps([{'tag': tag} for tag in all_tags]) if operand is not None: return render_template('pages/search.html', { 'stags': tags, 'sop': operand, 'sex': excludes, 'tags_json': tags_json, }) else: return render_template('pages/search.html', { 'tags_json': tags_json, })
def register_view(request): csrf_tk = {} csrf_tk.update(csrf(request)) login_error = False login_form = LoginForm() register_form = RegisterForm() if request.method == 'POST': register_form = RegisterForm(request.POST) if register_form.is_valid(): data = register_form.data user = User.objects.create_user(data['username'], data['email'], data['password']) profile = Profile(user=user) profile.save() user.backend = 'django.contrib.auth.backends.ModelBackend' login(request, user) return redirect('/user/register/success') csrf_tk['register_form'] = register_form csrf_tk['login_error'] = login_error csrf_tk['login_form'] = login_form if 'next' in request.GET: csrf_tk['next'] = request.GET.get('next') return render_template('user/register.html', csrf_tk)
def upvoted_list(request): from resrc.vote.models import Vote upvoted_links = Vote.objects.my_upvoted_links(request.user) upvoted_lists = Vote.objects.my_upvoted_lists(request.user) return render_template("links/upvoted_list.html", {"upvoted_links": upvoted_links, "upvoted_lists": upvoted_lists})
def home(request): from resrc.vote.models import Vote hot_lk_cache = 'hot_lk_15_10' lang_filter = [1] user = request.user if user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=user) lang_filter = profile.languages.all().order_by('name').values_list('pk', flat=True) hot_lk_cache = 'hot_lk_10_7_%s' % '_'.join(map(str, lang_filter)) user_upvoted_links = Vote.objects.my_upvoted_links(user) user_upvoted_links = [x['link__pk'] for x in user_upvoted_links] else: user_upvoted_links = [] hottest_links = cache.get(hot_lk_cache) if hottest_links is None: hottest_links = Vote.objects.hottest_links(limit=15, days=10, lang_filter=lang_filter) cache.set(hot_lk_cache, list(hottest_links), 60*2) tags_csv = cache.get('tags_csv') if tags_csv is None: from taggit.models import Tag tags_csv = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags_csv = '"%s"' % tags_csv cache.set('tags_csv', tags_csv, 60*15) return render_template('home.html', { 'hottest_links': hottest_links, 'upvoted_links': user_upvoted_links, 'csvtags': tags_csv, })
def settings_profile(request): profile = Profile.objects.get(user=request.user) if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): profile.about = form.data['about'] request.user.email = form.data['email'] languages = request.POST.getlist('languages') if 'show_email' in form.data: profile.show_email = form.data['show_email'] else: profile.show_email = False # Save the profile # and redirect the user to the configuration space # with message indicate the state of the operation try: profile.languages.clear() from resrc.language.models import Language for lang in languages: profile.languages.add(Language.objects.get(pk=lang)) profile.save() request.user.save() except: messages.error(request, 'Error') return redirect(reverse('user-settings')) messages.success( request, 'Update successful.') return redirect(reverse('user-settings')) else: return render_template('user/settings_profile.html', { 'usr': request.user, 'form': form, }) else: form = ProfileForm(initial={ 'about': profile.about, 'email': request.user.email, 'languages': profile.languages.all(), 'show_email': profile.show_email } ) return render_template('user/settings_profile.html', { 'usr': request.user, 'form': form })
def settings_account(request): if request.method == "POST": form = ChangePasswordForm(request.user, request.POST) if form.is_valid(): try: request.user.set_password(form.data["password_new"]) request.user.save() messages.success(request, "Password updated.") return redirect(reverse("user-settings")) except: messages.error(request, "Error while updating your password.") return redirect(reverse("user-settings")) else: return render_template("user/settings_account.html", {"usr": request.user, "form": form}) else: form = ChangePasswordForm(request.user, request.POST) return render_template("user/settings_account.html", {"usr": request.user, "form": form})
def ajax_own_lists(request, link_pk): if not request.user.is_authenticated(): raise PermissionDenied all_lists = List.objects.personal_lists(request.user) titles = list(List.objects.my_list_titles(request.user, link_pk).values_list("title", flat=True)) return render_template("lists/ajax_own_lists.html", {"lists": all_lists, "titles": titles, "link_pk": link_pk})
def home(request): from resrc.vote.models import Vote hot_lk_cache = 'hot_lk_5_7' hot_ls_cache = 'hot_ls_14_7' lang_filter = [1] user = request.user if user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=user) lang_filter = profile.languages.all().order_by('name').values_list('pk', flat=True) hot_lk_cache = 'hot_lk_10_7_%s' % '_'.join(map(str, lang_filter)) hot_ls_cache = 'hot_ls_10_14_%s' % '_'.join(map(str, lang_filter)) user_upvoted_lists = Vote.objects.my_upvoted_lists(user) user_upvoted_lists = [x['alist__pk'] for x in user_upvoted_lists] user_upvoted_links = Vote.objects.my_upvoted_links(user) user_upvoted_links = [x['link__pk'] for x in user_upvoted_links] else: user_upvoted_lists = [] user_upvoted_links = [] hottest_links = cache.get(hot_lk_cache) if hottest_links is None: hottest_links = Vote.objects.hottest_links(limit=5, days=7, lang_filter=lang_filter) cache.set(hot_lk_cache, list(hottest_links), 60*2) latest_links = Vote.objects.latest_links(limit=5, days=7, lang_filter=lang_filter) hottest_lists = cache.get(hot_ls_cache) if hottest_lists is None: hottest_lists = Vote.objects.hottest_lists(limit=5, days=14, lang_filter=lang_filter) cache.set(hot_ls_cache, list(hottest_lists), 60*2+2) tags = cache.get('tags_all') if tags is None: from taggit.models import Tag from django.db.models import Count tags = Tag.objects.select_related('links') \ .annotate(c=Count('link')).order_by('-c') \ .all() cache.set('tags_all', list(tags), 60*15) tags_csv = cache.get('tags_csv') if tags_csv is None: from taggit.models import Tag tags_csv = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags_csv = '"%s"' % tags_csv cache.set('tags_csv', tags_csv, 60*15) return render_template('home.html', { 'latest_links': latest_links, 'hottest_links': hottest_links, 'tags': tags[:25], 'csvtags': tags_csv, 'hottest_lists': hottest_lists, 'upvoted_links': user_upvoted_links, 'upvoted_lists': user_upvoted_lists, })
def upvoted_list(request): from resrc.vote.models import Vote upvoted_links = Vote.objects.my_upvoted_links(request.user) upvoted_lists = Vote.objects.my_upvoted_lists(request.user) return render_template('links/upvoted_list.html', { 'upvoted_links': upvoted_links, 'upvoted_lists': upvoted_lists, })
def index(request): from django.db.models import Count tags = Tag.objects.select_related('links') \ .annotate(c=Count('link')).order_by('-c', 'name') \ .exclude(name=None) \ .all() tags = list(tags) return render_template('tags/show_index.html', {'tags': tags})
def search(request, tags=None, operand=None, excludes=None, lang_filter=[1]): tags_csv = cache.get('tags_csv') if tags_csv is None: from taggit.models import Tag tags_csv = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags_csv = '"%s"' % tags_csv cache.set('tags_csv', tags_csv, 60*15) if operand is not None: return render_template('pages/search.html', { 'tags': tags_csv, 'stags': tags, 'sop': operand, 'sex': excludes, }) else: return render_template('pages/search.html', { 'tags': tags_csv, })
def search(request, tags=None, operand=None, excludes=None, lang_filter=[1]): tags_csv = cache.get('tags_csv') if tags_csv is None: from taggit.models import Tag tags_csv = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags_csv = '"%s"' % tags_csv cache.set('tags_csv', tags_csv, 60 * 15) if operand is not None: return render_template('pages/search.html', { 'tags': tags_csv, 'stags': tags, 'sop': operand, 'sex': excludes, }) else: return render_template('pages/search.html', { 'tags': tags_csv, })
def login_view(request, modal=False): csrf_tk = {} csrf_tk.update(csrf(request)) login_error = False if request.method == 'POST': login_form = LoginForm(request.POST) if login_form.is_valid(): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: login(request, user) request.session['get_token'] = generate_token() if not 'remember' in request.POST: request.session.set_expiry(0) if 'next' in request.POST: return redirect(request.POST['next'] or '/') else: return redirect('/') else: login_error = 'Bad user/password' else: if request.POST['username'] and request.POST['password']: login_error = 'Form invalid' else: login_error = 'Please enter username and password' next = '/' if 'next' in request.GET: next = request.GET['next'] login_form = LoginForm(({'next': next}),) register_form = RegisterForm() csrf_tk['register_form'] = register_form csrf_tk['login_error'] = login_error csrf_tk['login_form'] = login_form if 'next' in request.GET: csrf_tk['next'] = request.GET.get('next') if not modal: return render_template('user/login.html', csrf_tk) else: return render_template('user/login_register_modal.html', csrf_tk)
def login_view(request, modal=False): csrf_tk = {} csrf_tk.update(csrf(request)) login_error = False if request.method == 'POST': login_form = LoginForm(request.POST) if login_form.is_valid(): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: login(request, user) request.session['get_token'] = generate_token() if not 'remember' in request.POST: request.session.set_expiry(0) if 'next' in request.POST: return redirect(request.POST['next'] or '/') else: return redirect('/') else: login_error = 'Bad user/password' else: if request.POST['username'] and request.POST['password']: login_error = 'Form invalid' else: login_error = 'Please enter username and password' next = '/' if 'next' in request.GET: next = request.GET['next'] login_form = LoginForm(({'next': next}), ) register_form = RegisterForm() csrf_tk['register_form'] = register_form csrf_tk['login_error'] = login_error csrf_tk['login_form'] = login_form if 'next' in request.GET: csrf_tk['next'] = request.GET.get('next') if not modal: return render_template('user/login.html', csrf_tk) else: return render_template('user/login_register_modal.html', csrf_tk)
def details(request, user_name): """Displays details about a profile""" usr = get_object_or_404(User, username=user_name) try: profile = Profile.objects.get(user=usr) except Profile.DoesNotExist: raise Http404 return render_template("user/profile.html", {"usr": usr, "profile": profile})
def single(request, tag_slug): tag = get_object_or_404(Tag, slug=tag_slug) links = Link.objects.filter(tags=tag) return render_template('tags/show_single.html', { 'tag': tag, 'links': links, 'request': request, })
def home(request): from resrc.vote.models import Vote hot_lk_cache = 'hot_lk_5_7' hot_ls_cache = 'hot_ls_14_7' lang_filter = [1] user = request.user if user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=user) lang_filter = profile.languages.all().order_by('name').values_list('pk', flat=True) hot_lk_cache = 'hot_lk_10_30_%s' % '_'.join(map(str, lang_filter)) hot_ls_cache = 'hot_ls_10_30_%s' % '_'.join(map(str, lang_filter)) user_upvoted_lists = Vote.objects.my_upvoted_lists(user) user_upvoted_lists = [x['alist__pk'] for x in user_upvoted_lists] user_upvoted_links = Vote.objects.my_upvoted_links(user) user_upvoted_links = [x['link__pk'] for x in user_upvoted_links] else: user_upvoted_lists = [] user_upvoted_links = [] hottest_links = cache.get(hot_lk_cache) if hottest_links is None: hottest_links = Vote.objects.hottest_links(limit=5, days=30, lang_filter=lang_filter) cache.set(hot_lk_cache, list(hottest_links), 60*2) latest_links = Vote.objects.latest_links(limit=5, days=7, lang_filter=lang_filter) hottest_lists = cache.get(hot_ls_cache) if hottest_lists is None: hottest_lists = Vote.objects.hottest_lists(limit=5, days=30, lang_filter=lang_filter) cache.set(hot_ls_cache, list(hottest_lists), 60*2+2) tags = cache.get('tags_all') if tags is None: from taggit.models import Tag from django.db.models import Count tags = Tag.objects.select_related('links') \ .annotate(c=Count('link')).order_by('-c') \ .all() cache.set('tags_all', list(tags), 60*15) from taggit.models import Tag all_tags = Tag.objects.all().values_list('name', flat=True) tags_json = json.dumps([{'tag': tag} for tag in all_tags]) return render_template('home.html', { 'latest_links': latest_links, 'hottest_links': hottest_links, 'tags': tags[:22], 'hottest_lists': hottest_lists, 'upvoted_links': user_upvoted_links, 'upvoted_lists': user_upvoted_lists, 'tags_json': tags_json, })
def settings_profile(request): profile = Profile.objects.get(user=request.user) if request.method == "POST": form = ProfileForm(request.POST) if form.is_valid(): profile.about = form.data["about"] request.user.email = form.data["email"] languages = request.POST.getlist("languages") if "show_email" in form.data: profile.show_email = form.data["show_email"] else: profile.show_email = False # Save the profile # and redirect the user to the configuration space # with message indicate the state of the operation try: profile.languages.clear() from resrc.language.models import Language for lang in languages: profile.languages.add(Language.objects.get(pk=lang)) profile.save() request.user.save() except: messages.error(request, "Error") return redirect(reverse("user-settings")) messages.success(request, "Update successful.") return redirect(reverse("user-settings")) else: return render_template("user/settings_profile.html", {"usr": request.user, "form": form}) else: form = ProfileForm( initial={ "about": profile.about, "email": request.user.email, "languages": profile.languages.all(), "show_email": profile.show_email, } ) return render_template("user/settings_profile.html", {"usr": request.user, "form": form})
def index(request): from django.db.models import Count tags = Tag.objects.select_related('links') \ .annotate(c=Count('link')).order_by('-c', 'name') \ .exclude(name=None) \ .all() tags = list(tags) return render_template('tags/show_index.html', { 'tags' : tags })
def single(request, list_pk, list_slug=None): alist = get_object_or_404(List, pk=list_pk) alist.views = alist.views + 1 alist.save() if list_slug is None: return redirect(alist) # avoid https://twitter.com/this_smells_fishy/status/351749761935753216 if alist.slug != list_slug: raise Http404 if not alist.is_public and request.user != alist.owner: raise Http404 form = None tags_addlink = '' if request.user.is_authenticated(): from resrc.link.forms import NewLinkForm form = NewLinkForm(request.POST) from taggit.models import Tag tags_addlink = cache.get('tags_csv') if tags_addlink is None: from taggit.models import Tag tags_addlink = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags_addlink = '"%s"' % tags_addlink cache.set('tags_csv', tags_addlink, 60 * 15) # from tldr.tldr import TLDRClient # client = TLDRClient("victorfelder", "4vle5U5zqElu9xQrsoYC") # tldrs = client.searchBatch(list(alist.links.values_list('url', flat=True).all()[:100])) # for tl in tldrs['tldrs']: # tl['id'] = tl['_id'] # tldr_urls = [tl['originalUrl'] for tl in tldrs['tldrs']] from resrc.vote.models import Vote if request.user.is_authenticated(): voted = Vote.objects.filter(alist=alist.pk, user=request.user).exists() else: voted = False return render_template( 'lists/show_single.html', { 'form': form, 'list': alist, 'count': Vote.objects.votes_for_list(alist.pk), 'voted': voted, 'tags': alist.get_tags(), 'tags_addlink': tags_addlink, 'reading_list': 'Reading list', # 'tldrs': tldrs, # 'tldr_urls': tldr_urls, 'request': request })
def ake(request, link_pk, link_slug): if not request.user.is_staff: raise PermissionDenied link = cache.get('link_%s' % link_pk) if link is None: link = get_object_or_404(Link, pk=link_pk) cache.set('link_%s' % link_pk, link, 60 * 5) from resrc.utils.automatic_keyword_extraction import get_keywords_from_url keywords = get_keywords_from_url(link.url) return render_template('links/ake.html', {'keywords': keywords})
def login_view(request, modal=False): csrf_tk = {} csrf_tk.update(csrf(request)) login_error = False if request.method == "POST": login_form = LoginForm(request.POST) if login_form.is_valid(): username = request.POST["username"] password = request.POST["password"] user = authenticate(username=username, password=password) if user is not None: login(request, user) request.session["get_token"] = generate_token() if not "remember" in request.POST: request.session.set_expiry(0) if "next" in request.POST: return redirect(request.POST["next"] or "/") else: return redirect("/") else: login_error = "Bad user/password" else: if request.POST["username"] and request.POST["password"]: login_error = "Form invalid" else: login_error = "Please enter username and password" login_form = LoginForm() register_form = RegisterForm() csrf_tk["register_form"] = register_form csrf_tk["login_error"] = login_error csrf_tk["login_form"] = login_form if "next" in request.GET: csrf_tk["next"] = request.GET.get("next") if not modal: return render_template("user/login.html", csrf_tk) else: return render_template("user/login_register_modal.html", csrf_tk)
def single(request, list_pk, list_slug=None): alist = get_object_or_404(List, pk=list_pk) alist.views = alist.views + 1 alist.save() if list_slug is None: return redirect(alist) # avoid https://twitter.com/this_smells_fishy/status/351749761935753216 if alist.slug != list_slug: raise Http404 if not alist.is_public and request.user != alist.owner: raise Http404 form = None tags_addlink = '' if request.user.is_authenticated(): from resrc.link.forms import NewLinkForm form = NewLinkForm(request.POST) from taggit.models import Tag tags_addlink = cache.get('tags_csv') if tags_addlink is None: from taggit.models import Tag tags_addlink = '","'.join( Tag.objects.all().values_list('name', flat=True)) tags_addlink = '"%s"' % tags_addlink cache.set('tags_csv', tags_addlink, 60 * 15) # from tldr.tldr import TLDRClient # client = TLDRClient("victorfelder", "4vle5U5zqElu9xQrsoYC") # tldrs = client.searchBatch(list(alist.links.values_list('url', flat=True).all()[:100])) # for tl in tldrs['tldrs']: # tl['id'] = tl['_id'] # tldr_urls = [tl['originalUrl'] for tl in tldrs['tldrs']] from resrc.vote.models import Vote if request.user.is_authenticated(): voted = Vote.objects.filter(alist=alist.pk, user=request.user).exists() else: voted = False return render_template('lists/show_single.html', { 'form': form, 'list': alist, 'count': Vote.objects.votes_for_list(alist.pk), 'voted': voted, 'tags': alist.get_tags(), 'tags_addlink': tags_addlink, 'reading_list': 'Reading list', # 'tldrs': tldrs, # 'tldr_urls': tldr_urls, 'request': request })
def details(request, user_name): '''Displays details about a profile''' usr = get_object_or_404(User, username=user_name) try: profile = Profile.objects.get(user=usr) except SiteProfileNotAvailable: raise Http404 return render_template('user/profile.html', { 'usr': usr, 'profile': profile })
def revision(request): if not request.user.is_staff: raise PermissionDenied from resrc.link.models import RevisedLink revised = RevisedLink.objects.select_related('link').all() for rev in revised: rev.link.tags = ",".join(rev.link.tags.order_by('name').values_list('name', flat=True)) return render_template('pages/revision.html', { 'revised': revised, })
def ajax_own_lists(request, link_pk): if not request.user.is_authenticated(): raise Http404 all_lists = List.objects.personal_lists(request.user) titles = list(List.objects.my_list_titles(request.user, link_pk) .values_list('title', flat=True)) return render_template('lists/ajax_own_lists.html', { 'lists': all_lists, 'titles': titles, 'link_pk': link_pk })
def ajax_own_lists(request, link_pk): if not request.user.is_authenticated(): raise Http404 all_lists = List.objects.personal_lists(request.user) titles = list( List.objects.my_list_titles(request.user, link_pk).values_list('title', flat=True)) return render_template('lists/ajax_own_lists.html', { 'lists': all_lists, 'titles': titles, 'link_pk': link_pk })
def settings_account(request): if request.method == 'POST': form = ChangePasswordForm(request.user, request.POST) if form.is_valid(): try: request.user.set_password(form.data['password_new']) request.user.save() messages.success(request, 'Password updated.') return redirect(reverse('user-settings')) except: messages.error(request, 'Error while updating your password.') return redirect(reverse('user-settings')) else: return render_template('user/settings_account.html', { 'usr': request.user, 'form': form, }) else: form = ChangePasswordForm(request.user, request.POST) return render_template('user/settings_account.html', { 'usr': request.user, 'form': form, })
def ake(request, link_pk, link_slug): if not request.user.is_staff: raise PermissionDenied link = cache.get('link_%s' % link_pk) if link is None: link = get_object_or_404(Link, pk=link_pk) cache.set('link_%s' % link_pk, link, 60*5) from resrc.utils.automatic_keyword_extraction import get_keywords_from_url keywords = get_keywords_from_url(link.url) return render_template('links/ake.html', { 'keywords': keywords })
def revision(request): if not request.user.is_staff: from django.http import Http404 raise Http404 from resrc.link.models import RevisedLink revised = RevisedLink.objects.select_related('link').all() for rev in revised: rev.link.tags = ",".join(rev.link.tags.values_list('name', flat=True)) return render_template('pages/revision.html', { 'revised': revised, })
def revision(request): if not request.user.is_staff: from django.http import Http404 raise Http404 from resrc.link.models import RevisedLink revised = RevisedLink.objects.select_related("link").all() for rev in revised: rev.link.tags = ",".join(rev.link.tags.values_list("name", flat=True)) return render_template("pages/revision.html", {"revised": revised})
def single(request, list_pk, list_slug=None): alist = get_object_or_404(List, pk=list_pk) alist.views = alist.views + 1 alist.save() if list_slug is None: return redirect(alist) if alist.slug != list_slug: raise Http404 if not alist.is_public and request.user != alist.owner: raise PermissionDenied form = None tags_addlink = "" if request.user.is_authenticated(): from resrc.link.forms import NewLinkForm form = NewLinkForm(request.POST) from taggit.models import Tag tags_addlink = cache.get("tags_csv") if tags_addlink is None: from taggit.models import Tag tags_addlink = '","'.join(Tag.objects.all().values_list("name", flat=True)) tags_addlink = '"%s"' % tags_addlink cache.set("tags_csv", tags_addlink, 60 * 15) from resrc.vote.models import Vote if request.user.is_authenticated(): voted = Vote.objects.filter(alist=alist.pk, user=request.user).exists() else: voted = False return render_template( "lists/show_single.html", { "form": form, "list": alist, "count": Vote.objects.votes_for_list(alist.pk), "voted": voted, "tags": alist.get_tags(), "tags_addlink": tags_addlink, "reading_list": "Reading list", "request": request, }, )
def new_list(request): if request.method == 'POST': form = NewListForm(request.POST) if form.is_valid(): is_private = False if 'private' in form.data: is_private = form.data['private'] if form.data['url']: opener = urllib2.build_opener() opener.addheaders = [('Accept-Charset', 'utf-8')] url_response = opener.open(form.data['url']) mdcontent = url_response.read().decode('utf-8') else: mdcontent = form.data['mdcontent'] from resrc.language.models import Language try: lang = Language.objects.get(language=form.data['language']) except Language.DoesNotExist: lang = Language.objects.create(language=form.data['language']) from resrc.utils.templatetags.emarkdown import listmarkdown alist = List.objects.create( title=form.data['title'], description=form.data['description'], url=form.data['url'], md_content=mdcontent, html_content='', owner=request.user, is_public=not is_private, language=lang, ) alist.html_content = listmarkdown(mdcontent.strip(u'\ufeff'), alist) alist.save() return redirect(alist.get_absolute_url()) else: form = NewListForm() links = list(Link.objects.all()) return render_template('lists/new_list.html', { 'form': form, 'links': links })
def single(request, tag_slug): tag = get_object_or_404(Tag, slug=tag_slug) links = Link.objects.filter(tags=tag) upvoted_pk = [] if request.user.is_authenticated(): from resrc.vote.models import Vote upvoted_links = Vote.objects.my_upvoted_links(request.user) upvoted_pk = [x['link__pk'] for x in upvoted_links] return render_template('tags/show_single.html', { 'tag': tag, 'links': links, 'upvoted_pk': upvoted_pk, })
def single(request, list_pk, list_slug=None): alist = get_object_or_404(List, pk=list_pk) alist.views = alist.views + 1 alist.save() if list_slug is None: return redirect(alist) if alist.slug != list_slug: raise Http404 if not alist.is_public and request.user != alist.owner: raise PermissionDenied form = None tags_addlink = '' if request.user.is_authenticated(): from resrc.link.forms import NewLinkForm form = NewLinkForm(request.POST) from taggit.models import Tag tags_addlink = cache.get('tags_csv') if tags_addlink is None: from taggit.models import Tag tags_addlink = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags_addlink = '"%s"' % tags_addlink cache.set('tags_csv', tags_addlink, 60 * 15) from resrc.vote.models import Vote if request.user.is_authenticated(): voted = Vote.objects.filter(alist=alist.pk, user=request.user).exists() else: voted = False return render_template( 'lists/show_single.html', { 'form': form, 'list': alist, 'count': Vote.objects.votes_for_list(alist.pk), 'voted': voted, 'tags': alist.get_tags(), 'tags_addlink': tags_addlink, 'reading_list': 'Reading list', 'request': request })
def user_list(request): #profiles = Profile.objects.select_related('User').order_by('user__date_joined') users = User.objects.exclude(username='******').order_by('date_joined') return render_template('user/list.html', { 'users': list(users) })
def about(request): return render_template('pages/about.html', {})
def new_link(request, title=None, url=None): if title is not None and url is not None: url = url.replace('http:/', 'http://') url = url.replace('https:/', 'https://') form = NewLinkForm(initial={ 'title': title, 'url': url, }) tags = cache.get('tags_csv') if tags is None: from taggit.models import Tag tags = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags = '"%s"' % tags cache.set('tags_csv', tags, 60 * 15) return render_template('links/new_link_button.html', { 'form': form, 'tags': tags }) if request.method == 'POST': form = NewLinkForm(request.POST) if form.is_valid(): data = form.data link = Link() link.title = data['title'] link.url = data['url'] from resrc.language.models import Language try: lang = Language.objects.get(language=data['language']) except Language.DoesNotExist: lang = Language.objects.create(language=data['language']) link.language = lang link.level = data['level'] link.author = request.user if Link.objects.filter(url=data['url']).exists(): return redirect( Link.objects.get(url=data['url']).get_absolute_url()) link.save() list_tags = data['tags'].split(',') for tag in list_tags: link.tags.add(tag) cache.delete('tags_all') cache.delete('tags_csv') link.save() cache.set('link_%s' % link.pk, link, 60 * 5) if not 'ajax' in data: return redirect(link.get_absolute_url()) alist = get_object_or_404(List, pk=data['id']) # if alist.owner != request.user: # raise Http404 from resrc.list.models import ListLinks if not ListLinks.objects.filter(alist=alist, links=link).exists(): ListLinks.objects.create(alist=alist, links=link) from resrc.utils.templatetags.emarkdown import listmarkdown alist.html_content = listmarkdown( alist.md_content.strip(u'\ufeff'), alist) alist.save() data = json.dumps({'result': 'added'}) return HttpResponse(data, mimetype="application/javascript") else: if not 'ajax' in form.data: form = NewLinkForm() tags = cache.get('tags_csv') if tags is None: from taggit.models import Tag tags = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags = '"%s"' % tags cache.set('tags_csv', tags, 60 * 15) return render_template('links/new_link.html', { 'form': form, 'tags': tags }) else: data = json.dumps({'result': 'fail'}) return HttpResponse(data, mimetype="application/javascript") else: form = NewLinkForm() tags = cache.get('tags_csv') if tags is None: from taggit.models import Tag tags = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags = '"%s"' % tags cache.set('tags_csv', tags, 60 * 15) return render_template('links/new_link.html', {'form': form, 'tags': tags})
def my_links(request): links = Link.objects.filter(author=request.user).exclude(flagged=True) return render_template('links/my_links.html', { 'links': links, })
def edit_link(request, link_pk): link = cache.get('link_%s' % link_pk) if link is None: link = get_object_or_404(Link, pk=link_pk) cache.set('link_%s' % link_pk, link, 60 * 5) if request.user != link.author: raise Http404 if request.method == 'POST': form = EditLinkForm(link_pk, request.POST) if form.is_valid(): link.title = form.data['title'] from resrc.language.models import Language link.language = Language.objects.get( language=form.data['language']) link.level = form.data['level'] link.author = request.user has_tags = link.tags.all().values_list('name', flat=True) link.save() list_tags = form.data['tags'].split(',') for tag in list_tags: if tag not in has_tags: link.tags.add(tag) cache.delete('tags_all') cache.delete('tags_csv') for tag in has_tags: if tag not in list_tags: link.tags.remove(tag) link.save() cache.set('link_%s' % link_pk, link, 60 * 5) return redirect(link.get_absolute_url()) else: from taggit.models import Tag form = EditLinkForm( link_pk=link_pk, initial={ 'url': link.url, 'title': link.title, 'tags': ','.join([ t for t in Tag.objects.filter( link=link_pk).values_list('name', flat=True) ]), 'language': link.language.language, 'level': link.level }) tags = cache.get('tags_csv') if tags is None: from taggit.models import Tag tags = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags = '"%s"' % tags cache.set('tags_csv', tags, 60 * 15) return render_template('links/new_link.html', { 'edit': True, 'form': form, 'tags': tags }) else: from taggit.models import Tag form = EditLinkForm( link_pk=link_pk, initial={ 'url': link.url, 'title': link.title, 'tags': ','.join([ t for t in Tag.objects.filter( link=link_pk).values_list('name', flat=True) ]), 'language': link.language.language, 'level': link.level }) tags = cache.get('tags_csv') if tags is None: from taggit.models import Tag tags = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags = '"%s"' % tags cache.set('tags_csv', tags, 60 * 15) return render_template('links/new_link.html', { 'url': link.url, 'edit': True, 'form': form, 'tags': tags })
def single(request, link_pk, link_slug=None): from taggit.models import Tag link = cache.get('link_%s' % link_pk) if link is None: link = get_object_or_404(Link, pk=link_pk) cache.set('link_%s' % link_pk, link, 60 * 5) lang_filter = [1] if request.user.is_authenticated(): from resrc.userprofile.models import Profile profile = Profile.objects.get(user=request.user) lang_filter = profile.languages.all().order_by('name').values_list( 'pk', flat=True) titles = [] newlistform = None tags = None if link_slug is None: return redirect(link) if link.slug != link_slug: raise Http404 reviselinkform = '' if request.user.is_authenticated(): titles = list( List.objects.all_my_list_titles(request.user, link_pk).values_list('title', flat=True)) newlistform = NewListAjaxForm(link_pk) reviselinkform = SuggestEditForm( link_pk, initial={ 'url': link.url, 'title': link.title, 'tags': ','.join([ t for t in Tag.objects.filter( link=link_pk).values_list('name', flat=True) ]), 'language': link.language.language, 'level': link.level }) # for tag autocomplete tags = cache.get('tags_csv') if tags is None: tags = '","'.join(Tag.objects.all().values_list('name', flat=True)) tags = '"%s"' % tags cache.set('tags_csv', tags, 60 * 15) lists = List.objects.some_lists_from_link(link_pk, lang_filter) similars = cache.get('similar_link_%s' % link_pk) if similars is None or True: similars = list() link_tags = list(link.tags.all()) max_n = len(link_tags) if max_n > 5: max_n = 5 class Enough(Exception): pass try: # n as in n-gram for n in xrange(max_n, 1, -1): n_gram = [ link_tags[x:x + n] for x in xrange(len(link_tags) - n + 1) ] # iterate over all n_grams for this n for i in xrange(len(n_gram)): tag_tuple = n_gram[i] add_similars = Link.objects.filter(tags__name=link_tags[0].name)\ .exclude(flagged=True) for idx in xrange(1, n): add_similars = add_similars.filter( tags__name=tag_tuple[idx].name) add_similars = add_similars.exclude(pk=link.pk) # add them to similars if not already in, do it in the right order for tmp_link in add_similars: if tmp_link not in similars: similars.append(tmp_link) if len(similars) >= 10: similars = similars[:10] raise Enough except Enough: pass cache.set('similar_link_%s' % link_pk, similars, 60 * 60 * 2) tldr = cache.get('tldr_%s' % link_pk) if tldr is None: try: from tldr.tldr import TLDRClient client = TLDRClient("victorfelder", "4vle5U5zqElu9xQrsoYC") tldr = client.searchByUrl(link.url) except: tldr = False cache.set('tldr_%s' % link_pk, tldr, 60 * 60 * 24 * 8) from resrc.vote.models import Vote if request.user.is_authenticated(): voted = Vote.objects.filter(link=link.pk, user=request.user).exists() else: voted = False return render_template( 'links/show_single.html', { 'link': link, 'count': Vote.objects.votes_for_link(link.pk), 'voted': voted, 'request': request, 'titles': list(titles), 'newlistform': newlistform, 'similars': similars, 'tldr': tldr, 'lists': lists, 'reviselinkform': reviselinkform, 'tags': tags })