def vote_up(request): """Increment the 'up_votes' count for a link. POST type request. :param : id of a link :type : int :returns: dict :example: {} """ try: link_id = int(request.POST['id']) except ValueError: error_message = "Value of 'id' must be an integer." raise Exception(error_message) try: link = Link.objects.get(id=link_id) except Link.DoesNotExist: error_message = "A Link with id %s does not exist." % link_id raise Exception(error_message) req_lim_key = 'vote_up.%s' % link_id check_req_lim(request, req_lim_key) link.incr_up_votes() link.save() update_req_lim(request, req_lim_key) return {'up_votes': link.up_votes}
def add_link(request): """Create a new link in a given section of a documentation page. If the section and page don't exist create them as well. POST type request. :param page_title: Title of documentation's page :type page_title: str :param url: URL of documentation's page :type url: str :param section_id: HTML ID of section :type section_id: str :param section_title: Title of section :type section_title: str :param link_url: URL of new link :type link_url: str :returns: dict :example: {'id': 7, 'url': 'http://one.to.rule_them.al/l, 'title': 'Knights of Foo Bar, 'is_relevant': True,} """ req_lim_key = 'add_link' check_req_lim(request, req_lim_key) form = AddLinkForm(request.POST) if form.is_valid(): try: # Fetch & parse the linked page link_title = get_page_title(form.cleaned_data['link_url']) except Exception, e: error_message = 'Title could not be fetched for this url: %s' % form.cleaned_data['link_url'] raise Exception(error_message) page, created = Page.objects.get_or_create(url=form.cleaned_data['url'], defaults={'meta_title': form.cleaned_data['page_title']}) section, created = Section.objects.get_or_create(html_id=form.cleaned_data['section_id'], page=page, defaults={'html_title': form.cleaned_data['section_title']}) link, created = Link.objects.get_or_create(url=form.cleaned_data['link_url'], section=section, defaults={'title': link_title}) response = { 'id': link.id, 'url': form.cleaned_data['link_url'], 'title': link_title, 'is_relevant': True, 'up_votes': link.up_votes, } update_req_lim(request, req_lim_key) return response
def set_relevant(request): """Set the 'is_relevant' flag of a link. POST type request. :param : id of a link :type : int :param : is_relevant :values : 0 or 1 :type : int :returns: dict :example: {} """ try: link_id = int(request.POST['id']) except ValueError: error_message = "Value of 'id' must be an integer." raise Exception(error_message) req_lim_key = 'set_relevant.%s' % link_id check_req_lim(request, req_lim_key) try: is_relevant = int(request.POST['is_relevant']) except ValueError: error_message = "Value of 'is_relevant' must be an integer." raise Exception(error_message) if not is_relevant in [0, 1]: error_message = "Value of 'is_relevant' must be 1 or 0." raise Exception(error_message) try: link = Link.objects.get(id=link_id) except Link.DoesNotExist: error_message = "A link with id %s does not exist." % link_id raise Exception(error_message) link.set_relevant(bool(is_relevant)) link.save() update_req_lim(request, req_lim_key) return {}