def api_share_new_story(request): user = request.user body = request.body_json fields = body.get('actionFields') story_url = urlnorm.normalize(fields['story_url']) content = fields.get('story_content', "") story_title = fields.get('story_title', "[Untitled]") story_author = fields.get('story_author', "") comments = fields.get('comments', None) feed = Feed.get_feed_from_url(story_url, create=True, fetch=True) content = lxml.html.fromstring(content) content.make_links_absolute(story_url) content = lxml.html.tostring(content) shared_story = MSharedStory.objects.filter(user_id=user.pk, story_feed_id=feed and feed.pk or 0, story_guid=story_url).limit(1).first() if not shared_story: story_db = { "story_guid": story_url, "story_permalink": story_url, "story_title": story_title, "story_feed_id": feed and feed.pk or 0, "story_content": content, "story_author": story_author, "story_date": datetime.datetime.now(), "user_id": user.pk, "comments": comments, "has_comments": bool(comments), } shared_story = MSharedStory.objects.create(**story_db) socialsubs = MSocialSubscription.objects.filter(subscription_user_id=user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user(request, "~BM~FYSharing story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments)) else: logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments)) try: socialsub = MSocialSubscription.objects.get(user_id=user.pk, subscription_user_id=user.pk) except MSocialSubscription.DoesNotExist: socialsub = None if socialsub: socialsub.mark_story_ids_as_read([shared_story.story_hash], shared_story.story_feed_id, request=request) else: RUserStory.mark_read(user.pk, shared_story.story_feed_id, shared_story.story_hash) shared_story.publish_update_to_subscribers() return {"data": [{ "id": shared_story and shared_story.story_guid, "url": shared_story and shared_story.blurblog_permalink() }]}
def payment_history(request): user = request.user if request.user.is_staff: user_id = request.REQUEST.get('user_id', request.user.pk) user = User.objects.get(pk=user_id) history = PaymentHistory.objects.filter(user=user) statistics = { "last_seen_date": user.profile.last_seen_on, "timezone": unicode(user.profile.timezone), "stripe_id": user.profile.stripe_id, "profile": user.profile, "feeds": UserSubscription.objects.filter(user=user).count(), "email": user.email, "read_story_count": RUserStory.read_story_count(user.pk), "feed_opens": UserSubscription.objects.filter(user=user).aggregate( sum=Sum('feed_opens'))['sum'], } return { 'is_premium': user.profile.is_premium, 'premium_expire': user.profile.premium_expire, 'payments': history, 'statistics': statistics, }
def payment_history(request): user = request.user if request.user.is_staff: user_id = request.REQUEST.get('user_id', request.user.pk) user = User.objects.get(pk=user_id) history = PaymentHistory.objects.filter(user=user) statistics = { "created_date": user.date_joined, "last_seen_date": user.profile.last_seen_on, "timezone": unicode(user.profile.timezone), "stripe_id": user.profile.stripe_id, "profile": user.profile, "feeds": UserSubscription.objects.filter(user=user).count(), "email": user.email, "read_story_count": RUserStory.read_story_count(user.pk), "feed_opens": UserSubscription.objects.filter(user=user).aggregate(sum=Sum('feed_opens'))['sum'], "training": { 'title': MClassifierTitle.objects.filter(user_id=user.pk).count(), 'tag': MClassifierTag.objects.filter(user_id=user.pk).count(), 'author': MClassifierAuthor.objects.filter(user_id=user.pk).count(), 'feed': MClassifierFeed.objects.filter(user_id=user.pk).count(), } } return { 'is_premium': user.profile.is_premium, 'premium_expire': user.profile.premium_expire, 'payments': history, 'statistics': statistics, }
def payment_history(request): user = request.user if request.user.is_staff: user_id = request.REQUEST.get('user_id', request.user.pk) user = User.objects.get(pk=user_id) history = PaymentHistory.objects.filter(user=user) statistics = { "created_date": user.date_joined, "last_seen_date": user.profile.last_seen_on, "last_seen_ip": user.profile.last_seen_ip, "timezone": unicode(user.profile.timezone), "stripe_id": user.profile.stripe_id, "profile": user.profile, "feeds": UserSubscription.objects.filter(user=user).count(), "email": user.email, "read_story_count": RUserStory.read_story_count(user.pk), "feed_opens": UserSubscription.objects.filter(user=user).aggregate( sum=Sum('feed_opens'))['sum'], "training": { 'title': MClassifierTitle.objects.filter(user_id=user.pk).count(), 'tag': MClassifierTag.objects.filter(user_id=user.pk).count(), 'author': MClassifierAuthor.objects.filter(user_id=user.pk).count(), 'feed': MClassifierFeed.objects.filter(user_id=user.pk).count(), } } return { 'is_premium': user.profile.is_premium, 'premium_expire': user.profile.premium_expire, 'payments': history, 'statistics': statistics, }
def payment_history(request): user = request.user if request.user.is_staff: user_id = request.REQUEST.get("user_id", request.user.pk) user = User.objects.get(pk=user_id) history = PaymentHistory.objects.filter(user=user) statistics = { "last_seen_date": user.profile.last_seen_on, "timezone": unicode(user.profile.timezone), "stripe_id": user.profile.stripe_id, "profile": user.profile, "feeds": UserSubscription.objects.filter(user=user).count(), "email": user.email, "read_story_count": RUserStory.read_story_count(user.pk), "feed_opens": UserSubscription.objects.filter(user=user).aggregate(sum=Sum("feed_opens"))["sum"], } return { "is_premium": user.profile.is_premium, "premium_expire": user.profile.premium_expire, "payments": history, "statistics": statistics, }
def share_story(request, token=None): code = 0 story_url = request.POST['story_url'] comments = request.POST['comments'] title = request.POST['title'] content = request.POST.get('content', None) rss_url = request.POST.get('rss_url', None) feed_id = request.POST.get('feed_id', None) or 0 feed = None message = None profile = None if request.user.is_authenticated: profile = request.user.profile else: try: profile = Profile.objects.get(secret_token=token) except Profile.DoesNotExist: code = -1 if token: message = "Not authenticated, couldn't find user by token." else: message = "Not authenticated, no token supplied and not authenticated." if not profile: return HttpResponse(json.encode({ 'code': code, 'message': message, 'story': None, }), content_type='text/plain') if feed_id: feed = Feed.get_by_id(feed_id) else: if rss_url: logging.user(request.user, "~FBFinding feed (share_story): %s" % rss_url) feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True) if not feed: logging.user(request.user, "~FBFinding feed (share_story): %s" % story_url) feed = Feed.get_feed_from_url(story_url, create=True, fetch=True) if feed: feed_id = feed.pk if content: content = lxml.html.fromstring(content) content.make_links_absolute(story_url) content = lxml.html.tostring(content) else: importer = TextImporter(story=None, story_url=story_url, request=request, debug=settings.DEBUG) document = importer.fetch(skip_save=True, return_document=True) content = document['content'] if not title: title = document['title'] shared_story = MSharedStory.objects.filter(user_id=profile.user.pk, story_feed_id=feed_id, story_guid=story_url).limit(1).first() if not shared_story: story_db = { "story_guid": story_url, "story_permalink": story_url, "story_title": title, "story_feed_id": feed_id, "story_content": content, "story_date": datetime.datetime.now(), "user_id": profile.user.pk, "comments": comments, "has_comments": bool(comments), } shared_story = MSharedStory.objects.create(**story_db) socialsubs = MSocialSubscription.objects.filter(subscription_user_id=profile.user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user(profile.user, "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments)) message = "Sharing story from site: %s: %s" % (story_url, comments) else: shared_story.story_content = content shared_story.story_title = title shared_story.comments = comments shared_story.story_permalink = story_url shared_story.story_guid = story_url shared_story.has_comments = bool(comments) shared_story.story_feed_id = feed_id shared_story.save() logging.user(profile.user, "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" % (story_url, comments)) message = "Updating shared story from site: %s: %s" % (story_url, comments) try: socialsub = MSocialSubscription.objects.get(user_id=profile.user.pk, subscription_user_id=profile.user.pk) except MSocialSubscription.DoesNotExist: socialsub = None if socialsub: socialsub.mark_story_ids_as_read([shared_story.story_hash], shared_story.story_feed_id, request=request) else: RUserStory.mark_read(profile.user.pk, shared_story.story_feed_id, shared_story.story_hash) shared_story.publish_update_to_subscribers() response = HttpResponse(json.encode({ 'code': code, 'message': message, 'story': shared_story, }), content_type='text/plain') response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Allow-Methods'] = 'POST' return response
def share_story(request, token): code = 0 story_url = request.POST['story_url'] comments = request.POST['comments'] title = request.POST['title'] content = request.POST['content'] rss_url = request.POST.get('rss_url') feed_id = request.POST.get('feed_id') or 0 feed = None message = None if not story_url: code = -1 else: try: profile = Profile.objects.get(secret_token=token) except Profile.DoesNotExist: code = -1 if feed_id: feed = Feed.get_by_id(feed_id) else: if rss_url: feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True) if not feed: feed = Feed.get_feed_from_url(story_url, create=True, fetch=True) if feed: feed_id = feed.pk content = lxml.html.fromstring(content) content.make_links_absolute(story_url) content = lxml.html.tostring(content) shared_story = MSharedStory.objects.filter(user_id=profile.user.pk, story_feed_id=feed_id, story_guid=story_url).limit(1).first() if not shared_story: story_db = { "story_guid": story_url, "story_permalink": story_url, "story_title": title, "story_feed_id": feed_id, "story_content": content, "story_date": datetime.datetime.now(), "user_id": profile.user.pk, "comments": comments, "has_comments": bool(comments), } shared_story = MSharedStory.objects.create(**story_db) socialsubs = MSocialSubscription.objects.filter(subscription_user_id=profile.user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user(profile.user, "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments)) else: shared_story.story_content = content shared_story.story_title = title shared_story.comments = comments shared_story.story_permalink = story_url shared_story.story_guid = story_url shared_story.has_comments = bool(comments) shared_story.story_feed_id = feed_id shared_story.save() logging.user(profile.user, "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" % (story_url, comments)) try: socialsub = MSocialSubscription.objects.get(user_id=profile.user.pk, subscription_user_id=profile.user.pk) except MSocialSubscription.DoesNotExist: socialsub = None if socialsub: socialsub.mark_story_ids_as_read([shared_story.story_hash], shared_story.story_feed_id, request=request) else: RUserStory.mark_read(profile.user.pk, shared_story.story_feed_id, shared_story.story_hash) shared_story.publish_update_to_subscribers() response = HttpResponse(json.encode({ 'code': code, 'message': message, 'story': None, }), mimetype='text/plain') response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Allow-Methods'] = 'POST' return response
def api_share_new_story(request): user = request.user body = request.body_json fields = body.get('actionFields') story_url = urlnorm.normalize(fields['story_url']) story_content = fields.get('story_content', "") story_title = fields.get('story_title', "") story_author = fields.get('story_author', "") comments = fields.get('comments', None) logging.user(request.user, "~FBFinding feed (api_share_new_story): %s" % story_url) original_feed = Feed.get_feed_from_url(story_url, create=True, fetch=True) story_hash = MStory.guid_hash_unsaved(story_url) feed_id = (original_feed and original_feed.pk or 0) if not user.profile.is_premium and MSharedStory.feed_quota(user.pk, story_hash, feed_id=feed_id): return {"errors": [{ 'message': 'Only premium users can share multiple stories per day from the same site.' }]} quota = 3 if MSharedStory.feed_quota(user.pk, story_hash, quota=quota): logging.user(request, "~BM~FRNOT ~FYSharing story from ~SB~FCIFTTT~FY, over quota: ~SB%s: %s" % (story_url, comments)) return {"errors": [{ 'message': 'You can only share %s stories per day.' % quota }]} if not story_content or not story_title: ti = TextImporter(feed=original_feed, story_url=story_url, request=request) original_story = ti.fetch(return_document=True) if original_story: story_url = original_story['url'] if not story_content: story_content = original_story['content'] if not story_title: story_title = original_story['title'] if story_content: story_content = lxml.html.fromstring(story_content) story_content.make_links_absolute(story_url) story_content = lxml.html.tostring(story_content) shared_story = MSharedStory.objects.filter(user_id=user.pk, story_feed_id=original_feed and original_feed.pk or 0, story_guid=story_url).limit(1).first() if not shared_story: title_max = MSharedStory._fields['story_title'].max_length story_db = { "story_guid": story_url, "story_permalink": story_url, "story_title": story_title and story_title[:title_max] or "[Untitled]", "story_feed_id": original_feed and original_feed.pk or 0, "story_content": story_content, "story_author_name": story_author, "story_date": datetime.datetime.now(), "user_id": user.pk, "comments": comments, "has_comments": bool(comments), } try: shared_story = MSharedStory.objects.create(**story_db) socialsubs = MSocialSubscription.objects.filter(subscription_user_id=user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user(request, "~BM~FYSharing story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments)) except NotUniqueError: logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments)) else: logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments)) try: socialsub = MSocialSubscription.objects.get(user_id=user.pk, subscription_user_id=user.pk) except MSocialSubscription.DoesNotExist: socialsub = None if socialsub and shared_story: socialsub.mark_story_ids_as_read([shared_story.story_hash], shared_story.story_feed_id, request=request) elif shared_story: RUserStory.mark_read(user.pk, shared_story.story_feed_id, shared_story.story_hash) if shared_story: shared_story.publish_update_to_subscribers() return {"data": [{ "id": shared_story and shared_story.story_guid, "url": shared_story and shared_story.blurblog_permalink() }]}
def api_share_new_story(request): user = request.user body = request.body_json fields = body.get('actionFields') story_url = urlnorm.normalize(fields['story_url']) story_content = fields.get('story_content', "") story_title = fields.get('story_title', "") story_author = fields.get('story_author', "") comments = fields.get('comments', None) logging.user(request.user, "~FBFinding feed (api_share_new_story): %s" % story_url) original_feed = Feed.get_feed_from_url(story_url, create=True, fetch=True) story_hash = MStory.guid_hash_unsaved(story_url) if not user.profile.is_premium and MSharedStory.feed_quota(user.pk, original_feed and original_feed.pk or 0, story_hash): return {"errors": [{ 'message': 'Only premium users can share multiple stories per day from the same site.' }]} if not story_content or not story_title: ti = TextImporter(feed=original_feed, story_url=story_url, request=request) original_story = ti.fetch(return_document=True) if original_story: story_url = original_story['url'] if not story_content: story_content = original_story['content'] if not story_title: story_title = original_story['title'] if story_content: story_content = lxml.html.fromstring(story_content) story_content.make_links_absolute(story_url) story_content = lxml.html.tostring(story_content) shared_story = MSharedStory.objects.filter(user_id=user.pk, story_feed_id=original_feed and original_feed.pk or 0, story_guid=story_url).limit(1).first() if not shared_story: title_max = MSharedStory._fields['story_title'].max_length story_db = { "story_guid": story_url, "story_permalink": story_url, "story_title": story_title and story_title[:title_max] or "[Untitled]", "story_feed_id": original_feed and original_feed.pk or 0, "story_content": story_content, "story_author": story_author, "story_date": datetime.datetime.now(), "user_id": user.pk, "comments": comments, "has_comments": bool(comments), } try: shared_story = MSharedStory.objects.create(**story_db) socialsubs = MSocialSubscription.objects.filter(subscription_user_id=user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user(request, "~BM~FYSharing story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments)) except NotUniqueError: logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments)) else: logging.user(request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments)) try: socialsub = MSocialSubscription.objects.get(user_id=user.pk, subscription_user_id=user.pk) except MSocialSubscription.DoesNotExist: socialsub = None if socialsub and shared_story: socialsub.mark_story_ids_as_read([shared_story.story_hash], shared_story.story_feed_id, request=request) elif shared_story: RUserStory.mark_read(user.pk, shared_story.story_feed_id, shared_story.story_hash) if shared_story: shared_story.publish_update_to_subscribers() return {"data": [{ "id": shared_story and shared_story.story_guid, "url": shared_story and shared_story.pytuneblog_permalink() }]}
def share_story(request, token=None): code = 0 story_url = request.REQUEST['story_url'] comments = request.REQUEST['comments'] title = request.REQUEST['title'] content = request.REQUEST.get('content', None) rss_url = request.REQUEST.get('rss_url', None) feed_id = request.REQUEST.get('feed_id', None) or 0 feed = None message = None profile = None if request.user.is_authenticated(): profile = request.user.profile else: try: profile = Profile.objects.get(secret_token=token) except Profile.DoesNotExist: code = -1 if token: message = "Not authenticated, couldn't find user by token." else: message = "Not authenticated, no token supplied and not authenticated." if not profile: return HttpResponse(json.encode({ 'code': code, 'message': message, 'story': None, }), mimetype='text/plain') if feed_id: feed = Feed.get_by_id(feed_id) else: if rss_url: logging.user(request.user, "~FBFinding feed (share_story): %s" % rss_url) feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True) if not feed: logging.user(request.user, "~FBFinding feed (share_story): %s" % story_url) feed = Feed.get_feed_from_url(story_url, create=True, fetch=True) if feed: feed_id = feed.pk if content: content = lxml.html.fromstring(content) content.make_links_absolute(story_url) content = lxml.html.tostring(content) else: importer = TextImporter(story=None, story_url=story_url, request=request, debug=settings.DEBUG) document = importer.fetch(skip_save=True, return_document=True) content = document['content'] if not title: title = document['title'] shared_story = MSharedStory.objects.filter(user_id=profile.user.pk, story_feed_id=feed_id, story_guid=story_url).limit(1).first() if not shared_story: story_db = { "story_guid": story_url, "story_permalink": story_url, "story_title": title, "story_feed_id": feed_id, "story_content": content, "story_date": datetime.datetime.now(), "user_id": profile.user.pk, "comments": comments, "has_comments": bool(comments), } shared_story = MSharedStory.objects.create(**story_db) socialsubs = MSocialSubscription.objects.filter(subscription_user_id=profile.user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user(profile.user, "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments)) message = "Sharing story from site: %s: %s" % (story_url, comments) else: shared_story.story_content = content shared_story.story_title = title shared_story.comments = comments shared_story.story_permalink = story_url shared_story.story_guid = story_url shared_story.has_comments = bool(comments) shared_story.story_feed_id = feed_id shared_story.save() logging.user(profile.user, "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" % (story_url, comments)) message = "Updating shared story from site: %s: %s" % (story_url, comments) try: socialsub = MSocialSubscription.objects.get(user_id=profile.user.pk, subscription_user_id=profile.user.pk) except MSocialSubscription.DoesNotExist: socialsub = None if socialsub: socialsub.mark_story_ids_as_read([shared_story.story_hash], shared_story.story_feed_id, request=request) else: RUserStory.mark_read(profile.user.pk, shared_story.story_feed_id, shared_story.story_hash) shared_story.publish_update_to_subscribers() response = HttpResponse(json.encode({ 'code': code, 'message': message, 'story': shared_story, }), mimetype='text/plain') response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Allow-Methods'] = 'POST' return response
def api_share_new_story(request): user = request.user body = request.body_json fields = body.get('actionFields') story_url = urlnorm.normalize(fields['story_url']) story_content = fields.get('story_content', "") story_title = fields.get('story_title', "") story_author = fields.get('story_author', "") comments = fields.get('comments', None) original_feed = Feed.get_feed_from_url(story_url, create=True, fetch=True) if not story_content or not story_title: ti = TextImporter(feed=original_feed, story_url=story_url, request=request) original_story = ti.fetch(return_document=True) if original_story: story_url = original_story['url'] if not story_content: story_content = original_story['content'] if not story_title: story_title = original_story['title'] story_content = lxml.html.fromstring(story_content) story_content.make_links_absolute(story_url) story_content = lxml.html.tostring(story_content) shared_story = MSharedStory.objects.filter( user_id=user.pk, story_feed_id=original_feed and original_feed.pk or 0, story_guid=story_url).limit(1).first() if not shared_story: story_db = { "story_guid": story_url, "story_permalink": story_url, "story_title": story_title or "[Untitled]", "story_feed_id": original_feed and original_feed.pk or 0, "story_content": story_content, "story_author": story_author, "story_date": datetime.datetime.now(), "user_id": user.pk, "comments": comments, "has_comments": bool(comments), } shared_story = MSharedStory.objects.create(**story_db) socialsubs = MSocialSubscription.objects.filter( subscription_user_id=user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user( request, "~BM~FYSharing story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments)) else: logging.user( request, "~BM~FY~SBAlready~SN shared story from ~SB~FCIFTTT~FY: ~SB%s: %s" % (story_url, comments)) try: socialsub = MSocialSubscription.objects.get( user_id=user.pk, subscription_user_id=user.pk) except MSocialSubscription.DoesNotExist: socialsub = None if socialsub: socialsub.mark_story_ids_as_read([shared_story.story_hash], shared_story.story_feed_id, request=request) else: RUserStory.mark_read(user.pk, shared_story.story_feed_id, shared_story.story_hash) shared_story.publish_update_to_subscribers() return { "data": [{ "id": shared_story and shared_story.story_guid, "url": shared_story and shared_story.blurblog_permalink() }] }
def share_story(request, token): code = 0 story_url = request.POST['story_url'] comments = request.POST['comments'] title = request.POST['title'] content = request.POST['content'] rss_url = request.POST.get('rss_url') feed_id = request.POST.get('feed_id') or 0 feed = None message = None if not story_url: code = -1 else: try: profile = Profile.objects.get(secret_token=token) except Profile.DoesNotExist: code = -1 if feed_id: feed = Feed.get_by_id(feed_id) else: if rss_url: feed = Feed.get_feed_from_url(rss_url, create=True, fetch=True) if not feed: feed = Feed.get_feed_from_url(story_url, create=True, fetch=True) if feed: feed_id = feed.pk content = lxml.html.fromstring(content) content.make_links_absolute(story_url) content = lxml.html.tostring(content) shared_story = MSharedStory.objects.filter( user_id=profile.user.pk, story_feed_id=feed_id, story_guid=story_url).limit(1).first() if not shared_story: story_db = { "story_guid": story_url, "story_permalink": story_url, "story_title": title, "story_feed_id": feed_id, "story_content": content, "story_date": datetime.datetime.now(), "user_id": profile.user.pk, "comments": comments, "has_comments": bool(comments), } shared_story = MSharedStory.objects.create(**story_db) socialsubs = MSocialSubscription.objects.filter( subscription_user_id=profile.user.pk) for socialsub in socialsubs: socialsub.needs_unread_recalc = True socialsub.save() logging.user( profile.user, "~BM~FYSharing story from site: ~SB%s: %s" % (story_url, comments)) else: shared_story.story_content = content shared_story.story_title = title shared_story.comments = comments shared_story.story_permalink = story_url shared_story.story_guid = story_url shared_story.has_comments = bool(comments) shared_story.story_feed_id = feed_id shared_story.save() logging.user( profile.user, "~BM~FY~SBUpdating~SN shared story from site: ~SB%s: %s" % (story_url, comments)) try: socialsub = MSocialSubscription.objects.get( user_id=profile.user.pk, subscription_user_id=profile.user.pk) except MSocialSubscription.DoesNotExist: socialsub = None if socialsub: socialsub.mark_story_ids_as_read([shared_story.story_hash], shared_story.story_feed_id, request=request) else: RUserStory.mark_read(profile.user.pk, shared_story.story_feed_id, shared_story.story_hash) shared_story.publish_update_to_subscribers() response = HttpResponse(json.encode({ 'code': code, 'message': message, 'story': None, }), mimetype='text/plain') response['Access-Control-Allow-Origin'] = '*' response['Access-Control-Allow-Methods'] = 'POST' return response
def payment_history(request): user = request.user if request.user.is_staff: user_id = request.GET.get('user_id', request.user.pk) user = User.objects.get(pk=user_id) history = PaymentHistory.objects.filter(user=user) statistics = { "created_date": user.date_joined, "last_seen_date": user.profile.last_seen_on, "last_seen_ip": user.profile.last_seen_ip, "timezone": str(user.profile.timezone), "stripe_id": user.profile.stripe_id, "paypal_email": user.profile.latest_paypal_email, "profile": user.profile, "feeds": UserSubscription.objects.filter(user=user).count(), "email": user.email, "read_story_count": RUserStory.read_story_count(user.pk), "feed_opens": UserSubscription.objects.filter(user=user).aggregate( sum=Sum('feed_opens'))['sum'], "training": { 'title_ps': MClassifierTitle.objects.filter(user_id=user.pk, score__gt=0).count(), 'title_ng': MClassifierTitle.objects.filter(user_id=user.pk, score__lt=0).count(), 'tag_ps': MClassifierTag.objects.filter(user_id=user.pk, score__gt=0).count(), 'tag_ng': MClassifierTag.objects.filter(user_id=user.pk, score__lt=0).count(), 'author_ps': MClassifierAuthor.objects.filter(user_id=user.pk, score__gt=0).count(), 'author_ng': MClassifierAuthor.objects.filter(user_id=user.pk, score__lt=0).count(), 'feed_ps': MClassifierFeed.objects.filter(user_id=user.pk, score__gt=0).count(), 'feed_ng': MClassifierFeed.objects.filter(user_id=user.pk, score__lt=0).count(), } } next_invoice = None stripe_customer = user.profile.stripe_customer() paypal_api = user.profile.paypal_api() if stripe_customer: try: invoice = stripe.Invoice.upcoming(customer=stripe_customer.id) for lines in invoice.lines.data: next_invoice = dict( payment_date=datetime.datetime.fromtimestamp( lines.period.start), payment_amount=invoice.amount_due / 100.0, payment_provider="(scheduled)", scheduled=True) break except stripe.error.InvalidRequestError: pass if paypal_api and not next_invoice and user.profile.premium_renewal and len( history): next_invoice = dict(payment_date=history[0].payment_date + dateutil.relativedelta.relativedelta(years=1), payment_amount=history[0].payment_amount, payment_provider="(scheduled)", scheduled=True) return { 'is_premium': user.profile.is_premium, 'is_archive': user.profile.is_archive, 'is_pro': user.profile.is_pro, 'premium_expire': user.profile.premium_expire, 'premium_renewal': user.profile.premium_renewal, 'active_provider': user.profile.active_provider, 'payments': history, 'statistics': statistics, 'next_invoice': next_invoice, }