def put(self, request): payload = dict() try: payload["username"] = request.user.username fields = [ 'visibility', 'autoformat', 'twitter', 'facebook', 'linkedin', 'slack' ] form_data = dict() for field in fields: form_data[field] = str2bool( unicodedata.normalize('NFC', request.data[field])) request.user.pref_post_public_visibility = form_data["visibility"] request.user.pref_post_autoformat = form_data["autoformat"] request.user.pref_post_repost_TW = form_data["twitter"] request.user.pref_post_repost_FB = form_data["facebook"] request.user.pref_post_repost_LKin = form_data["linkedin"] request.user.pref_post_repost_Slack = form_data["slack"] request.user.save() payload["success"] = True except Exception as e: payload["success"] = False payload["error"] = "An error occured in the process: " + str(e) payload["operation"] = "modify personal Information" payload["timestamp"] = get_timestamp() return Response(payload)
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)
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)
def put(self, request): payload = dict() try: payload["username"] = request.user.username url_validator = URLValidator() fields = [ 'firstname', 'lastname', 'email', 'birthdate', 'country', 'gender', 'feedtitle', # not Checked 'description', # not Checked 'job', # not Checked 'company_name', # not Checked 'company_website', "newsletter_subscription", # Not Saved yet ! ] form_data = dict() for field in fields: form_data[field] = unicodedata.normalize( 'NFC', request.data[field]) ############################################################################### # DATA VALIDATION # ############################################################################### if form_data["firstname"] != "": FeedUser.objects._validate_firstname(form_data["firstname"]) if form_data["lastname"] != "": FeedUser.objects._validate_lastname(form_data["lastname"]) if form_data["gender"] != "": FeedUser.objects._validate_gender(form_data["gender"]) FeedUser.objects._validate_email(form_data["email"]) url_validator(form_data["company_website"]) ############################################################################### # DATA VALIDATION & Set Attributes - Special Attr # ############################################################################### if form_data["birthdate"] != "": FeedUser.objects._validate_birthdate(form_data["birthdate"]) request.user.birthdate = datetime.datetime.strptime( form_data["birthdate"], '%d/%m/%Y').date() else: request.user.birthdate = None if form_data["country"] != "": FeedUser.objects._validate_country(form_data["country"]) request.user.country = Country.objects.get( name=form_data["country"]) else: request.user.country = None ############################################################################### # SAVING ATTRIBUTES # ############################################################################### request.user.first_name = form_data["firstname"] request.user.last_name = form_data["lastname"] request.user.gender = form_data["gender"] request.user.email = form_data["email"] request.user.rss_feed_title = form_data["feedtitle"] request.user.description = form_data["description"] request.user.job = form_data["job"] request.user.company_name = form_data["company_name"] request.user.company_website = form_data["company_website"] request.user.pref_newsletter_subscription = str2bool( form_data["newsletter_subscription"]) request.user.save() payload["success"] = True except Exception as e: payload["success"] = False payload["error"] = "An error occured in the process: " + str(e) payload["post_id"] = None payload["operation"] = "modify personal Information" payload["timestamp"] = get_timestamp() return Response(payload)