def delete(request, status_pk): status = get_object_or_404(Status, pk=status_pk) if status.user != request.user: return json_view(error=(1, "Permission denied.")) status.delete() return json_view()
def read(request, notification_pk): try: notification = Notification.objects.get(pk = notification_pk) except Notification.ObjectNotFound: return json_view(error=(1, "Wrong request.")) notification.is_unread = False notification.save() return json_view()
def follow(request, user_pk): user = get_object_or_404(User, pk=user_pk) if Follow.objects.filter(user=request.user, target=user).count() != 0: return json_view(error=(1, "You are following this user.")) Follow(user=request.user, target=user).save() url = reverse("kiqlist.users.views.friends") + "#followers" send_notification(user, "%s %s is now following you." % (request.user.first_name, request.user.last_name), url) return json_view()
def unfollow(request, user_pk): user = get_object_or_404(User, pk=user_pk) try: follow = Follow.objects.filter(user=request.user, target=user).get() except Follow.DoesNotExist: return json_view(error=(1, "You are not following this user.")) follow.delete() return json_view()
def delete(request): try: date_cutoff_delta = timedelta(days = NOTIFICATIONS_DAYS_CUTOFF) date_cutoff = datetime.now().replace(tzinfo=utc) - date_cutoff_delta notifications = Notification.objects.filter( user = request.user, created__lt = date_cutoff ) except Notification.ObjectNotFound: return json_view(error=(1, "Wrong request.")) notifications.delete() return json_view()
def report(request, id): goal = get_object_or_404(Goal, id) goal.reports += 1 if goal.reports <= 1: pass # send email return json_view(data=Goal.objects.filter(id=id))
def done_goals(request, user_pk=None): if user_pk: user = get_object_or_404(User, pk=user_pk) done_goals = Goal.objects.filter(user=user, is_done=True).order_by("-created") else: done_goals = Goal.objects.filter(is_done=True).order_by("-created")[:20] return json_view(data=done_goals)
def delete(request, goal_pk): goal = get_object_or_404(Goal, pk=goal_pk) if goal.user != request.user: raise PermissionDenied goal.delete() return json_view()
def following(request): following_users = [x.target for x in request.user.following.all()] goals = Goal.objects.filter(user__in=following_users) statuses = Status.objects.filter(user__in=following_users) data = sorted(chain(goals, statuses), key=attrgetter("created"), reverse=True) return json_view(data=data)
def like(request, goal_pk): goal = get_object_or_404(Goal, pk=goal_pk) try: goallike = GoalLike.objects.get(user=request.user, goal=goal) goallike.delete() except GoalLike.DoesNotExist: GoalLike(user=request.user, goal=goal).save() return json_view(goal.goallike_set.count())
def content_done(request, user_pk=None): if user_pk: user = get_object_or_404(User, pk=user_pk) done_goals = Goal.objects.filter(user=user, is_done=True) statuses = Status.objects.filter(user=user) else: done_goals = Goal.objects.filter(is_done=True)[:10] statuses = Status.objects.all()[:10] data = sorted(chain(done_goals, statuses), key=attrgetter("created"), reverse=True) return json_view(data=data)
def following_activity(request): following_users = [x.target for x in request.user.following.all()] activity = [] def get_user_dict(user): return { "pk": user.pk, "fields": { "first_name": user.first_name, "last_name": user.last_name } } follows = Follow.objects.filter(user__in=following_users).order_by("-created")[:5] for follow in follows: activity.append({ "type": "follow", "data": { "user": get_user_dict(follow.user), "target": get_user_dict(follow.target) }, "created": follow.created }) for user in following_users: last_goal_queryset = Goal.objects.filter(user=user).order_by("-created") if len(last_goal_queryset): last_goal = last_goal_queryset[0] goal_type = "done" if last_goal.is_done else "added" activity.append({ "type": goal_type, "data": { "user": get_user_dict(last_goal.user) }, "created": last_goal.created }) activity = sorted(activity, key=operator.itemgetter("created"), reverse=True)[:5] for item in activity: del item["created"] return json_view(data=activity)
def following_list(request): users = [x.target for x in request.user.following.all()] offset = int(request.GET.get("offset", 0)) data = Status.objects.filter(user__in=users).order_by("-created")[offset:offset + settings.STATUSES_ON_PAGE_LIMIT] return json_view(data=data)
def top_tags(request): top_tags = calculate_top_tags() return json_view(data=top_tags)
def mark_as_old(request): pks = request.GET.get('pks').split(',') if len(pks) > 0 and pks[0]: Notification.objects.filter(user=request.user, pk__in=pks).update(is_new=False) return json_view()
def all(request): notifications = Notification.objects.filter(user=request.user).order_by('-created') return json_view(notifications);
def user_list(request, user_pk): user = get_object_or_404(User, pk=user_pk) offset = int(request.GET.get("offset", 0)) data = Status.objects.filter(user=user).order_by("-created")[offset:offset + settings.STATUSES_ON_PAGE_LIMIT] return json_view(data=data)
def lifelist(request, user_pk): user = get_object_or_404(User, pk=user_pk) goals = Goal.objects.filter(user=user).order_by("created").all() return json_view(data=goals)
def json_search_by_tag(request, tag): return json_view(data=search(request, tag))
def following_done_goals(request, user_pk): user = get_object_or_404(User, pk=user_pk) following_users = [x.target for x in request.user.following.all()] done_goals = Goal.objects.filter(user__in=following_users, is_done=True).order_by("-created") return json_view(data=done_goals)