def get_topic(request, topic_name): selected_tags = topic_name.strip("/") if selected_tags: selected_tags = selected_tags.split("/") else: selected_tags = [] if request.POST: if request.POST.get("link_tag"): return save_tag(request, topic_name) if request.POST.get("delete_tag"): return delete_tag(request, selected_tags) else: return save_link(request, topic_name) template = loader.get_template('topics/show_topic.html') links = Link.get_all_links(get_current_site(request), selected_tags) links = paginate_links(links, request.GET.get('page')) top_tags = list(Tag.get_top_tag_list(get_current_site(request), selected_tags)) if len(links) < 6: top_tags = top_tags[:-1] context = RequestContext(request, { 'links': links, 'top_tags_first': top_tags[:1], 'top_tags_rest': top_tags[1:], 'selected_tags': selected_tags }) return HttpResponse(template.render(context))
def add_link(request, topic_slug): topic = Topic.objects.get(slug=topic_slug) def render_page(): params = {'topic_name': topic.name} return render(request, 'topics/add_link.html', params) if request.method == 'POST': url = request.POST.get('url') description = request.POST.get('description') if not Link.objects.filter(url=url).exists(): link = Link(url=url, description=description) link.topic = topic link.save() return HttpResponseRedirect(reverse('topics.views.view_topic', args=(topic.slug,))) else: return render_page()
def add_link(request, topic_slug): topic = Topic.objects.get(slug=topic_slug) def render_page(): params = {'topic_name': topic.name} return render(request, 'topics/add_link.html', params) if request.method == 'POST': url = request.POST.get('url') description = request.POST.get('description') if not Link.objects.filter(url=url).exists(): link = Link(url=url, description=description) link.topic = topic link.save() return HttpResponseRedirect( reverse('topics.views.view_topic', args=(topic.slug, ))) else: return render_page()
def tag_topic(request): if request.POST: if request.POST.get("link_tag"): save_tag(request, "") return redirect("topics:tag_topic") template = loader.get_template('topics/tag_link.html') context = RequestContext(request, { 'link': Link.get_no_tag_link(current_site=get_current_site(request)), 'rubric': rubric }) return HttpResponse(template.render(context))
def _bing_api_call(search_term): auth = str( base64.b64encode( b'GADjTrr1YGG7uFx58yNvkuJNUTEN7s6++SnOiOnwaYM:GADjTrr1YGG7uFx58yNvkuJNUTEN7s6++SnOiOnwaYM' ), 'utf-8') header = {'Authorization': 'Basic ' + auth} url = 'https://api.datamarket.azure.com/Bing/Search/Web?$format=json' data = {'Query': "'" + search_term + "'"} data = urllib.parse.urlencode(data) request = urllib.request.Request(url + '&' + data, None, header) request_open = urllib.request.urlopen(request) response = request_open.read() request_open.close() parsed = json.loads(response.decode('utf-8')) links = [] for entry in parsed['d']['results']: link = Link(url=entry['Url'], description=entry['Description']) links.append(link) return links[:10]
def save_tag(request, topic_name): tag_list_raw = request.POST.get("tag_text") link_title = request.POST.get("link_title") link_delete = request.POST.get("link_delete") == "delete" link_id = int(request.POST.get("link_tag")) link_type = request.POST.get("type") link_difficulty = request.POST.get("difficulty") link_quality = request.POST.get("quality") tag_list = tag_list_raw.lstrip("#").replace("#", ",").split(",") Link.save_ratings(link_id, link_type, link_difficulty, link_quality) if link_delete: Link.delete_link(link_id) else: if link_title: Link.rename_link(link_id, link_title) Tag.save_tags(link_id, tag_list, request.user) return redirect("topics:get_topic", topic_name)
def save_link(request, topic_name): try: Link.save_link(request.POST.get('url'), request.user, get_current_site(request)) except IntegrityError: messages.error(request, 'Link already posted.') return redirect("topics:get_topic", topic_name)