Beispiel #1
0
def generate_users(cnt):
    names = [
        'john', 'peter', 'kolya', 'misha', 'pasha', 'ilja', 'trump', 'derek'
    ]
    snames = ['obama', 'trum', 'ivanov', 'prtrov', 'lomov', 'morgan']
    mails = ['ya', 'ma', 'go']
    nsize = len(names)
    ssize = len(snames)
    msize = len(mails)
    imgs = os.listdir('/home/hase/technopark/web/AskZaytsev/scripts/test_img')
    imgsc = len(imgs)
    for i in range(0, cnt):
        print(i)
        fname = names[random.randint(0, nsize - 1)]
        sname = snames[random.randint(0, ssize - 1)]
        email = names[random.randint(
            0, nsize - 1)] + '@' + mails[random.randint(0, msize - 1)] + '.ru'
        pwd = str(random.randint(10000, 1000000))
        u = User()
        u.username = fname + ' ' + sname + str(i)
        u.password = pwd
        u.email = email
        u.first_name = fname
        u.last_name = sname
        u.is_active = True
        u.is_superuser = False
        u.save()

        img = imgs[random.randint(0, imgsc - 1)]
        print(img)
        p = Profile(user=u, avatar=img)
        p.save()
        print(p.avatar)
Beispiel #2
0
def signup(request):
	if request.POST:
		form = UserForm(request.POST, request.FILES)
		if form.is_valid():
			password = form.cleaned_data.get('password')
			repeat_password = form.cleaned_data.get('repeat_password')
			if (password == repeat_password):
				username = form.cleaned_data.get('login')
				try:
					username = User.objects.get(username = username)
					return render(request, 'signup.html', { 'form' : form, 'user_error' : True })
				except User.DoesNotExist as error:	
					email = form.cleaned_data.get('email')
					last_name = form.cleaned_data.get('nickname')
					u = User.objects.create_user(username, email, password)
					u.last_name = last_name
					p = Profile(user = u, avatar = request.FILES.get('upload_avatar'))
					p.save()
					user = auth.authenticate(username=username, password=password)
					auth.login(request, user)
					return redirect('/')
		else:
			return render(request, 'signup.html', { 'form' : form })
	else:
		form = UserForm()
	return render(request, 'signup.html', { 'form' : form })
    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)
Beispiel #4
0
    def handle(self, *args, **options):
        login = options['login']
        email = options['email']
        nick = options['nick']
        passwd = options['passwd']

        user = User.objects.create_user(login, email, passwd, first_name=nick)

        profile = Profile(user=user)
        profile.save()
Beispiel #5
0
 def save(self, commit=True):
     user = super(RegistrationForm, self).save(commit=False)
     print(self.cleaned_data['username'])
     print(self.cleaned_data['first_name'])
     print(self.cleaned_data['password1'])
     print(self.cleaned_data['title'])
     user_profile = Profile(user=user, title=self.cleaned_data['title'])
     if commit:
         user.save()
         user_profile.save()
     return user, user_profile
Beispiel #6
0
 def save_user(self):
     if self.is_valid_():
         user = User.objects.create_user(
             username=self.cleaned_data.get('login'),
             email=self.cleaned_data.get('email'),
             first_name=self.cleaned_data.get('nickName'),
             password=self.cleaned_data.get('password1'),
         )
         user.avatar = self.cleaned_data.get('avatar')
         user.save()
         profile = Profile(user=user)
         profile.save()
         return True
     return False
Beispiel #7
0
def signup(request):
    if request.method == 'POST':
        form = SignupForm(request.POST, request.FILES)
        if form.is_valid():
            data = form.cleaned_data
            user = User.objects.create_user(data['nick'], data['email'],
                                            data['password'])
            p = Profile(user=user,
                        name=data['name'],
                        avatar=request.FILES['avatar'])
            p.save()
            user = authenticate(username=data['nick'],
                                password=data['password'])
            login(request, user)
            return redirect(reverse('index'))
    else:
        form = SignupForm()
    return render(request, 'signup.html', {'form': form})
Beispiel #8
0
    def handle(self, *args, **options):
        fake = Factory.create()
        fakeen = Factory.create('en_US')

        number = int(options['number'])

        for i in range(0, number):
            profile = fake.simple_profile()

            u = User.objects.create_user(profile['username'], profile['mail'],
                                         make_password('qwerty'))
            u.first_name = fakeen.first_name()
            u.last_name = fakeen.last_name()
            u.is_active = True
            u.is_superuser = False
            u.save()

            up = Profile()
            up.user = u
            up.save()
Beispiel #9
0
    def handle(self, *args, **options):
        fake = Faker('en_US')
        count = int(options['number'])
        added = 0

        for i in range(count):
            profile = fake.simple_profile()
            user = User.objects.create_user(profile['username'], profile['mail'], make_password('test'))
            user.first_name = fake.first_name()
            user.last_name = fake.last_name()
            user.is_active = True
            user.is_superuser = False
            user.save()

            user_profile = Profile()
            user_profile.user = user
            user_profile.save()

            added += 1

        print(added, " Users is added")
Beispiel #10
0
    def handle(self, *args, **options):
        fake = Factory.create()
        fakeen = Factory.create('en_US')

        number = int(options['number'])
        imgs = os.listdir(
            '/home/hase/technopark/web/AskZaytsev/scripts/test_img')
        #imgsc = len(imgs)
        for i in range(0, number):
            profile = fake.simple_profile()

            u = User.objects.create_user(profile['username'] + str(i + 5000),
                                         profile['mail'],
                                         make_password('qwerty'))
            u.first_name = fakeen.first_name()
            u.last_name = fakeen.last_name()
            u.is_active = True
            u.is_superuser = False
            u.save()

            up = Profile()
            up.user = u
            up.avatar = choice(imgs)
            #up.info = '%s [%s]' % (fakeen.company(), fakeen.catch_phrase())
            up.save()

            self.stdout.write('[%d] added user %s' % (u.id, u.username))
Beispiel #11
0
    def post(self, request):
        try:
            profile = Profile.objects.get(user=request.user)
        except Profile.DoesNotExist:
            profile = Profile(user=request.user)

        form = QuestionForm(data=request.POST, user=profile)
        if form.is_valid():
            form.save()
            form.save_m2m()
            return HttpResponseRedirect("../question/" +
                                        str(Question.objects.latest('id').id))
        return render(request, 'ask.html', {'form': form})
Beispiel #12
0
 def post(self, request, id):
     try:
         profile = Profile.objects.get(user=request.user)
     except Profile.DoesNotExist:
         profile = Profile(user=request.user)
     q = Question.objects.get(pk=id)
     answers = Answer.objects.filter(question=q)
     answers = paginate(answers, request)
     form = AnswerForm(data=request.POST, user=profile, id=id)
     if form.is_valid():
         form.save()
         return HttpResponseRedirect("../question/" + str(id))
     return render(request, 'questions.html', {
         "question": q,
         "answers": answers,
         'form': form
     })
Beispiel #13
0
    def post(self, request):
        try:
            profile = Profile.objects.get(user=request.user)
        except Profile.DoesNotExist:
            profile = Profile(user=request.user)

        avatar_url = ""
        if request.user.is_authenticated:
            p = Profile.objects.get(user=request.user)
            avatar_url = p.avatar_url()
        form = SettingsForm(request.POST, request.FILES, user=request.user)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/')
        return render(request, 'settings.html', {
            'form': form,
            'avatar': avatar_url
        })
Beispiel #14
0
    def save(self):
        data = self.cleaned_data
        password = data.get('password1')
        u = User()

        u.username = data.get('username')
        u.password = make_password(password)
        u.email = data.get('email')
        u.first_name = data.get('first_name')
        u.last_name = data.get('last_name')
        u.is_active = True
        u.is_superuser = False
        u.save()

        up = Profile()
        up.user = u
        up.info = data.get('info')
        if data.get('avatar') == None:
            up.avatar = "no-avatar.png"
        else:
            up.avatar = data.get('avatar')
        print(up.avatar)
        up.save()
        return authenticate(username=u.username, password=password)
Beispiel #15
0
    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'