Beispiel #1
0
def api_save_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', "")
    user_tags = fields.get('user_tags', "")
    story = None

    logging.user(request.user,
                 "~FBFinding feed (api_save_new_story): %s" % story_url)
    original_feed = Feed.get_feed_from_url(story_url)
    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']
    try:
        story_db = {
            "user_id": user.pk,
            "starred_date": datetime.datetime.now(),
            "story_date": datetime.datetime.now(),
            "story_title": story_title or '[Untitled]',
            "story_permalink": story_url,
            "story_guid": story_url,
            "story_content": story_content,
            "story_author_name": story_author,
            "story_feed_id": original_feed and original_feed.pk or 0,
            "user_tags": [tag for tag in user_tags.split(',')]
        }
        story = MStarredStory.objects.create(**story_db)
        logging.user(
            request, "~FCStarring by ~SBIFTTT~SN: ~SB%s~SN in ~SB%s" %
            (story_db['story_title'][:50], original_feed and original_feed))
        MStarredStoryCounts.count_for_user(user.pk)
    except OperationError:
        logging.user(
            request, "~FCAlready starred by ~SBIFTTT~SN: ~SB%s" %
            (story_db['story_title'][:50]))
        pass

    return {
        "data": [{
            "id": story and story.id,
            "url": story and story.story_permalink
        }]
    }
Beispiel #2
0
def api_save_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', "")
    user_tags = fields.get('user_tags', "")
    story = None
    
    logging.user(request.user, "~FBFinding feed (api_save_new_story): %s" % story_url)
    original_feed = Feed.get_feed_from_url(story_url)
    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']
    try:
        story_db = {
            "user_id": user.pk,
            "starred_date": datetime.datetime.now(),
            "story_date": datetime.datetime.now(),
            "story_title": story_title or '[Untitled]',
            "story_permalink": story_url,
            "story_guid": story_url,
            "story_content": story_content,
            "story_author_name": story_author,
            "story_feed_id": original_feed and original_feed.pk or 0,
            "user_tags": [tag for tag in user_tags.split(',')]
        }
        story = MStarredStory.objects.create(**story_db)
        logging.user(request, "~FCStarring by ~SBIFTTT~SN: ~SB%s~SN in ~SB%s" % (story_db['story_title'][:50], original_feed and original_feed))
        MStarredStoryCounts.count_for_user(user.pk)
    except OperationError:
        logging.user(request, "~FCAlready starred by ~SBIFTTT~SN: ~SB%s" % (story_db['story_title'][:50]))
        pass
    
    return {"data": [{
        "id": story and story.id,
        "url": story and story.story_permalink
    }]}
Beispiel #3
0
def delete_starred_stories(request):
    timestamp = request.POST.get('timestamp', None)
    if timestamp:
        delete_date = datetime.datetime.fromtimestamp(int(timestamp))
    else:
        delete_date = datetime.datetime.now()
    starred_stories = MStarredStory.objects.filter(user_id=request.user.pk,
                                                   starred_date__lte=delete_date)
    stories_deleted = starred_stories.count()
    starred_stories.delete()

    MStarredStoryCounts.count_for_user(request.user.pk, total_only=True)
    starred_counts, starred_count = MStarredStoryCounts.user_counts(request.user.pk, include_total=True)
    
    logging.user(request.user, "~BC~FRDeleting %s/%s starred stories (%s)" % (stories_deleted,
                               stories_deleted+starred_count, delete_date))

    return dict(code=1, stories_deleted=stories_deleted, starred_counts=starred_counts,
                starred_count=starred_count)
Beispiel #4
0
def delete_starred_stories(request):
    timestamp = request.POST.get("timestamp", None)
    if timestamp:
        delete_date = datetime.datetime.fromtimestamp(int(timestamp))
    else:
        delete_date = datetime.datetime.now()
    starred_stories = MStarredStory.objects.filter(user_id=request.user.pk, starred_date__lte=delete_date)
    stories_deleted = starred_stories.count()
    starred_stories.delete()

    MStarredStoryCounts.count_for_user(request.user.pk, total_only=True)
    starred_counts, starred_count = MStarredStoryCounts.user_counts(request.user.pk, include_total=True)

    logging.user(
        request.user,
        "~BC~FRDeleting %s/%s starred stories (%s)" % (stories_deleted, stories_deleted + starred_count, delete_date),
    )

    return dict(code=1, stories_deleted=stories_deleted, starred_counts=starred_counts, starred_count=starred_count)
Beispiel #5
0
def api_save_new_story(request):
    user = request.user
    body = json.decode(request.body)
    fields = body.get('actionFields')
    story_url = fields['story_url']
    story_content = fields.get('story_content', "")
    story_title = fields.get('story_title', "[Untitled]")
    story_author = fields.get('story_author', "")
    user_tags = fields.get('user_tags', "")
    story = None
    
    try:
        original_feed = Feed.get_feed_from_url(story_url)
        story_db = {
            "user_id": user.pk,
            "starred_date": datetime.datetime.now(),
            "story_date": datetime.datetime.now(),
            "story_title": story_title or '[Untitled]',
            "story_permalink": story_url,
            "story_guid": story_url,
            "story_content": story_content,
            "story_author_name": story_author,
            "story_feed_id": original_feed and original_feed.pk or 0,
            "user_tags": [tag for tag in user_tags.split(',')]
        }
        story = MStarredStory.objects.create(**story_db)
        logging.user(request, "~FCStarring by ~SBIFTTT~SN: ~SB%s~SN in ~SB%s" % (story_db['story_title'][:50], original_feed and original_feed))
        MStarredStoryCounts.count_tags_for_user(user.pk)
    except OperationError:
        logging.user(request, "~FCAlready starred by ~SBIFTTT~SN: ~SB%s" % (story_db['story_title'][:50]))
        pass
    
    return {"data": [{
        "id": story and story.id,
        "url": story and story.story_permalink
    }]}
Beispiel #6
0
def add_site_load_script(request, token):
    code = 0
    usf = None
    profile = None
    user_profile = None
    starred_counts = {}

    def image_base64(image_name, path='icons/circular/'):
        image_file = open(
            os.path.join(settings.MEDIA_ROOT, 'img/%s%s' % (path, image_name)),
            'rb')
        return base64.b64encode(image_file.read()).decode('utf-8')

    accept_image = image_base64('newuser_icn_setup.png')
    error_image = image_base64('newuser_icn_sharewith_active.png')
    new_folder_image = image_base64('g_icn_arrow_right.png')
    add_image = image_base64('g_icn_expand_hover.png')

    try:
        profiles = Profile.objects.filter(secret_token=token)
        if profiles:
            profile = profiles[0]
            usf = UserSubscriptionFolders.objects.get(user=profile.user)
            user_profile = MSocialProfile.get_user(user_id=profile.user.pk)
            starred_counts = MStarredStoryCounts.user_counts(profile.user.pk)
        else:
            code = -1
    except Profile.DoesNotExist:
        code = -1
    except UserSubscriptionFolders.DoesNotExist:
        code = -1

    return render(
        request,
        'api/share_bookmarklet.js', {
            'code': code,
            'token': token,
            'folders': (usf and usf.folders) or [],
            'user': profile and profile.user or {},
            'user_profile':
            user_profile and json.encode(user_profile.canonical()) or {},
            'starred_counts': json.encode(starred_counts),
            'accept_image': accept_image,
            'error_image': error_image,
            'add_image': add_image,
            'new_folder_image': new_folder_image,
        },
        content_type='application/javascript')
Beispiel #7
0
def api_saved_tag_list(request):
    user = request.user
    starred_counts, starred_count = MStarredStoryCounts.user_counts(user.pk, include_total=True)
    tags = []
    
    for tag in starred_counts:
        if tag['tag'] == "": continue
        tags.append(dict(label="%s (%s %s)" % (tag['tag'], tag['count'], 
                                               'story' if tag['count'] == 1 else 'stories'),
                         value=tag['tag']))
    tags = sorted(tags, key=lambda t: t['value'].lower())
    catchall = dict(label="All Saved Stories (%s %s)" % (starred_count,
                                                         'story' if starred_count == 1 else 'stories'),
                    value="all")
    tags.insert(0, catchall)
    
    return {"data": tags}
Beispiel #8
0
def api_saved_tag_list(request):
    user = request.user
    starred_counts, starred_count = MStarredStoryCounts.user_counts(user.pk, include_total=True)
    tags = []
    
    for tag in starred_counts:
        if not tag['tag'] or tag['tag'] == "": continue
        tags.append(dict(label="%s (%s %s)" % (tag['tag'], tag['count'], 
                                               'story' if tag['count'] == 1 else 'stories'),
                         value=tag['tag']))
    tags = sorted(tags, key=lambda t: t['value'].lower())
    catchall = dict(label="All Saved Stories (%s %s)" % (starred_count,
                                                         'story' if starred_count == 1 else 'stories'),
                    value="all")
    tags.insert(0, catchall)
    
    return {"data": tags}
Beispiel #9
0
def save_story(request, token=None):
    code      = 0
    story_url = request.POST['story_url']
    user_tags = request.POST.getlist('user_tags') or request.POST.getlist('user_tags[]') or []
    add_user_tag = request.POST.get('add_user_tag', None)
    title     = request.POST['title']
    content   = request.POST.get('content', None)
    rss_url   = request.POST.get('rss_url', None)
    user_notes = request.POST.get('user_notes', 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 (save_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 (save_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']
    
    if add_user_tag:
        user_tags = user_tags + [tag for tag in add_user_tag.split(',')]
        
    starred_story = MStarredStory.objects.filter(user_id=profile.user.pk,
                                                 story_feed_id=feed_id, 
                                                 story_guid=story_url).limit(1).first()
    if not starred_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(),
            "starred_date": datetime.datetime.now(),
            "user_id": profile.user.pk,
            "user_tags": user_tags,
            "user_notes": user_notes,
        }
        starred_story = MStarredStory.objects.create(**story_db)
        logging.user(profile.user, "~BM~FCStarring story from site: ~SB%s: %s" % (story_url, user_tags))
        message = "Saving story from site: %s: %s" % (story_url, user_tags)
    else:
        starred_story.story_content = content
        starred_story.story_title = title
        starred_story.user_tags = user_tags
        starred_story.story_permalink = story_url
        starred_story.story_guid = story_url
        starred_story.story_feed_id = feed_id
        starred_story.user_notes = user_notes
        starred_story.save()
        logging.user(profile.user, "~BM~FC~SBUpdating~SN starred story from site: ~SB%s: %s" % (story_url, user_tags))
        message = "Updating saved story from site: %s: %s" % (story_url, user_tags)

    MStarredStoryCounts.schedule_count_tags_for_user(request.user.pk)
    
    response = HttpResponse(json.encode({
        'code':     code,
        'message':  message,
        'story':    starred_story,
    }), content_type='text/plain')
    response['Access-Control-Allow-Origin'] = '*'
    response['Access-Control-Allow-Methods'] = 'POST'
    
    return response
Beispiel #10
0
def ScheduleCountTagsForUser(user_id):
    from apps.rss_feeds.models import MStarredStoryCounts

    MStarredStoryCounts.count_for_user(user_id)
Beispiel #11
0
 def run(self, user_id):
     from apps.rss_feeds.models import MStarredStoryCounts
     
     MStarredStoryCounts.count_for_user(user_id)
Beispiel #12
0
 def run(self, user_id):
     from apps.rss_feeds.models import MStarredStoryCounts
     
     MStarredStoryCounts.count_for_user(user_id)