def create_post(request): if request.method == 'POST' and request.user.is_authenticated: form = PostForm(request.POST) if form.is_valid(): post = Post() post.post_speciality_number = form.cleaned_data[ 'post_speciality_number'] post.post_short_description = form.cleaned_data[ 'post_short_description'] post.post_text = form.cleaned_data['post_text'] post.post_title = form.cleaned_data['post_title'] post.post_author = request.user post.save() tags = form.cleaned_data['tags'] for tag in tags: post.tag_set.add(tag) tag.post_set.add(post) if request.POST['tag'] != '': new_tag, _ = Tag.objects.get_or_create( tag_name=request.POST['tag']) new_tag.save() post.tag_set.add(new_tag) new_tag.post_set.add(post) post.save() return HttpResponseRedirect('/MyProfile/') else: form = PostForm() d = { 'form': form, } try: d['theme'] = request.session['theme'] except KeyError: pass return render(request, 'home/EditPost.html', d)
def create(self, validated_data): p = Post(**validated_data) p.save() filelist = self.context['request'].FILES.getlist('file') for file in filelist: f = Files.objects.create(file=file, file_user=p) f.save() return p
def new_post(request): if request.method == 'POST': new_post = Post() new_post.body = request.POST['body'] new_post.user = request.user new_post.save() return redirect('/') return render(request, 'new_post.html')
def renew_post(request, pk): username = request.user.username channel_id = pk channel_url = request.GET.get('channel_url') success = True options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--blink-settings=imagesEnabled=false') options.add_argument('--window-size=1366x768') options.add_argument('--disable-gpu') options.add_argument('--lang=ko_KR') options.add_argument('--no-sandbox') options.add_argument( "--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36" ) chrome_driver = webdriver.Chrome(chrome_options=options) chrome_driver_wait = WebDriverWait(chrome_driver, 30) try: chrome_driver.get("https://accounts.kakao.com") s3_client = boto3.client( 's3', region_name='ap-northeast-2', aws_access_key_id=config('AWS_ACCESS_KEY_ID'), aws_secret_access_key=config('AWS_SECRET_ACCESS_KEY')) s3_response = s3_client.get_object( Bucket=config('AWS_STORAGE_BUCKET_NAME'), Key='uploads/cookies/' + username.replace('@', '') + '.pkl') get_cookies = s3_response['Body'].read() for cookie in c_pickle.loads(get_cookies): chrome_driver.add_cookie(cookie) chrome_driver.get(channel_url) sleep(2) for i in range(1): chrome_driver.execute_script( "window.scrollTo(0, document.body.scrollHeight);") sleep(5) post_post_list = chrome_driver_wait.until( EC.presence_of_all_elements_located( (By.CSS_SELECTOR, ".story_channel"))) for item in post_post_list: post_title = item.find_element_by_css_selector(".tit_story").text get_register_date = item.find_element_by_css_selector( ".link_date").text get_register_date = get_register_date.split(" ") get_month = get_register_date[0].replace("월", "") get_day = get_register_date[1].replace("일", "") get_hour = get_register_date[3].split(":")[0] get_hour = get_hour if (get_register_date[2] == "오전") else str(int(get_hour) + 12) get_minute = get_register_date[3].split(":")[1] now = datetime.now() naive = datetime.strptime( str(now.year) + "-" + get_month + "-" + get_day + " " + get_hour + ":" + get_minute + ":00", "%Y-%m-%d %H:%M:%S") local = pytz.timezone(settings.TIME_ZONE) local_dt = local.localize(naive, is_dst=None) # datetimefield를 직접 넣으면 파싱한 시간 그대로 들어가져버려 tz이 꼬인다. 그래서 직접 utc로 바꿨다. post_register_date = local_dt.astimezone(pytz.utc) post_url = item.find_element_by_css_selector( ".link_title").get_attribute("href") try: # update check_post = Post.objects.get(channel_id=channel_id, post_title=post_title) check_post.post_register_date = post_register_date check_post.post_url = post_url check_post.save() except ObjectDoesNotExist: # create post = Post(post_title=post_title, post_register_date=post_register_date, post_url=post_url, channel_id=channel_id) post.save() except Exception as e: logger.info(e) success = False finally: chrome_driver.close() return HttpResponse(json.dumps(success), content_type="application/json")
def checkArticle(request): form = ArticleForm() if request.method == "POST": print('inside post') form = ArticleForm(request.POST) if form.is_valid(): print('form is valid') # try: title = form.cleaned_data["title"] link = form.cleaned_data["link"] body = form.cleaned_data["body"] posted = form.cleaned_data["posted"] data = { 'url': link, 'title': title, 'content': body, } #idhar se karna replace if request.user.is_authenticated: article = Article(title=title, link=link, body=body, user=request.user) else: article = Article(title=title, link=link, body=body) #yahan tak karna h change #article.save() response = requests.post(url='http://localhost:8080/fakebox/check', data=data) temp = json.loads(response.text) result = {} #prediction driver code u = np.array([[0.68244157, 0.68253419]]) var = np.array([[0.01124549, 0.02151113]]) # print(f'mean: {u} and sigma: {var}') #res = result(np.array([0.889, 0.908]), u, sigma2) res = resultFunc( np.array([temp['content']['score'], temp['title']['score']]), u, var) print(f'the result is {res}') if (res > 0.33): article.fake = False print(f'True') result['status'] = 'Real' result['article'] = article result['score'] = temp['title']['score'] + temp['content'][ 'score'] result['resp'] = temp else: article.fake = True print(f'False') result['status'] = 'Fake' result['article'] = article result['score'] = temp['title']['score'] + temp['content'][ 'score'] result['resp'] = temp print(f"The news is {result['status']}") article.save() if posted is True: print('posted if was true') p = Post(article=article) p.save() return render(request, "evaluated.html", {'result': result}) # except: # print('in exception') else: print('form is invalid') return render(request, "checkarticle.html", {'form': form})