def setUpClass(cls): """ Create user, rss and feed for tests :return: """ super().setUpClass() data = { 'email': '*****@*****.**', 'password': '******' } user = User.register_user(email=data['email'], password=data['password']) user.confirm_email() user.is_staff = True user.is_superuser = True user.save() cls.header = {'HTTP_AUTHORIZATION': f'JWT {user.generate_token()}'} varzesh3_data = { 'title': 'Varzesh3', 'link': 'https://www.varzesh3.com/rss/all' } rss = Rss(**varzesh3_data) rss.save() feed_data = { 'title': 'some title', 'link': 'some link', 'published': 1613298529, 'description': 'some description', 'rss': rss, } feed = Feed(**feed_data) feed.save()
def check_new_rss(rss_id): rss = Rss.objects.get(id=rss_id) feed_list = scrap_rss(rss.link, 0) for feed in feed_list: feed['rss'] = rss feed_item = Feed(**feed) feed_item.save()
def addFeeds(request): #authentications username = None if request.user.is_authenticated(): username = request.user.username else: #If user is not authenticated,return corresponding api error code: newData = { 'status': 401, #bad credential 'media type': 'application/json', 'exception': 'credential failed. Please check your username and password' } return render(request, 'feed/addFeed_response.html.html', {'data': json.dumps(newData)}) if request.method == 'GET': #display for brower api return render(request, 'feed/addFeed.html', {'username': username}) elif request.method == 'POST': try: #avoid all misleading operation exception data = json.loads( request.POST.get('content')) # change into json format url = data['url'] name = data['name'] #check feed name is in the db: duplicate feed name is not allowed if Feed.objects.filter(name=name): newData = { 'status': 400, 'exception': 'Duplicated:Feed is existed' } return render(request, 'feed/addFeed_response.html', {'data': json.dumps(newData)}) #get user name userid = User.objects.get(username=username) # save db feedEntry = Feed(url=url, name=name, creator=userid) feedEntry.save() newData = { 'status': 200, 'url': url, 'name': name, 'action': 'add feed', 'media type': 'application/json', } except: newData = { 'status': 400, 'exception': 'wrong json format/[url] and [name] is a must in the messgae body', } #process feed adding return render(request, 'feed/addFeed_response.html', {'data': json.dumps(newData)})
def add_message(): ref = request.referrer form = FeedPostForm() if form.validate_on_submit(): # process images post_images = [] uploaded_files = request.files.getlist('images') if uploaded_files and uploaded_files[0].filename != '': for file in uploaded_files: filename = secure_filename(file.filename) file_path = os.path.join(UPLOAD_FOLDER, 'posts', filename) file.save(file_path) post_images.append(file_path) # process post from_user = User.objects.get(username=session.get('username')) to_user = User.objects.get(username=request.values.get('to_user')) post = form.post.data # if this is a self post if to_user == from_user: to_user = None # write the message message = Message( from_user=from_user, to_user=to_user, text=post, message_type=POST, ).save() # store on the same user's feed feed = Feed( user=from_user, message=message, ).save() # store images if len(post_images): images = [] for file_path in post_images: (image_ts, width) = image_height_transform(file_path, 'posts', str(message.id)) images.append({"ts": str(image_ts), "w": str(width)}) message.images = images message.save() # process the message process_message(message) if ref: return redirect(ref) else: return redirect(url_for('home_app.home')) else: return 'Error!'
def add_message(): ref = request.referrer form = FeedPostForm() if form.validate_on_submit(): from_user = User.getByName(session.get('username')) from_user = from_user.id # process images post_images = [] uploaded_files = request.files.getlist('images') if uploaded_files and uploaded_files[0].filename != '': for file in uploaded_files: filename = secure_filename(file.filename) folder_path = os.path.join(Config.UPLOAD_FOLDER, 'posts_' + from_user) if not os.path.exists(folder_path): os.makedirs(folder_path) file_path = os.path.join(folder_path, filename) file.save(file_path) post_images.append(file_path) # process post to_user = User.getByName(request.values.get('to_user')) to_user = to_user.id post = form.post.data # if this is a self post if from_user == to_user: to_user = None message = Message( from_user=from_user, to_user=to_user, text=post, ) message.save_database() feed = Feed( user=from_user, message=message.id ) feed.save_database() # store images if len(post_images): images = [] for file_path in post_images: (image_ts, width) = image_height_transform(file_path, 'posts', str(message.id)) images.append({"ts": str(image_ts), "w": str(width)}) message.images = images message.update_record() # process the message process_message(message) if ref: return redirect(ref) else: return redirect(url_for('user_blueprint.home')) else: return abort(404)
def check_rss_every_hour(): for rss in Rss.objects.filter(is_active=True): last_time = rss.feed_set.first() last_time = last_time.published if last_time else 0 feed_list = scrap_rss(rss.link, last_time) for feed in feed_list: feed['rss'] = rss feed_item = Feed(**feed) feed_item.save()
def process_message(message): # get the from_user's friends from_user = message.from_user friends = Relationship.objects.filter(from_user=from_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) for friend in friends: feed = Feed(user=friend.to_user, message=message).save() return True
def process_message(message): # Get the from users friends from_user = message.from_user friends = Relationship.objects.filter(from_user=from_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) for friend in friends: rel = Relationship.get_relationship(friend.to_user, message.to_user) if rel != 'BLOCKED': feed = Feed(user=friend.to_user, message=message).save() return True
def create_mysql_feed(mongo_feed, user): feed_id = str(mongo_feed.id) mysql_feed_query = Feed.objects.filter(feed_obj_id=feed_id) if mysql_feed_query: mysql_feed = mysql_feed_query else: mysql_feed = Feed( keywords=mongo_feed.keywords, talent_level=mongo_feed.talent_level, expect_area=mongo_feed.expect_area, job_desc=mongo_feed.job_desc, salary_min=mongo_feed.salary_min, salary_max=mongo_feed.salary_max, feed_type=mongo_feed.feed_type, job_welfare=','.join(mongo_feed.job_welfare) if mongo_feed.job_welfare else '', language=mongo_feed.language, degree=mongo_feed.degree, major=mongo_feed.major, job_type=mongo_feed.job_type, user=user, expire_time=mongo_feed.expire_time, feed_expire_time=mongo_feed.feed_expire_time, feed_obj_id=str(mongo_feed.id), ) job_domain = [ get_object_or_none( CompanyCategory, category=domain, ) for domain in mongo_feed.job_domain ] prefer = [ get_object_or_none( CompanyCategoryPrefer, name=name, ) for name in mongo_feed.company_prefer ] mysql_feed.save() mysql_feed.job_domain.add(*job_domain) mysql_feed.company_prefer.add(*prefer) mysql_feed.save() return mysql_feed
def create_feeds(db): counter = 1 for rss_feed in feeds: now = datetime.utcnow() dt = datetime(now.year - 1, now.month, now.day, now.hour, now.minute, now.second, tzinfo=pytz.utc) db.session.add( Feed(id=counter, url=rss_feed.get("url"), parser=rss_feed.get("parser"), time_format=rss_feed.get("time_format"), last_updated=dt)) counter += 1 db.session.commit()
def process_message(message): ## get from user's friends and broadcast message ## to all his friends # get the from_user's friends from_user = message.from_user friends = Relationship.objects.filter(from_user=from_user, rel_type=Relationship.FRIENDS, status=Relationship.APPROVED) for friend in friends: # post on all of friends feeds the message # except if to_user is blocked rel = Relationship.get_relationship(friend.to_user, message.to_user) if rel != "BLOCKED": feed = Feed(user=friend.to_user, message=message).save() return True
def add_rss(self, params, url): feed = Feed.objects.filter(url=url).first() if feed: f2u = Feed2User.objects.filter( owner_feed=feed, owner_user_id=self.get_meid()).first() if f2u: raise Exception('订阅地址已添加') else: feed = Feed(url=url) feed_data = get_feed(url) dic2obj(feed_data, ['title', 'subtitle'], feed) feed.save() Post.create_by_entries(feed.id, feed_data.get('entries')) Feed2User.objects.create(owner_user_id=self.get_meid(), owner_feed=feed) return { 'id': feed.id, 'title': feed.title, 'subtitle': feed.subtitle, 'url': url, }
def signup(request): if request.method == 'POST': form = SignUpForm(request.POST) if not form.is_valid(): return render(request, 'user/signup.html', {'form': form}) else: username = form.cleaned_data.get('username') email = form.cleaned_data.get('email') password = form.cleaned_data.get('password') User.objects.create_user(username=username, password=password, email=email) user = authenticate(username=username, password=password) login(request, user) welcome_post = u'{0} has joined the network.'.format( user.username, user.username) feed = Feed(user=user, post=welcome_post) feed.save() return redirect('/') else: return render(request, 'user/signup.html', {'form': SignUpForm()})
def process_message(message): # get the from_user friends from_user = User.getById(message.from_user) friends = Relationship.get_friends(user=from_user.id, rel_type=RELATIONSHIP_TYPE.get(FRIENDS), status=STATUS_TYPE.get(APPROVED)) """ Fan out pattern: means that each user have a list of posts of users that are friends with him (friends_approved). So each user have it's feed. NB: Fan out pattern is Scalable. """ # get the from_user's friends for friend in friends: rel_status = Relationship.get_relationship_status( friend.to_user, message.to_user) if rel_status != "BLOCKED": Feed( user=friend.to_user, message=message.id, ).save_database() return True
def add_message(): ref = request.referrer form = FeedPostForm() # if form.validate_on_submit() or request.method == "POST": if form.validate_on_submit(): # For this to work, have to include text when uploading a picture # It's because post in forms.py has DataRequired() as a validator # At least I think that's why # Process images post_images = [] uploaded_files = request.files.getlist("images") if uploaded_files and uploaded_files[0].filename != "": for file in uploaded_files: filename = secure_filename(file.filename) file_path = os.path.join(UPLOAD_FOLDER, "posts", filename) file.save(file_path) post_images.append(file_path) # Process post from_user = User.objects.get(username=session.get("username")) to_user = User.objects.get(username=request.values.get("to_user")) post = form.post.data # If this a self post if to_user == from_user: to_user = None # Write the message to the database message = Message( from_user=from_user, to_user=to_user, text=post, message_type=POST, ).save() # Store on the same user's feed feed = Feed( user=from_user, message=message, ).save() # Store images if len(post_images): images = [] for file_path in post_images: (image_ts, width) = image_height_transform(file_path, "posts", str(message.id)) images.append({"ts": str(image_ts), "w": str(width)}) message.images = images message.save() # Process the message process_message(message) if ref: return redirect(ref) else: return redirect(url_for("home_app.home"))
def generate_setup(): """Used to initially populate the database with sample data""" user1 = User(username="******", password=pbkdf2_sha256.hash("pass")) user2 = User(username="******", password=pbkdf2_sha256.hash("pass")) user3 = User(username="******", password=pbkdf2_sha256.hash("pass")) feed_dt = datetime(year=2020, month=9, day=1, hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.utc) item_dt = datetime(year=2020, month=9, day=9, hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.utc) feed1 = Feed(id=1, url="https://feeds.feedburner.com/tweakers/mixed", parser="html5lib", time_format="%a, %d %b %Y %H:%M:%S %Z", last_updated=feed_dt) feed2 = Feed(id=2, url="http://www.nu.nl/rss/Algemeen", parser="lxml", time_format="%a, %d %b %Y %H:%M:%S %z", last_updated=feed_dt) follows1 = Follows(username=user1.username, feed_id=feed1.id) follows2 = Follows(username=user2.username, feed_id=feed2.id) item1 = FeedItem(id=1, url="https://stackoverflow.com/", title="Item 1", description="Desc 1", feed_id=1, published=item_dt) item2 = FeedItem(id=2, url="https://www.quora.com/", title="Item 2", description="Desc 2", feed_id=1, published=item_dt) item3 = FeedItem(id=3, url="https://stackoverflow.com/", title="Item 3", description="Desc 3", feed_id=2, published=item_dt) item4 = FeedItem(id=4, url="https://www.quora.com/", title="Item 4", description="Desc 4", feed_id=2, published=item_dt) unread1 = Unread(username=user1.username, item_id=1, feed_id=1) read1 = Read(username=user1.username, item_id=2, feed_id=1) unread2 = Unread(username=user2.username, item_id=3, feed_id=2) read2 = Read(username=user2.username, item_id=4, feed_id=2) return [ user1, user2, user3, feed1, feed2, follows1, follows2, item1, item2, item3, item4, unread1, unread2, read1, read2 ]
def do_task(self, task_code): if task_code == 'commit_customization': feed = Feed( user=self.user, deleted=False, ) feed = feed.save() return True # if task_code == 'lookup_resume': # request = HttpRequest() # request.user = self.user # self.do_resume_read_task(request) if task_code == 'collect_resume': watch_record = UserWatchResume( user=self.user, resume_id='567cd2bd563310078dda3db9', feed_id='522db684fb6dec1d505249f9', add_time=datetime.datetime.now(), type=1, ) watch_record = watch_record.save() return True if task_code == 'download_resume': buy_record = ResumeBuyRecord(user=self.user, resume_id='', resume_url='', status='LookUp') buy_record = buy_record.save() return True if task_code == 'mark_resume': buy_record = ResumeBuyRecord.objects.all() buy_record = buy_record[0] mark_record = DownloadResumeMark(buy_record=buy_record, mark_time=datetime.datetime.now()) mark_record = mark_record.save() return True # if task_code == 'feedback_resume': # result = self.c.post() # self.assertEqual(result.status_code, 200) if task_code == 'buy_package': order = UserOrder.objects.all() order = order[0] order.order_status = 'paid' order.order_type = 1 order = order.save() return True if task_code == 'send_company_card': send_record = SendCompanyCard(send_user=self.user) send_record.save() return True if task_code == 'mutual_recruitment': upload_record = UploadResume(user=self.user) upload_record.save() return True if task_code == 'user_portrait': questionnaire_record = QuestionnaireResult(user=self.user) questionnaire_record.save() return True