def story_public_comments(request): format = request.REQUEST.get('format', 'json') relative_user_id = request.REQUEST.get('user_id', None) feed_id = int(request.REQUEST['feed_id']) story_id = request.REQUEST['story_id'] if not relative_user_id: relative_user_id = get_user(request).pk stories = MSharedStory.objects.filter(story_feed_id=feed_id, story_guid=story_id).limit(1) stories = Feed.format_stories(stories) stories, profiles = MSharedStory.stories_with_comments_and_profiles(stories, relative_user_id, check_all=True, public=True) if format == 'html': stories = MSharedStory.attach_users_to_stories(stories, profiles) return render_to_response('social/story_comments.xhtml', { 'story': stories[0], }, context_instance=RequestContext(request)) else: return json.json_response(request, { 'comments': stories[0]['public_comments'], 'user_profiles': profiles, })
def mark_story_as_unshared(request): feed_id = int(request.POST['feed_id']) story_id = request.POST['story_id'] format = request.REQUEST.get('format', 'json') original_story_found = True story = MStory.objects(story_feed_id=feed_id, story_guid=story_id).limit(1).first() if not story: original_story_found = False shared_story = MSharedStory.objects(user_id=request.user.pk, story_feed_id=feed_id, story_guid=story_id).limit(1).first() if not shared_story: return json.json_response(request, {'code': -1, 'message': 'Shared story not found.'}) socialsubs = MSocialSubscription.objects.filter(subscription_user_id=request.user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user(request, "~FC~SKUn-sharing ~FM%s: ~SB~FB%s" % (shared_story.story_title[:20], shared_story.comments[:30])) shared_story.delete() if original_story_found: story.count_comments() else: story = shared_story story = Feed.format_story(story) stories, profiles = MSharedStory.stories_with_comments_and_profiles([story], request.user.pk, check_all=True) if format == 'html': stories = MSharedStory.attach_users_to_stories(stories, profiles) return render_to_response('social/story_share.xhtml', { 'story': stories[0], }, context_instance=RequestContext(request)) else: return json.json_response(request, { 'code': 1, 'message': "Story unshared.", 'story': stories[0], 'user_profiles': profiles, })
def mark_story_as_unshared(request): feed_id = int(request.POST["feed_id"]) story_id = request.POST["story_id"] relative_user_id = request.POST.get("relative_user_id") or request.user.pk format = request.REQUEST.get("format", "json") original_story_found = True story = MStory.objects(story_feed_id=feed_id, story_guid=story_id).limit(1).first() if not story: original_story_found = False shared_story = ( MSharedStory.objects(user_id=request.user.pk, story_feed_id=feed_id, story_guid=story_id).limit(1).first() ) if not shared_story: return json.json_response(request, {"code": -1, "message": "Shared story not found."}) socialsubs = MSocialSubscription.objects.filter(subscription_user_id=request.user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user( request, "~FC~SKUn-sharing ~FM%s: ~SB~FB%s" % (shared_story.story_title[:20], shared_story.comments[:30]) ) shared_story.delete() if original_story_found: story.count_comments() else: story = shared_story story = Feed.format_story(story) stories, profiles = MSharedStory.stories_with_comments_and_profiles([story], relative_user_id, check_all=True) if format == "html": stories = MSharedStory.attach_users_to_stories(stories, profiles) return render_to_response( "social/social_story.xhtml", {"story": stories[0]}, context_instance=RequestContext(request) ) else: return json.json_response( request, {"code": 1, "message": "Story unshared.", "story": stories[0], "user_profiles": profiles} )
def story_public_comments(request): format = request.REQUEST.get("format", "json") relative_user_id = request.REQUEST.get("user_id", None) feed_id = int(request.REQUEST["feed_id"]) story_id = request.REQUEST["story_id"] if not relative_user_id: relative_user_id = get_user(request).pk stories = MSharedStory.objects.filter(story_feed_id=feed_id, story_guid=story_id).limit(1) stories = Feed.format_stories(stories) stories, profiles = MSharedStory.stories_with_comments_and_profiles( stories, relative_user_id, check_all=True, public=True ) if format == "html": stories = MSharedStory.attach_users_to_stories(stories, profiles) return render_to_response( "social/story_comments.xhtml", {"story": stories[0]}, context_instance=RequestContext(request) ) else: return json.json_response(request, {"comments": stories[0]["public_comments"], "user_profiles": profiles})
def mark_story_as_shared(request): code = 1 feed_id = int(request.POST['feed_id']) story_id = request.POST['story_id'] comments = request.POST.get('comments', '') source_user_id = request.POST.get('source_user_id') post_to_services = request.POST.getlist('post_to_services') format = request.REQUEST.get('format', 'json') MSocialProfile.get_user(request.user.pk) story, original_story_found = MStory.find_story(feed_id, story_id) if not story: return json.json_response(request, { 'code': -1, 'message': 'Could not find the original story and no copies could be found.' }) shared_story = MSharedStory.objects.filter(user_id=request.user.pk, story_feed_id=feed_id, story_guid=story_id).limit(1).first() if not shared_story: story_db = dict([(k, v) for k, v in story._data.items() if k is not None and v is not None]) story_values = dict(user_id=request.user.pk, comments=comments, has_comments=bool(comments), story_db_id=story.id) story_db.update(story_values) shared_story = MSharedStory.objects.create(**story_db) if source_user_id: shared_story.set_source_user_id(int(source_user_id)) socialsubs = MSocialSubscription.objects.filter(subscription_user_id=request.user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user(request, "~FCSharing ~FM%s: ~SB~FB%s" % (story.story_title[:20], comments[:30])) else: shared_story.comments = comments shared_story.has_comments = bool(comments) shared_story.save() logging.user(request, "~FCUpdating shared story ~FM%s: ~SB~FB%s" % ( story.story_title[:20], comments[:30])) if original_story_found: story.count_comments() shared_story.publish_update_to_subscribers() story = Feed.format_story(story) check_all = not original_story_found stories, profiles = MSharedStory.stories_with_comments_and_profiles([story], request.user.pk, check_all=check_all) story = stories[0] story['shared_comments'] = strip_tags(shared_story['comments'] or "") if post_to_services: for service in post_to_services: if service not in shared_story.posted_to_services: PostToService.delay(shared_story_id=shared_story.id, service=service) if shared_story.source_user_id and shared_story.comments: EmailStoryReshares.apply_async(kwargs=dict(shared_story_id=shared_story.id), countdown=60) if format == 'html': stories = MSharedStory.attach_users_to_stories(stories, profiles) return render_to_response('social/story_share.xhtml', { 'story': story, }, context_instance=RequestContext(request)) else: return json.json_response(request, { 'code': code, 'story': story, 'user_profiles': profiles, })
def load_social_page(request, user_id, username=None, **kwargs): start = time.time() user = request.user social_user_id = int(user_id) social_user = get_object_or_404(User, pk=social_user_id) offset = int(request.REQUEST.get('offset', 0)) limit = int(request.REQUEST.get('limit', 6)) page = request.REQUEST.get('page') format = request.REQUEST.get('format', None) has_next_page = False feed_id = kwargs.get('feed_id') or request.REQUEST.get('feed_id') if page: offset = limit * (int(page) - 1) user_social_profile = None if user.is_authenticated(): user_social_profile = MSocialProfile.get_user(user.pk) social_profile = MSocialProfile.get_user(social_user_id) params = dict(user_id=social_user.pk) if feed_id: params['story_feed_id'] = feed_id mstories = MSharedStory.objects(**params).order_by('-shared_date')[offset:offset+limit+1] stories = Feed.format_stories(mstories) if len(stories) > limit: has_next_page = True stories = stories[:-1] checkpoint1 = time.time() if not stories: params = { "user": user, "stories": [], "feeds": {}, "social_user": social_user, "social_profile": social_profile, 'user_social_profile' : json.encode(user_social_profile and user_social_profile.page()), } template = 'social/social_page.xhtml' return render_to_response(template, params, context_instance=RequestContext(request)) story_feed_ids = list(set(s['story_feed_id'] for s in stories)) feeds = Feed.objects.filter(pk__in=story_feed_ids) feeds = dict((feed.pk, feed.canonical(include_favicon=False)) for feed in feeds) for story in stories: if story['story_feed_id'] in feeds: # Feed could have been deleted. story['feed'] = feeds[story['story_feed_id']] shared_date = localtime_for_timezone(story['shared_date'], social_user.profile.timezone) story['shared_date'] = shared_date stories, profiles = MSharedStory.stories_with_comments_and_profiles(stories, social_user.pk, check_all=True) checkpoint2 = time.time() if user.is_authenticated(): for story in stories: if user.pk in story['shared_by_friends'] or user.pk in story['shared_by_public']: story['shared_by_user'] = True shared_story = MSharedStory.objects.get(user_id=user.pk, story_feed_id=story['story_feed_id'], story_guid=story['id']) story['user_comments'] = shared_story.comments stories = MSharedStory.attach_users_to_stories(stories, profiles) params = { 'social_user' : social_user, 'stories' : stories, 'user_social_profile' : json.encode(user_social_profile and user_social_profile.page()), 'social_profile': social_profile, 'feeds' : feeds, 'user_profile' : hasattr(user, 'profile') and user.profile, 'has_next_page' : has_next_page, 'holzer_truism' : random.choice(jennyholzer.TRUISMS) #if not has_next_page else None } diff1 = checkpoint1-start diff2 = checkpoint2-start timediff = time.time()-start logging.user(request, "~FYLoading ~FMsocial page~FY: ~SB%s%s ~SN(%.4s seconds, ~SB%.4s/%.4s~SN)" % ( social_profile.title[:22], ('~SN/p%s' % page) if page > 1 else '', timediff, diff1, diff2)) if format == 'html': template = 'social/social_stories.xhtml' else: template = 'social/social_page.xhtml' return render_to_response(template, params, context_instance=RequestContext(request))
def mark_story_as_shared(request): code = 1 feed_id = int(request.POST["feed_id"]) story_id = request.POST["story_id"] comments = request.POST.get("comments", "") source_user_id = request.POST.get("source_user_id") relative_user_id = request.POST.get("relative_user_id") or request.user.pk post_to_services = request.POST.getlist("post_to_services") format = request.REQUEST.get("format", "json") MSocialProfile.get_user(request.user.pk) story, original_story_found = MStory.find_story(feed_id, story_id) if not story: return json.json_response( request, {"code": -1, "message": "Could not find the original story and no copies could be found."} ) shared_story = ( MSharedStory.objects.filter(user_id=request.user.pk, story_feed_id=feed_id, story_guid=story_id) .limit(1) .first() ) if not shared_story: story_db = dict([(k, v) for k, v in story._data.items() if k is not None and v is not None]) story_values = dict( user_id=request.user.pk, comments=comments, has_comments=bool(comments), story_db_id=story.id ) story_db.update(story_values) shared_story = MSharedStory.objects.create(**story_db) if source_user_id: shared_story.set_source_user_id(int(source_user_id)) socialsubs = MSocialSubscription.objects.filter(subscription_user_id=request.user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user(request, "~FCSharing ~FM%s: ~SB~FB%s" % (story.story_title[:20], comments[:30])) else: shared_story.comments = comments shared_story.has_comments = bool(comments) shared_story.save() logging.user(request, "~FCUpdating shared story ~FM%s: ~SB~FB%s" % (story.story_title[:20], comments[:30])) if original_story_found: story.count_comments() shared_story.publish_update_to_subscribers() story = Feed.format_story(story) check_all = not original_story_found stories, profiles = MSharedStory.stories_with_comments_and_profiles([story], relative_user_id, check_all=check_all) story = stories[0] story["shared_comments"] = strip_tags(shared_story["comments"] or "") story["shared_by_user"] = True if post_to_services: for service in post_to_services: if service not in shared_story.posted_to_services: PostToService.delay(shared_story_id=shared_story.id, service=service) if shared_story.source_user_id and shared_story.comments: EmailStoryReshares.apply_async( kwargs=dict(shared_story_id=shared_story.id), countdown=settings.SECONDS_TO_DELAY_CELERY_EMAILS ) if format == "html": stories = MSharedStory.attach_users_to_stories(stories, profiles) return render_to_response( "social/social_story.xhtml", {"story": story}, context_instance=RequestContext(request) ) else: return json.json_response(request, {"code": code, "story": story, "user_profiles": profiles})
def load_social_page(request, user_id, username=None, **kwargs): start = time.time() user = request.user social_user_id = int(user_id) social_user = get_object_or_404(User, pk=social_user_id) offset = int(request.REQUEST.get("offset", 0)) limit = int(request.REQUEST.get("limit", 6)) page = request.REQUEST.get("page") format = request.REQUEST.get("format", None) has_next_page = False feed_id = kwargs.get("feed_id") or request.REQUEST.get("feed_id") if page: offset = limit * (int(page) - 1) user_social_profile = None user_social_services = None if user.is_authenticated(): user_social_profile = MSocialProfile.get_user(user.pk) user_social_services = MSocialServices.get_user(user.pk) social_profile = MSocialProfile.get_user(social_user_id) params = dict(user_id=social_user.pk) if feed_id: params["story_feed_id"] = feed_id mstories = MSharedStory.objects(**params).order_by("-shared_date")[offset : offset + limit + 1] stories = Feed.format_stories(mstories) if len(stories) > limit: has_next_page = True stories = stories[:-1] checkpoint1 = time.time() if not stories: params = { "user": user, "stories": [], "feeds": {}, "social_user": social_user, "social_profile": social_profile, "user_social_services": user_social_services, "user_social_profile": json.encode(user_social_profile and user_social_profile.page()), } template = "social/social_page.xhtml" return render_to_response(template, params, context_instance=RequestContext(request)) story_feed_ids = list(set(s["story_feed_id"] for s in stories)) feeds = Feed.objects.filter(pk__in=story_feed_ids) feeds = dict((feed.pk, feed.canonical(include_favicon=False)) for feed in feeds) for story in stories: if story["story_feed_id"] in feeds: # Feed could have been deleted. story["feed"] = feeds[story["story_feed_id"]] shared_date = localtime_for_timezone(story["shared_date"], social_user.profile.timezone) story["shared_date"] = shared_date stories, profiles = MSharedStory.stories_with_comments_and_profiles(stories, social_user.pk, check_all=True) checkpoint2 = time.time() if user.is_authenticated(): for story in stories: if user.pk in story["share_user_ids"]: story["shared_by_user"] = True shared_story = MSharedStory.objects.get( user_id=user.pk, story_feed_id=story["story_feed_id"], story_guid=story["id"] ) story["user_comments"] = shared_story.comments stories = MSharedStory.attach_users_to_stories(stories, profiles) params = { "social_user": social_user, "stories": stories, "user_social_profile": user_social_profile, "user_social_profile_page": json.encode(user_social_profile and user_social_profile.page()), "user_social_services": user_social_services, "user_social_services_page": json.encode(user_social_services and user_social_services.to_json()), "social_profile": social_profile, "feeds": feeds, "user_profile": hasattr(user, "profile") and user.profile, "has_next_page": has_next_page, "holzer_truism": random.choice(jennyholzer.TRUISMS), # if not has_next_page else None } diff1 = checkpoint1 - start diff2 = checkpoint2 - start timediff = time.time() - start logging.user( request, "~FYLoading ~FMsocial page~FY: ~SB%s%s ~SN(%.4s seconds, ~SB%.4s/%.4s~SN)" % (social_profile.title[:22], ("~SN/p%s" % page) if page > 1 else "", timediff, diff1, diff2), ) if format == "html": template = "social/social_stories.xhtml" else: template = "social/social_page.xhtml" return render_to_response(template, params, context_instance=RequestContext(request))