def story_changes(request): story_hash = request.REQUEST.get('story_hash', None) show_changes = is_true(request.REQUEST.get('show_changes', True)) story, _ = MStory.find_story(story_hash=story_hash) if not story: logging.user(request, "~FYFetching ~FGoriginal~FY story page: ~FRstory not found") return {'code': -1, 'message': 'Story not found.', 'original_page': None, 'failed': True} return { 'story': Feed.format_story(story, show_changes=show_changes) }
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 send_story_email(request): code = 1 message = 'OK' story_id = request.POST['story_id'] feed_id = request.POST['feed_id'] to_address = request.POST['to'] from_name = request.POST['from_name'] from_email = request.POST['from_email'] comments = request.POST['comments'] comments = comments[:2048] # Separated due to PyLint from_address = '*****@*****.**' if not email_re.match(to_address): code = -1 message = 'You need to send the email to a valid email address.' elif not email_re.match(from_email): code = -1 message = 'You need to provide your email address.' elif not from_name: code = -1 message = 'You need to provide your name.' else: story = MStory.objects(story_feed_id=feed_id, story_guid=story_id)[0] story = Feed.format_story(story, feed_id, text=True) feed = Feed.objects.get(pk=story['story_feed_id']) text = render_to_string('mail/email_story_text.xhtml', locals()) html = render_to_string('mail/email_story_html.xhtml', locals()) subject = "%s is sharing a story with you: \"%s\"" % ( from_name, story['story_title']) subject = subject.replace('\n', ' ') msg = EmailMultiAlternatives( subject, text, from_email='NewsBlur <%s>' % from_address, to=[to_address], cc=['%s <%s>' % (from_name, from_email)], headers={'Reply-To': '%s <%s>' % (from_name, from_email)}) msg.attach_alternative(html, "text/html") msg.send() logging.user( request, '~BMSharing story by email: ~FY~SB%s~SN~BM~FY/~SB%s' % (story['story_title'][:50], feed.feed_title[:50])) return {'code': code, 'message': message}
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 send_story_email(request): code = 1 message = 'OK' story_id = request.POST['story_id'] feed_id = request.POST['feed_id'] to_address = request.POST['to'] from_name = request.POST['from_name'] from_email = request.POST['from_email'] comments = request.POST['comments'] comments = comments[:2048] # Separated due to PyLint from_address = '*****@*****.**' if not email_re.match(to_address): code = -1 message = 'You need to send the email to a valid email address.' elif not email_re.match(from_email): code = -1 message = 'You need to provide your email address.' elif not from_name: code = -1 message = 'You need to provide your name.' else: story = MStory.objects(story_feed_id=feed_id, story_guid=story_id)[0] story = Feed.format_story(story, feed_id, text=True) feed = Feed.objects.get(pk=story['story_feed_id']) text = render_to_string('mail/email_story_text.xhtml', locals()) html = render_to_string('mail/email_story_html.xhtml', locals()) subject = "%s is sharing a story with you: \"%s\"" % (from_name, story['story_title']) subject = subject.replace('\n', ' ') msg = EmailMultiAlternatives(subject, text, from_email='NewsBlur <%s>' % from_address, to=[to_address], cc=['%s <%s>' % (from_name, from_email)], headers={'Reply-To': '%s <%s>' % (from_name, from_email)}) msg.attach_alternative(html, "text/html") try: msg.send() except boto.ses.connection.ResponseError, e: code = -1 message = "Email error: %s" % str(e) logging.user(request, '~BMSharing story by email: ~FY~SB%s~SN~BM~FY/~SB%s' % (story['story_title'][:50], feed.feed_title[:50]))
def send_story_email(request): code = 1 message = 'OK' story_id = request.POST['story_id'] feed_id = request.POST['feed_id'] to_address = request.POST['to'] from_name = request.POST['from_name'] from_email = request.POST['from_email'] comments = request.POST['comments'] from_address = '*****@*****.**' if not email_re.match(to_address): code = -1 message = 'You need to send the email to a valid email address.' if not email_re.match(from_email): code = -1 message = 'You need to provide your email address.' if not from_name: code = -1 message = 'You need to provide your name.' else: story = MStory.objects(story_feed_id=feed_id, story_guid=story_id)[0] story = Feed.format_story(story, feed_id, text=True) feed = Feed.objects.get(pk=story['story_feed_id']) text = render_to_string('mail/email_story_text.xhtml', locals()) html = render_to_string('mail/email_story_html.xhtml', locals()) subject = "%s is sharing a story with you: \"%s\"" % (from_name, story['story_title']) msg = EmailMultiAlternatives(subject, text, from_email='NewsBlur <%s>' % from_address, to=[to_address], cc=['%s <%s>' % (from_name, from_email)], headers={'Reply-To': '%s <%s>' % (from_name, from_email)}) msg.attach_alternative(html, "text/html") msg.send() return {'code': code, 'message': message}
def newsletter_story(request, story_hash): story = MStory.objects.get(story_hash=story_hash) story = Feed.format_story(story) return HttpResponse(story['story_content'])
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 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})