Esempio n. 1
0
def rate(request, ratee, ratee_id):
  """
  Rate either a ``Link`` or ``LinkLibrary``.

  Parameters:
    ``ratee`` - a string, whose value must be either 'link' or 'library'. The value of ``ratee`` is
                guaranteed by the app's URL conf file.
    ``ratee_id`` - the ID of the ``Link`` or ``LinkLibrary`` to be rated

  Returns:
    a JSON object. For GET requests, the JSON object represent the ``Link`` or ``LinkLibrary`` and the
    related ``Rating``, if one already exists. For POST requests, the JSON object is simply the new
    ``Rating`` instance resulting for updating the database.
  """
  user = get_object_or_404(CoreUser, username=request.user.username)
  link = get_object_or_404(Link, pk=ratee_id) if ratee == 'link' else None
  link_library = get_object_or_404(LinkLibrary, pk=ratee_id) if ratee == 'library' else None

  # check to see if a RatingFK already exists for this (CoreUser, (Link|LinkLibrary)) combo. If the combo already exists:
  #   1. and this is a GET, pass the Rating to the template to be rendered so the user can update the Rating
  #   2. and this is a POST, update the Rating
  try:
    rating_fk = RatingFK.objects.get(user=user, link=link, link_library=link_library)
  except RatingFK.DoesNotExist:
    rating_fk = None

  if rating_fk:
    try:
      rating = Rating.objects.get(rating_fk=rating_fk)
    except Rating.DoesNotExist:
      if not rating: raise IntegrityError('A RatingFK %s exists, but is not associated with a Rating' % rating_fk)

  if request.method == 'GET':
    if rating_fk:
      context = {'rating': utils.django_to_dict(rating), 'link': utils.django_to_dict(link),
                 'link_library': utils.django_to_dict(link_library)}
    else:
      context = {'link': utils.django_to_dict(link), 'link_library': utils.django_to_dict(link_library)}

    return HttpResponse(json.dumps(context))
  else:
    if rating_fk:
      rating.score, rating.comment = (request.POST['score'], request.POST['comment'].strip())
      rating.save()
    else:
      if ratee == 'link': rating_fk = RatingFK.objects.create(user=user, link=link)
      elif ratee == 'library': rating_fk = RatingFK.objects.create(user=user, link_library=link_library)

      rating = Rating.objects.create(rating_fk=rating_fk, score=request.POST['score'], comment=request.POST['comment'].strip())

    return HttpResponse(json.dumps(utils.django_to_dict(rating)))
Esempio n. 2
0
def rate(request, ratee, ratee_id):
  """
  Rate either a ``Link`` or ``LinkLibrary``.

  Parameters:
    ``ratee`` - a string, whose value must be either 'link' or 'library'. The value of ``ratee`` is
                guaranteed by the app's URL conf file.
    ``ratee_id`` - the ID of the ``Link`` or ``LinkLibrary`` to be rated

  Returns:
    a JSON object. For GET requests, the JSON object represent the ``Link`` or ``LinkLibrary`` and the
    related ``Rating``, if one already exists. For POST requests, the JSON object is simply the new
    ``Rating`` instance resulting for updating the database.
  """
  user = get_object_or_404(CoreUser, username=request.user.username)
  link = get_object_or_404(Link, pk=ratee_id) if ratee == 'link' else None
  link_library = get_object_or_404(LinkLibrary, pk=ratee_id) if ratee == 'library' else None

  # check to see if a RatingFK already exists for this (CoreUser, (Link|LinkLibrary)) combo. If the combo already exists:
  #   1. and this is a GET, pass the Rating to the template to be rendered so the user can update the Rating
  #   2. and this is a POST, update the Rating
  try:
    rating_fk = RatingFK.objects.get(user=user, link=link, link_library=link_library)
  except RatingFK.DoesNotExist:
    rating_fk = None

  if rating_fk:
    try:
      rating = Rating.objects.get(rating_fk=rating_fk)
    except Rating.DoesNotExist:
      if not rating: raise IntegrityError('A RatingFK %s exists, but is not associated with a Rating' % rating_fk)

  if request.method == 'GET':
    if rating_fk:
      context = {'rating': utils.django_to_dict(rating), 'link': utils.django_to_dict(link),
                 'link_library': utils.django_to_dict(link_library)}
    else:
      context = {'link': utils.django_to_dict(link), 'link_library': utils.django_to_dict(link_library)}

    return HttpResponse(json.dumps(context))
  else:
    if rating_fk:
      rating.score, rating.comment = (request.POST['score'], request.POST['comment'].strip())
      rating.save()
    else:
      if ratee == 'link': rating_fk = RatingFK.objects.create(user=user, link=link)
      elif ratee == 'library': rating_fk = RatingFK.objects.create(user=user, link_library=link_library)

      rating = Rating.objects.create(rating_fk=rating_fk, score=request.POST['score'], comment=request.POST['comment'].strip())

    return HttpResponse(json.dumps(utils.django_to_dict(rating)))
Esempio n. 3
0
def get_link(request, linkId):
  if linkId and linkId.isdigit():
    link = Link.objects.get(pk=int(linkId))
    if link:
      return HttpResponse(json.dumps(utils.django_to_dict(link)))
  return HttpResponseNotFound('Link %s doesn\'t exist' % linkId)
Esempio n. 4
0
def get_link(request, linkId):
  if linkId and linkId.isdigit():
    link = Link.objects.get(pk=int(linkId))
    if link:
      return HttpResponse(json.dumps(utils.django_to_dict(link)))
  return HttpResponseNotFound('Link %s doesn\'t exist' % linkId)