def activity_create(sender, instance, created, **kwargs): if created: if instance.actor.__class__.__name__ == 'User' and instance.verb != verbs.FOLLOWED: if not actions.is_following(instance.actor, obj=instance): actions.follow(user=instance.actor, obj=instance) if instance.action_object: if not actions.is_following(instance.actor, obj=instance.action_object): actions.follow(user=instance.actor, obj=instance.action_object) groups = [] if instance.target: group = '{0}_{1}'.format(instance.target.__class__.__name__, instance.target.pk).lower() groups.append(group) payload = { 'groups': groups, 'type': 'notify', 'data': ActivitySerializer(instance).data } try: redis.notify(json.dumps(payload)) except Exception as e: print(e) instance.make_dar()
def create(self, request, *args, **kwargs): user_id = request.user.id if request.user.is_authenticated else None errors = [] term = None if request.data.get("term"): try: term = models.Term.objects.get( pk=int(request.data.get("term"))) except models.Term.DoesNotExist: term = None else: if request.data.get("term_title"): term, is_created = models.Term.objects.get_or_create( slug=slugify(request.data.get("term_title")), defaults={"title": request.data.get("term_title")}) try: pub = models.Publication.objects.get( pk=int(request.data.get("publication"))) except models.Publication.DoesNotExist: pub = None if term is None: errors.append({"term": "TERM_NONE"}) if request.data.get("taxonomy") is None: errors.append({"taxonomy": "TAXONOMY_NONE"}) if pub is None: errors.append({"publication": "PUBLICATION_NONE"}) if len(errors) > 0: return Response(status=status.HTTP_400_BAD_REQUEST, data=errors) tax = models.PublicationTerm.objects.filter( publication=pub, taxonomy=request.data.get("taxonomy"), description=request.data.get("description"), meta=request.data.get("meta", {}), options=request.data.get("options", {}), term=term, db_status=1).first() if tax is None: tax = models.PublicationTerm.objects.create( publication=pub, taxonomy=request.data.get("taxonomy"), term=term) if request.user.is_authenticated: actions.follow(request.user, tax) with connection.cursor() as cursor: cursor.execute("SELECT FETCH_TAXONOMY(%s, %s, %s, %s)", [ term.slug, request.data.get("publication"), request.data.get("taxonomy"), user_id ]) result = cursor.fetchone()[0] cursor.close() connection.close() return Response(status=status.HTTP_200_OK, data=result)
def follow_term(request, app_id, slug): instance = PublicationTerm.objects.get(pk=slug) if actions.is_following(request.user, instance): if request.method == 'POST': actions.un_follow(request.user, instance) flag = False else: flag = True else: if request.method == 'POST': actions.follow(request.user, instance) flag = True else: flag = False return Response(flag)
def follow(request, app_id, slug): instance = Post.objects.get(pk=slug) if request.method == "GET": return Response({ "is_follow": actions.is_following(request.user, instance), "total_follow": actions.total_following(instance) }) else: if actions.is_following(request.user, instance): actions.un_follow(request.user, instance) flag = False else: actions.follow(request.user, instance) flag = True return Response({ "is_follow": flag, "total_follow": actions.total_following(instance) })
def fetch_posts(request, app_id): pub = Publication.objects.get(pk=app_id) if request.method == "GET": search = request.GET.get('search') page_size = 10 if request.GET.get('page_size') is None else int( request.GET.get('page_size')) page = 1 if request.GET.get('page') is None else int( request.GET.get('page')) auth_id = request.user.id if request.user.is_authenticated else None user_id = request.GET.get("user") post_type = request.GET.get("post_type") terms = request.GET.get("terms").split(",") if request.GET.get( "terms") else [] related_posts = request.GET.get("related_posts").split( ",") if request.GET.get("related_posts") else [] meta = request.GET.get("meta") q = Q(show_cms=True, primary_publication_id=app_id) if user_id: q = q & Q(user_id=user_id) if post_type: q = q & Q(post_type=post_type) if terms: q = q & Q(terms__id__in=terms) if search: q = q & (Q(title__icontains=search) | Q(description__icontains=search)) if related_posts: q = q & Q(post_related__id__in=related_posts) queryset = models.Post.objects.order_by("-id").values_list( "id", flat=True).filter(q) paginator = Paginator(queryset, page_size) posts = list(paginator.page(page).object_list) results = [] for post in posts: results.append( clone_dict( caching_v2.make_post(pub.host, {"instance": post}, False), [ "title", "id", "post_type", "description", "media", "slug" ], None)) return Response(status=status.HTTP_200_OK, data={ "results": results, "count": queryset.count() }) if request.method == "POST": if pub.options.get("allow_guess_post", False): meta = request.data.get("meta", {}) post = Post.objects.create( primary_publication=pub, status="POSTED", show_cms=pub.options.get("auto_guess_public", False) or (request.user.is_authenticated and request.user.is_staff), post_type=request.data.get("post_type", "article"), title=request.data.get("title", "Untitled"), description=request.data.get("description"), content=request.data.get("content"), user=request.user if request.user.is_authenticated else None, meta=meta, is_guess_post=True) if request.data.get("post_related_add", None) is not None: for p in request.data.get("post_related_add", None): pr = Post.objects.get(pk=p) post.post_related.add(pr) if pub.options.get("auto_guess_public", False): for order in ["p", "n"]: key_path_post = "term_{}_{}_{}".format( p.id, post.post_type, order) ids = p.make_posts(post.post_type, order) cache.set(key_path_post, list(ids), timeout=CACHE_TTL) if request.data.get("terms_add", None) is not None: prs = PublicationTerm.objects.filter( id__in=request.data.get("terms_add", [])) for p in prs: post.terms.add(p) if pub.options.get("auto_guess_public", False): for order in ["p", "n"]: key_path_post = "term_{}_{}_{}".format( p.id, post.post_type, order) ids = p.make_posts(post.post_type, order) cache.set(key_path_post, list(ids), timeout=CACHE_TTL) if request.user.is_authenticated: actions.follow(request.user, post) if pub.options.get("auto_guess_public", False): for order in ["p", "n"]: key_path = "{}_{}_{}".format(post.primary_publication.host, post.post_type, order) ids = post.primary_publication.make_posts( post.post_type, order) cache.set(key_path, ids, timeout=CACHE_TTL * 12) return Response(status=status.HTTP_200_OK, data=caching.make_post(True, None, str(post.id), {"master": True})) return Response(status=status.HTTP_401_UNAUTHORIZED)