示例#1
0
    def delete(self, request, post_id=None):

        payload = dict()

        try:
            check_passed = check_admin_api(request.user)

            post_id = int(unicodedata.normalize('NFC', post_id))
            if type(post_id) is not int or post_id < 1:
                raise Exception("post_id parameter is not valid")

            if not check_passed:
                raise Exception(check_passed)

            payload["username"] = request.user.username

            post = Post.objects.filter(id=post_id, user=request.user)
            if post.count() == 0:
                raise Exception("Post does not exist")

            post.delete()
            payload["success"] = True
            payload["post_id"] = post_id

        except Exception as e:
            payload["success"] = False
            payload["error"] = "An error occured in the process: " + str(e)
            payload["post_id"] = None

        payload["operation"] = "delete article"
        payload["timestamp"] = get_timestamp()
        return Response(payload)
示例#2
0
    def post(self, request, filename=''):

        payload = dict()

        try:
            check_passed = check_admin_api(request.user)

            if check_passed != True:
                raise Exception(check_passed)

            if not 'opml_file' in request.data:
                raise Exception("opml_file not received")

            opml_file = request.data["opml_file"].read().decode(
                'iso-8859-1').encode('ascii', 'ignore').replace("  ", " ")

            payload["feed_errors"] = request.user.load_opml(opml_file)

            payload["success"] = True

        except Exception as e:
            payload["success"] = False
            payload["error"] = "An error occured in the process: " + str(e)

        payload["operation"] = "OPML Import"
        payload["timestamp"] = get_timestamp()
        return Response(payload)
示例#3
0
    def put(self, request, post_id=None):

        payload = dict()

        try:
            check_passed = check_admin_api(request.user)

            post_id = int(post_id)

            if type(post_id) is not int or post_id < 1:
                raise Exception("post_id parameter is not valid")

            if not check_passed:
                raise Exception(check_passed)

            payload["username"] = request.user.username

            title = unicodedata.normalize('NFC', request.data['title'])
            link = unicodedata.normalize('NFC', request.data['link'])

            if title == "" or link == "":
                raise Exception("Title and/or Link is/are missing")

            tags = unicodedata.normalize('NFC', request.data['tags']).split(
                ',')  # We separate each tag and create a list out of it.

            activated_bool = str2bool(
                unicodedata.normalize('NFC', request.data['activated']))

            twitter_bool = str2bool(
                unicodedata.normalize('NFC', request.POST['twitter']))
            facebook_bool = str2bool(
                unicodedata.normalize('NFC', request.POST['facebook']))
            linkedin_bool = str2bool(
                unicodedata.normalize('NFC', request.POST['linkedin']))
            slack_bool = str2bool(
                unicodedata.normalize('NFC', request.POST['slack']))

            if str2bool(
                    unicodedata.normalize('NFC', request.data['autoformat'])):
                title = format_title(title)

            tmp_post = Post.objects.get(id=post_id, user=request.user)

            tmp_post.title = title
            tmp_post.link = link
            tmp_post.activeLink = activated_bool
            tmp_post.tags.clear()

            for i, tag in enumerate(tags):

                tag = tag.replace(" ", "")
                tags[i] = tag

                if tag != "":
                    tmp_obj, created_bool = Tag.objects.get_or_create(name=tag)
                    tmp_post.tags.add(tmp_obj)

                else:
                    tags.pop(i)

            tmp_post.save()

            if twitter_bool and request.user.is_social_network_enabled(
                    network="twitter"):
                tasks.publish_on_twitter.delay(id_article=tmp_post.id)

            if facebook_bool and request.user.is_social_network_enabled(
                    network="facebook"):
                tasks.publish_on_facebook.delay(id_article=tmp_post.id)

            if linkedin_bool and request.user.is_social_network_enabled(
                    network="linkedin"):
                tasks.publish_on_linkedin.delay(id_article=tmp_post.id)

            if slack_bool and request.user.is_social_network_enabled(
                    network="slack"):
                tasks.publish_on_slack.delay(id_article=tmp_post.id)

            payload["success"] = True
            payload["post_id"] = str(post_id)

        except Exception as e:
            payload["success"] = False
            payload["error"] = "An error occured in the process: " + str(e)
            payload["post_id"] = None

        payload["operation"] = "modify article"
        payload["timestamp"] = get_timestamp()
        return Response(payload)
示例#4
0
    def post(self, request, apikey=""):

        payload = dict()

        try:
            if apikey == "":
                check_passed = check_admin_api(request.user)

                if not check_passed:
                    raise Exception(check_passed)

                user = request.user

            else:
                try:
                    user = FeedUser.objects.filter(apikey=apikey)[:1][0]
                except IndexError:
                    raise Exception("The apikey Used is not Valid")

                if user.is_superuser and 'posting_user' in request.POST:
                    tmp_username = unicodedata.normalize(
                        'NFC', request.POST['posting_user'])
                    try:
                        user = FeedUser.objects.get(username=tmp_username)
                    except ObjectDoesNotExist:
                        raise Exception(
                            "The Provided posting_user `%s` does not exist" %
                            tmp_username)

            payload["username"] = user.username

            if 'article_id' in request.POST:
                RSSArticle_Assoc_id = unicodedata.normalize(
                    'NFC', request.POST['article_id'])
                RSSArticle_Assoc_QuerySet = RSSArticle_Assoc.objects.filter(
                    id=RSSArticle_Assoc_id, user=user)

                if not RSSArticle_Assoc_QuerySet.exists():
                    _err = "The given RSSArticle (id: %d) with the given username `%s`doesn't exist." % (
                        RSSArticle_Assoc_id, user.username)
                    raise Exception(_err)

                RSSArticle_Assoc_obj = RSSArticle_Assoc_QuerySet[0]

            else:
                RSSArticle_Assoc_id = -1

            title = unicodedata.normalize('NFC', request.POST['title'])
            link = unicodedata.normalize('NFC', request.POST['link'])

            # We separate each tag and create a list out of it.
            tags = unicodedata.normalize('NFC',
                                         request.POST['tags']).split(',')

            activated_bool = str2bool(
                unicodedata.normalize('NFC', request.POST['activated']))

            twitter_bool = str2bool(
                unicodedata.normalize('NFC', request.POST['twitter']))
            facebook_bool = str2bool(
                unicodedata.normalize('NFC', request.POST['facebook']))
            linkedin_bool = str2bool(
                unicodedata.normalize('NFC', request.POST['linkedin']))
            slack_bool = str2bool(
                unicodedata.normalize('NFC', request.POST['slack']))

            if str2bool(
                    unicodedata.normalize('NFC', request.POST['autoformat'])):
                title = format_title(title)

            if title == "" or link == "":
                raise Exception("Title and/or Link is/are missing")

            tmp_post = Post.objects.create(title=title,
                                           link=link,
                                           clicks=0,
                                           user=user,
                                           activeLink=activated_bool)

            for i, tag in enumerate(tags):

                tag = tag.replace(" ", "")
                tags[i] = tag

                if tag != "":
                    tmp_obj, created_bool = Tag.objects.get_or_create(name=tag)
                    tmp_post.tags.add(tmp_obj)

                else:
                    tags.pop(i)

            tmp_post.save()

            if RSSArticle_Assoc_id != -1:
                RSSArticle_Assoc_obj.reposted = True
                RSSArticle_Assoc_obj.marked_read = True
                RSSArticle_Assoc_obj.save()

            if twitter_bool and user.is_social_network_enabled(
                    network="twitter"):
                tasks.publish_on_twitter.delay(id_article=tmp_post.id)

            if facebook_bool and user.is_social_network_enabled(
                    network="facebook"):
                tasks.publish_on_facebook.delay(id_article=tmp_post.id)

            if linkedin_bool and user.is_social_network_enabled(
                    network="linkedin"):
                tasks.publish_on_linkedin.delay(id_article=tmp_post.id)

            if slack_bool and user.is_social_network_enabled(network="slack"):
                tasks.publish_on_slack.delay(id_article=tmp_post.id)

            payload["success"] = True
            payload["post_id"] = str(tmp_post.id)

        except Exception as e:
            payload["success"] = False
            payload["error"] = "An error occured in the process: " + str(e)

        payload["operation"] = "submit article"
        payload["timestamp"] = get_timestamp()
        return Response(payload)