def ask(request): if request.method == 'POST': form = AskQuestionForm(request.POST) if form.is_valid() and request.user.is_authenticated(): kw = { "name": form.cleaned_data["title"], "content": form.cleaned_data["content"], "author": request.user, } q = question(**kw) q.save() for tg in form.cleaned_data["tags"].split(","): tg_c = tg.replace(" ", "") t = Tag.objects.all().filter(tagName=tg_c) if t: q.tags.add(t[0]) else: t = Tag(tagName=tg_c) t.save() q.tags.add(t) return HttpResponseRedirect('/question/' + str(q.id)) else: form = AskQuestionForm() return render(request, 'ask.html', {'form': form})
def handle(self, *args, **options): tags = [ 'java', 'php', 'android', 'jquery', 'python', 'html', 'css', 'ios', 'mysql', 'sql', ] for tag in tags: if len(Tag.objects.filter(name=tag)) == 0: t = Tag() t.name = tag t.save() number = int(options['number']) tags = Tag.objects.all() for q in Question.objects.all(): if len(q.tags.all()) < number: for i in range(0, number - len(q.tags.all())): t = choice(tags) if t not in q.tags.all(): q.tags.add(t)
def handle(self, *args, **options): user = User.objects.get(email='*****@*****.**') list_tags = ["Perl", "Python", "TechnoPark", "MySQL", "Django", "Mailru", "Voloshin", "Firefox", "black-jack", "bender"] Question.objects.all().delete() Answer.objects.all().delete() Tag.objects.all().delete() for i in range(0, 100): q=Question( author=user, body='body ' + str(i), title = 'title ' + str(i), ) q.save() for j in range(1, 3): a=Answer( text='answer ' + str(i) + ' ' + str(j), author=user, question=q, ) a.save() tmp = i%10 t=Tag( name = list_tags[tmp], rating = tmp, ) t.save() t.question.add(q)
def connectTags(q, tags): _tags = tags.split(',') for tag in _tags: tag = tag.strip(' ') t = Tag(text = tag) t.save() q.tags.add(t)
def _fill_tag(self): print('FILLING TAG MODEL...') for ix in range(500): temp_tag = Tag(name='Tag{}'.format(ix + 1), path='tag{}'.format(ix + 1)) temp_tag.save() print('ADDED {} TAGS'.format(Tag.objects.count()))
def fill_db(self): for i in range(0, 30): usr = User(username='******' + str(i), password='******' + str(i), first_name='User') usr.save() prof = Profile(avatar='avatar.jpeg', user=usr) prof.save() try: tbag = Tag.objects.get(name='Bucket' + str(i % 20)) tbag.popularity += 1 except Exception: tbag = Tag(name='Bucket' + str(i % 20), popularity=1) tbag.save() quest = Question(title=str(i) + 'bucket', text='see my bucket, I have a' + str(10000 - i) + ' bucket remains', author=prof, votes=i) quest.save() quest.liked.add(prof) quest.tag.add(tbag) for j in range(0, 20): ans = Answer(text='Test answer' + str(i), author=prof, question=quest) ans.votes = int(((i + j) * 6) % 40) ans.save() ans.voted.add(prof)
def test_tags(self): q = Question.create( author_id=self.user._id, title='How to patch KDE on FreeBSD?', body="subjxxxxxx", tags=["kde", "freebsd", "anime"], ) q.save() self.run_tasks() cnt = 0 for tag in Tag.find(): cnt += 1 self.assertEqual(tag.questions_count, 1) self.assertEqual(cnt, len(q.tags)) q2 = Question.create( author_id=self.user._id, title='TCPDump on FreeBSD', body="Does FreeBSD has a linux-like tcpdump or it's been as usual", tags=["freebsd", "tcpdump"], ) q2.save() self.run_tasks() t = Tag.get("freebsd") self.assertEqual(t.questions_count, 2) q2.destroy() q.destroy() self.run_tasks() self.assertEqual(Tag.find().count(), 4) # tags are not deleted automatically anymore
def generate_tags(n): arr = open('/home/hase/technopark/web/AskZaytsev/scripts/tags', 'r').read().split('\n') asize = len(arr) for i in range(0, asize): t = Tag(title=arr[i]) print(t.title) t.save() print('tag')
def generateTags(n): import urllib2 url = "http://randomword.setgetgo.com/get.php" for i in range(n): urldata = urllib2.urlopen(url) word = urldata.readlines()[0][:-2] #removed \r\n tag = Tag(tagName=word) #print word, tag tag.save() if i % 5 == 0: print i
def ask(request): if request.POST: form = QuestionForm(request.POST, author=request.user) if form.is_valid(): question = form.save() tag = request.POST.get('tags') for word in tag.split(" "): t = Tag(name = word) t.save() t.question.add(question) return redirect('/') form = QuestionForm() return render(request, 'ask.html', { 'form' : form })
def handle(self, *args, **options): tags = [ 'javascript', 'java', 'cshp', 'php', 'android', 'jquery', 'python', 'html', 'css', 'cpp', 'ios', 'mysql', 'objective_c', 'sql', 'asp_net', 'ruby_on_rails', 'iphone', 'angularjs', 'regexp' ] number = int(options['number']) for i in range(0, number): t = Tag() t.title = choice(tags) + str(i+2000) t.save() self.stdout.write('[%d] added tag %s' % (t.id, t.title))
def handle(self, *args, **options): (amount,) = args count = int(amount) #EXCEPTIONS if count < 1: raise ValueError('incorrect parameter') #relations_q_t=0 createdTags=0 #generate tags for i in range(count): tag_text = randomWord(random.randint(3,7)) t = Tag(text=tag_text) t.save() createdTags += 1 print('was created : ' + str(createdTags) + ' tags') print('was created : ' + str(createdTags) + ' tags')
def handle(self, *args, **options): tags = [ 'JavaScript', 'Java', 'Django', 'Nginx', 'Gunicorn', 'php', 'Android', 'Jquery', 'Python', 'HTML', 'CSS', 'ios', 'MySQL', 'Windows', 'Docker' ] for tag in tags: if len(Tag.objects.filter(text=tag)) == 0: t = Tag() t.text = tag t.style_number = randint(1, 8) t.save() number = int(options['number']) tags = Tag.objects.all() questions = Question.objects.all() for q in questions: if len(q.tags.all()) < number: for i in range(0, number - len(q.tags.all())): t = choice(tags) if t not in q.tags.all(): q.tags.add(t) q.save() self.stdout.write('in question [%d] add tags' % q.id)
def test_create_question(self): attrs = { "title": "a real question", "body": "what is the meaning of life and everything", "tags": ["guide"] } resp = self.post(f"/api/v1/questions/", json=attrs) self.assertEqual(resp.status_code, 401) resp = self.post(f"/api/v1/questions/", json=attrs, user=self.user1) self.assertEqual(resp.status_code, 200) self.run_tasks() data = resp.json q = Question.get(data["data"]["_id"]) self.assertEqual(q.body, attrs["body"]) self.assertEqual(q.title, attrs["title"]) self.assertCountEqual(q.tags, attrs["tags"]) t: Tag = Tag.get("guide") self.assertIsNotNone(t) self.assertEqual(t.questions_count, 1)
def setup(): f = Faker() tags = f.words(nb=20) for i in tags: a = Tag(tag=i) a.save() for i in range(100): tt = f.text(50) te = f.text() a = Question(title=tt, text=te, author_id=randint(2, 4)) a.save() a.tags.add(randint(1, 20), randint(1, 20), randint(1, 20)) for i in range(300): te = f.text() an = Answer(text=te, author_id=randint(2, 4), question_id=randint(1, 100)) an.save()
def handle(self, *args, **options): def get_rand_color(): return random.choice([ 'red', 'orange', 'blue', 'green', 'cyan', 'violet', 'teal', 'maroon', 'indigo', 'peru' ]) tags = [ 'Technopark', 'C++', 'Java', 'Python', 'Perl', 'Django', 'Apple', 'Android', 'Haskell', 'CSS', 'HTML', 'Bootstrap', 'Twitter', 'Vkontakte', 'Facebook', 'IOS', 'Windows' ] added = 0 for tag in tags: if len(Tag.objects.filter(title=tag)) == 0: t = Tag() t.title = tag t.color = get_rand_color() t.save() added += 1 print(added, " Tags is added")
def handle(self, *args, **options): user_range = 25 ask_range = 37 answers_range = 97 f_name = [ 'Red', 'Mad', 'Dummy', 'Crazy', 'Big', 'Black', 'Lonely', 'Shiz', 'Drunken', 'Wicked', 'Brainless', 'Rich', 'Stubborn', 'Stupid', 'Fat', 'Wooden', 'Donald', 'American', 'Dead', 'Lucky', 'Night' ] l_name = [ 'Sailor', 'Rat', 'Man', 'Rabbit', 'Donkey', 'Policeman', 'Child', 'Penguin', 'Joker', 'Lawmaker', 'Judge', 'Iron', 'President', 'Trump', 'Duck', 'Timmy', 'Kartman', 'Kenny', 'Orc', 'Illuminati', 'Band', 'Slave', 'Coffee', 'Aviato', 'Redneck', 'Camper', 'King' ] for i in range(0, user_range): username = u'{0}{1}'.format( f_name[random.randrange(0, len(f_name))], l_name[random.randrange(0, len(l_name))]) email = u'{0}@e-corp.com'.format(username) password = u'{0}123'.format(username) try: User.object.create_user(username=username, email=email, password=password) except: username = u'{0}{1}'.format(username, i) email = u'{0}@e-corp.com'.format(username) User.object.create_user(username=username, email=email, password=password) tags_list = [ 'LOL', 'mad', 'wazzzup', 'DonaldTrump', 'luxury', 'shaurma', 'Skynet', 'TheyAreWatchingYou', 'illuminati', 'Futurama', 'Bender', 'BlackJack', 'TacoBell', 'BreakingBad', ] for tag in tags_list: try: new_tag = Tag() new_tag.title = tag new_tag.save() except: pass f_question = ['How to', 'Why to', 'Can I', 'What if'] l_question = ['fly', 'run', 'jump', 'degrade', 'capture the world'] users = User.object.all() users_count = len(users) tags_list = Tag.objects.all() tags_count = len(tags_list) for i in range(0, ask_range): ask = Ask() random_pk = random.randrange(1, users_count) ask.author = users[random_pk] ask.question = u'{0} {1}'.format( f_question[random.randrange(0, len(f_question))], l_question[random.randrange(0, len(l_question))]) ask.text = u'{0} question by {1}'.format(ask.question, ask.author.username) ask_tags = [] tags_range = random.randrange(1, 4) for i in range(0, tags_range): ask_tags.append(tags_list[random.randrange(0, tags_count)]) ask.rating = random.randrange( -10, 30) # temporary remove later by UserVote ask.save() ask.tags = set(ask_tags) ask.save() asks = Ask.objects.all() asks_count = len(asks) f_answer = ['Just', 'I dont', 'May be', 'You should'] l_answer = ['do it', 'know', 'know', 'forget it'] for i in range(0, answers_range): answer = Answer() random_pk = random.randrange(1, users_count) answer.author = users[random_pk] ask_pk = random.randrange(1, asks_count) answer.ask = asks[ask_pk] answer.text = u'{0} {1}'.format( f_answer[random.randrange(0, len(f_answer))], l_answer[random.randrange(0, len(l_answer))]) answer.save()
def setUp(self) -> None: BasePost.destroy_all() Tag.destroy_all()
def handle(self, *args, **options): # User.objects.all().delete() # Tag.objects.all().delete() # Profile.objects.all().delete() # Question.objects.all().delete() # Answer.objects.all().delete() # Like.objects.all().delete() try: u = User.objects.get(username='******') except: u = User.objects.create(username='******', first_name='test', email='*****@*****.**') u.set_password('test') u.save() p = Profile.objects.create(user_id=u.id, rating=20) item_list = [] for i in range(0, int(options['users'])): u = User(username=randomword(9) + str(i), first_name=randomword(3) + str(i), email=randomword(10) + str(i) + '@aithelle.com') item_list.append(u) if i % 10000 == 0: User.objects.bulk_create(item_list) item_list = [] User.objects.bulk_create(item_list) um = User.objects.aggregate(Min('id'), Max('id')) item_list = [] for i in range(0, int(options['users'])): p = Profile(user_id=um['id__max'] - i, rating=random.randint(0, 20)) item_list.append(p) if i % 10000 == 0: Profile.objects.bulk_create(item_list) item_list = [] Profile.objects.bulk_create(item_list) print 'Users created\n' item_list = [] for i in range(0, int(options['tags'])): t = Tag(text=randomword(5)) item_list.append(t) if i % 10000 == 0: Tag.objects.bulk_create(item_list) item_list = [] Tag.objects.bulk_create(item_list) tm = Tag.objects.aggregate(Min('id'), Max('id')) print 'Tags created\n' for i in range(0, int(options['questions'])): q = Question(author_id=random.randint(um['id__min'], um['id__max']), title=randomword(20), text=randomword(10) + ' ' + randomword(20), rating=random.randint(-100, 100)) q.save() q.tags.add(random.randint(tm['id__min'], tm['id__max']), \ random.randint(tm['id__min'], tm['id__max']), \ random.randint(tm['id__min'], tm['id__max'])) qm = Question.objects.aggregate(Min('id'), Max('id')) print 'Questions created\n' item_list = [] for i in range(0, int(options['answers'])): a = Answer(author_id=random.randint(um['id__min'], um['id__max']), question_id=random.randint(qm['id__min'], qm['id__max']), is_right=random.randint(0, 1), text=randomword(10) + ' ' + randomword(10), rating=random.randint(-100, 100)) item_list.append(a) if i % 10000 == 0: Answer.objects.bulk_create(item_list) item_list = [] Answer.objects.bulk_create(item_list) am = Answer.objects.aggregate(Min('id'), Max('id')) print 'Answers created\n' item_list = [] for i in range(0, int(options['likes'])): item_type = random.choice(['question', 'answer']) if item_type == 'question': item = random.randint(qm['id__min'], qm['id__max']) else: item = random.randint(am['id__min'], am['id__max']) l = Like(author_id=random.randint(um['id__min'], um['id__max']), item_type=item_type, item=item, is_like=random.randint(0, 1)) item_list.append(l) if i % 20000 == 0: Like.objects.bulk_create(item_list) item_list = [] Like.objects.bulk_create(item_list) print 'Likes created\n'
def unsubscribe(tagname): Tag.get(tagname, "tag not found") user: User = get_user_from_app_context() user.unsubscribe_from_tag(tagname) return json_response({"data": user.tag_subscription.to_dict()})
def create_qusetion_answer_and_connect_user(how_many): does_NOT_exist_users = '' for j in range(how_many): ### User get random user_id = random.randint(1, 10362) try: u = User.objects.get(id=user_id) except: does_NOT_exist_users = does_NOT_exist_users + str(user_id) + ' ' user_id = random.randint(1, 10362) try: u = User.objects.get(id=user_id) except: does_NOT_exist_users = does_NOT_exist_users + str(user_id) + ' ' user_id = random.randint(1, 10362) u = User.objects.get(id=user_id) ### End User get random ### Tag create start_tag_lenght = 3 end_tag_lenght = 6 t_text = randomword(start_tag_lenght, end_tag_lenght) t = Tag(text = t_text) t.save() ### End Tag create ### Question create question_length_low = 8 question_length_high = 15 q_title = randomword(question_length_low, question_length_high) for i in range(random.randint(0, 2)): q_title = q_title + ' ' + randomword(question_length_low, question_length_high) q_title = q_title + '?' q_text = randomword(question_length_low, question_length_high) for i in range(random.randint(3, 20)): q_text = q_text + ' ' + randomword(question_length_low, question_length_high) q_rating = random.randint(-10, 150) q = Question(title = q_title, text = q_text, author = u, rating = q_rating ) q.save() q.tags.add(t) q.save() ### End Question create ### User get random ser_id = random.randint(1, 10362) try: u = User.objects.get(id=user_id) except: does_NOT_exist_users = does_NOT_exist_users + str(user_id) + ' ' user_id = random.randint(1, 10362) try: u = User.objects.get(id=user_id) except: does_NOT_exist_users = does_NOT_exist_users + str(user_id) + ' ' user_id = random.randint(1, 10362) u = User.objects.get(id=user_id) ### End User get random ### Answer create answer_length_low = 8 answer_length_high = 15 a_text = randomword(answer_length_low, answer_length_high) for i in range(random.randint(7, 25)): a_text = a_text + ' ' + randomword(answer_length_low, answer_length_high) a_right_answer = True a = Answer(question = q, text = a_text, right_answer = a_right_answer, author = u ) a.save() ### End asnwer create print('Create: ' + str(j)) print('Create: ' + str(how_many)) print(does_NOT_exist_users)
def show(tagname): tag = Tag.get(tagname, "tag not found") return json_response({"data": tag.to_dict()})
def rt_sync_tags(task: SyncTagsTask): from ask.models import Tag Tag.sync(task.tags) ctx.log.info("tags %s synced", task.tags)
def handle(self, *args, **options): name = options['name'] tag = Tag(name=name) tag.save()