def get_post_info(driver): #SCROLL_PAUSE_TIME = 1 # # # Get scroll height # last_height = driver.execute_script("return document.body.scrollHeight") # while True: # # Scroll down to bottom # driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") # # Wait to load page # time.sleep(SCROLL_PAUSE_TIME) # # Calculate new scroll height and compare with last scroll height # new_height = driver.execute_script("return document.body.scrollHeight") # if new_height == last_height: # break # last_height = new_height html = driver.page_source soup = BeautifulSoup(html, 'html.parser') writeSubject = soup.find('h1',{'class' : 'tNGpbb uTUgB YVvGBb'}).getText() div = soup.findAll('div', {'class' : 'n4xnA'}) for post in div: temp = post.findAll('span',{'class' : 'PazDv'}) title= temp[0].getText() write_date = temp[1].getText() if(("오전" in write_date) or ("오후" in write_date)): try: writer = post.find('span',{'class' : 'YVvGBb asQXV'}).getText() content = post.find('div',{'class' : 'pco8Kc obylVb'}).getText("\n") if(Post.objects.filter(postDate = write_date, postContent = content)): return else: Post( postWriter = writer, postTitle = title, postDate = write_date, postContent = content, postUrl = driver.current_url, postSubject = writeSubject ).save() except: content = post.find('div', {'class' : 'JZicYb QRiHXd'}).getText("\n") if(Post.objects.filter(postDate = write_date, postContent = content)): return else: Post( postWriter = writer, postTitle = title, postDate = write_date, postContent = content, postUrl = driver.current_url, postSubject = writeSubject ).save() else: pass return
def setUp(self): super(LikeTests, self).setUp() user = User.objects.create_user('test', '*****@*****.**', '12345678OK') self.current_user = user self.client.force_login(user=user) self.post = Post(title='test', text='test', author=self.current_user) self.post.save()
def create(self, validated_data): request = self.context['request'] p = Post(**validated_data) for file in request.FILES.getlist('file'): f = Files.objects.create(file_user=p, file=file) title = self.context['request'].data.get('title') description = self.context['request'].data.get('description') password = '' token = '' uname = request.data.get('username') uemail = request.data.get('email') if uname: password = randomstring(stringLength=5) token = randomstring(stringLength=10) u = User.objects.create(username=uname, email=uemail) u.set_password(password) pro = Profile.objects.create(user=u) pro.activation_token = token pro.save() u.save() p.poster = u p.save() body = 'Title:' + title + '\n' + 'Description:' + description + '\n' + 'username:'******'\npassword:'******'\nActivation url:http://127.0.0.1:8000/user/activate/' + str( token) msg = EmailMessage(title, body, settings.EMAIL_HOST_USER, ['*****@*****.**']) msg.send() print(request.data) return p
def _get_github_events(author): """Retrieves all the public events for the given GitHub author. Events are retrieved from GitHub's API at https://api.github.com/users/<user>/events GitHub has a rate limit of 60. In order to not exhaust the limit as quickly, the events are cached. A GitHub E-tag is also stored, and used in the header of the request so that GitHub will not count the request towards the rate limit if events are unchanged. """ headers = {'Connection': 'close'} if len(author.github_etag) > 0: headers['If-None-Match'] = author.github_etag url = 'https://api.github.com/users/%s/events' % author.github_user response = requests.get(url, headers=headers) # print response # We didn't get a response or we've reached our GitHub limit of 60. if not response or int(response.headers["X-RateLimit-Remaining"]) == 0: return [] if response.status_code == 200: # Store the etag for future use author.github_etag = response.headers['ETag'] author.save() events = [] for event in response.json(): content = _build_github_event_text(event, author) if content is not None: # Construct the GitHub event post post = Post(content=content, content_type=Post.PLAIN_TEXT, visibility=Post.PRIVATE, author=author, publication_date=dateutil.parser.parse(event['created_at'])) events.append(post.getJsonObj()) # Cache these results in the event that we've reached our rate # limit, or we get a 304 because the events haven't changed. cache.set(author.user.id, events, None) return events elif response.status_code == 304: # Results haven't changed, let's just return the cache, if one exists, # otherwise, we need to get it again. cached = cache.get(author.user.id) if cached is None: author.github_etag = '' return _get_github_events(author) else: return cached else: # print 'ERROR: API at %s returned %d' % url, response.status_code return []
def create(request): if request.method == 'POST': if request.POST["title"] and request.POST['URL'] and request.POST['summary'] and request.POST['body'] and\ request.FILES['image']: post = Post() post.title = request.POST["title"] if request.POST['URL'].startswith( 'http://') or request.POST['URL'].startswith('https://'): post.URL = request.POST['URL'] else: post.URL = 'http://' + request.POST['URL'] post.summary = request.POST['summary'] post.body = request.POST['body'] post.image = request.FILES['image'] post.pub_date = timezone.datetime.now() post.total_votes = 0 post.SentBy = request.user post.save() return redirect('home') else: return render(request, 'create.html', {'error': 'You need to fill all areas!'}) else: return render( request, 'create.html', )
def save_data(self, data, user): """ Creates and saves Post instance after validation. Adds success and error messages for each row in the excel file. """ row_num = 1 missing_errors = [] title_errors = [] success_message = [] has_error = False for row in data: if len(row) == 2: if row[0] == 'Title': continue title = row[0] desc = row[1] new_post = Post(title=title, user=user, desc=desc) try: new_post.full_clean() except ValidationError as e: for message in e.messages: messages.error(self.request, "row " + str(row_num) + ": " + message, extra_tags='invalid_data') has_error = True else: messages.success(self.request, "row " + str(row_num) + ": '" + title + "' saved successfully") new_post.save() else: messages.error(self.request, "row " + str(row_num) + ": " + "missing Title/Desc", extra_tags='invalid_data') row_num = row_num + 1
def upload(): if 'username' in session: form = UploadForm() if request.method == 'POST': if form.validate_on_submit(): # create new record new_post = Post() new_post.poster = User.objects.get( username=session['username']).id new_post.title = form.title.data new_post.tags = [x for x in form.tags.data] if form.link.data: new_post.link = form.link.data if request.files.get('meme'): filename = secure_filename(form.meme.data.filename) hashed_fname = sha256(filename.encode('utf-8')).hexdigest() file_path = os.path.join('upload_folder', hashed_fname) form.meme.data.save(file_path) new_post.image = hashed_fname new_post.save() return redirect(url_for('general_app.index')) return render_template('post/upload.html', form=form) else: return abort(403)
def handle(self, *args, **options): for i in range(1, 100): p = Post() p.title = 'Post number %s' % i p.content = 'Post content number %s' % i p.save() print('Saving %s' % i)
def setUp(self): CustomUser(id=1, first_name='Bohdan', last_name='Dubas', phone='123456789', email='*****@*****.**', is_active=False).save() self.user = CustomUser.objects.get(id=1) Blog(id=22, name='TestName', description='TestDescription', author=self.user).save() self.blog = Blog.objects.get(id=22) Post(id=333, title='TestPost', content='Just testing post', author=self.user, blog=self.blog).save() self.post = Post.objects.get(id=333) Comment(id=4444, author=self.user, post=self.post, content='Great post!', created_at=TEST_DATE).save() self.comment = Comment.objects.get(id=4444)
def handle(self, *args, **options): post_cnt = options['post_cnt'] if post_cnt > 0: Post.objects.bulk_create( [Post(text="Sample Text #{}".format(i)) for i in range(post_cnt)] ) self.stdout.write(self.style.SUCCESS('Successfully add {} posts'.format(post_cnt)))
def pub(request: HttpRequest): try: payload = simplejson.loads(request.body) title = payload['title'] c = payload['content'] post = Post() post.title = title post.postdate = datetime.datetime.now( datetime.timezone(datetime.timedelta(hours=8))) post.auther = User( pk=request.user.id ) # request.user # User(request.user.id) # User(pk=request.user.id) post.save() content = Content() content.post = post content.content = c content.save() return JsonResponse({'post_id': post.id}) except Exception as e: print(e) return HttpResponseBadRequest()
def pub(request): try: payload = simplejson.loads(request.body) post = Post() content = Content() content.content = payload['content'] post.title = payload['title'] post.author = request.user post.content = content try: content.save() post.save() except Exception as e: print(e) return HttpResponse("pub content save error") try: post.save() return JsonResponse({ 'post_id': post.id, 'title': post.title, 'author': post.author.name, 'content': post.content.content }) except Exception as e: print(e) return HttpResponse("pub content save error") except Exception as e: print(e) return HttpResponse("pub content save error")
def post(self, request, *args, **kwargs): data = request.data usertags = [] hashtags = [] for usertag_pk in data.get('usertags'): try: usertag = get_user_model().objects.get(pk=usertag_pk) except get_user_model().DoesNotExist: usertag = None if usertag is not None: usertags.append(usertag) for hashtag_pk in data.get('hashtags'): try: hashtag = get_user_model().objects.get(pk=hashtag_pk) except get_user_model().DoesNotExist: hashtag = None if hashtag is not None: hashtags.append(hashtag) try: author = get_user_model().objects.get(pk=data.get('author')) except get_user_model().DoesNotExist: author = None if author is not None: post = Post( author=author, image=request.FILES["image"], caption=data.get('caption'), location=data.get('location'), usertags=usertags, hashtags=hashtags, ) post.save() return Response(status=status.HTTP_201_CREATED) return Response(status=status.HTTP_404_NOT_FOUND, data={"error": "Invalid pk values"})
def index(request): """发帖的界面""" if request.method == "POST": # 如果请求方式为POST, 则为提交内容 topic = request.POST.get("topic") # 主题 course = request.POST.get("course") content = request.POST.get("content") # 内容 if topic and course and content: post = Post(topic=topic, course=course, counter=0, author_user_id=request.user.id, content=content) # 数据库插入 post.save() request.session["status"] = "提交成功" request.session["id"] = post.id return JsonResponse({"status": "提交成功", "code": 200}) else: if not topic: return JsonResponse({"status": "标题不能为空", "code": 400}) elif not course: return JsonResponse({'status': '请选择对应的课程', "code": 400}) else: return JsonResponse({"status": "请输入内容", "code": 400}) course = Course.objects.all() data = [] for c in course: data.append(c.name) status = None id = None if request.session.get("status") and request.session.get("id"): status = request.session.pop("status") id = request.session.pop("id") return render(request, "post/index.html", context={"data": data, "msg": status, "id": id})
def _create_post_name_text(self, user): text = 'text' name = 'name' post = Post(text=text, name=name, author=user) post.save() return post, name, text
def test_check_cannot_create_old_version_true(self): """ Method check_cannot_create_old_version must return false if old properties not set """ post = Post() self.assertTrue(post._check_cannot_create_old_version(), 'Method check_cannot_create_old_version must return true')
def fake_post(num=500): for i in range(num): user = User.objects.order_by('?').first() ask = Post(title=faker.sentence()[:40], content=faker.paragraph(), user=user) ask.save() print('create post: {}:{}'.format(user.username, ask))
def post(request): if request.method == 'POST': caption = request.POST.get('caption') url = request.POST.get('url') desc = request.POST.get('desc') ins = Post(caption=caption, url=url, desc=desc) ins.save() return render(request, 'post.html')
def post(self, request, pk1, pk2): profile = self.get_profile(pk1) post = self.get_post(pk2) new_post = Post(image=post.image, owner=profile, repost=post.owner, text=post.text) new_post.save() return Response({}, status=status.HTTP_201_CREATED)
def setUp(self): self.client = Client() self.factory = RequestFactory() self.post = Post(type_id=2, title="test_title", content="test_content", created_by_id=8, created_at="2014-03-26 15:33:06", isPublished=True) self.client.login(username="******", password="******")
def test_get_post(self): post1 = Post( title='test_title1', text='test_text1', author=self.test_user ) post2 = Post( title='test_title2', text='test_text2', author=self.test_user ) post1.save() post2.save() self.assertEquals(Post.objects.count(), 2) url = "/api/v1/posts/" response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_401_UNAUTHORIZED) self.assertEquals(Post.objects.count(), 2)
def setUp(self) -> None: """Define the test client and other test variables.""" user = User.objects.create(username="******") # Initialize client and force it to use authentication self.client = APIClient() self.client.force_authenticate(user=user) self.post = Post(title='mansour', text='mansour reviewing tdd') self.post.save() self.url = reverse('api-post-details', kwargs={'pk': self.post.pk})
def create( title, content, author, ): post = Post() post.title = title post.content = content post.author = author post.save() return post
def create_post_comment(file, comment_content): post = Post( author=request.user, photo=file, ) post.save() if comment_content != '': post.add_comment( user=request.user, content=comment_content, )
def post_publish(request): post_id = int(request.POST.get('post_id', 0)) if not post_id: Post().post_save(request) info = "亲! 您的分享已发布成功,正在等待审核!" return redirect(reverse("index:post_success") + '?msg_info=' + info) else: post = Post.objects.get(pk=post_id) post.post_edit(request) info = "您的文章已完成编辑" return redirect(reverse("index:post_success") + '?msg_info=' + info)
def test_save_old_version_error(self): """ save_old_version method must not save old version of post for new post, it must raise exception instead """ text = 'text' name = 'name' post = Post(text=text, name=name) with self.assertRaises(Exception): post._save_old_version()
def put(self, request, user, *args, **kwargs): try: body = self.process_body(request, ['text', 'name', ]) except Exception as error: return self.respond_error_json(error) post = Post(text=body['text'], name=body['name'], author=user, status=Post.ACTIVE) post.save() return self.respond_success_json({'post': post.serialize()})
def post(self, request, *args, **kwargs): print(request.data) post = Post(user=User.objects.get(id=1), model=request.data['model']) post.save() tweets = twitterpy.getTweets(subject=request.data['model']) for co, i in enumerate(tweets): # nlp.analyze(i) Tweet(post=post, tweet=i).setToUniqo(co).save() print('*' * 30) print('*' * 30) print(post.tweets) return self.create(request, *args, **kwargs)
def create_post_view(request): account = request.user post = Post(account=account) if request.method == "POST": serializer = PostSerializer(post, data=request.data) data = {} if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def make_post(request, id_): if request.method == 'POST': title = str(request.POST.get('title', False)) image = request.FILES.get('image', False) video = str(request.POST.get('video', False)) audio = str(request.POST.get('audio', False)) description = str(request.POST.get('content', False)) for_coop = int(request.POST.get('for_cooperative', False)) count = 0 attachments = [] while request.FILES.get('attachment' + str(count), False) is not False: attachments.append( request.FILES.get('attachment' + str(count), False)) count = count + 1 if title and description: if id_ != 'new': new_post = Post.objects.get(id=id_) else: new_post = Post() new_post.author_id = request.user.id new_post.title = title new_post.author_status = author_status(request.user.id) new_post.date_posted = timezone.datetime.now() new_post.content = description if image: new_post.image = image if video: new_post.video = video if audio: new_post.audio = audio if for_coop == 1: new_post.for_cooperative = True member = Member.objects.get(user_id=request.user.id) new_post.cooperative_name = member.coop_detail().name new_post.save() for val in attachments: new_attachment = Attachment() new_attachment.post = new_post new_attachment.file = val new_attachment.save() return render(request, 'post/make_post.html', { 'message': 'Your post has been uploaded', 'status': 'success' }) else: return render(request, 'post/make_post.html', { 'message': 'All fields must be filled', 'status': 'danger' }) return render(request, 'post/make_post.html')