def handle(self, *args, **kwargs): east_blocks = [ 'PAKYONG', 'GANGTOK', 'REGU', 'RHENOCK', 'KHAMDONG', 'RAKDONG TINTEK', 'DUGA', 'RANKA', 'MARTAM', 'PARAKHA' ] west_blocks = [ 'GYALSHING', 'SORENG', 'KALUK', 'DENTAM', 'DARAMDIN', 'YUKSAM', 'BERMIOK MARTAM', 'CHONGRANG', 'CHUMBONG' ] north_blocks = ['MANGAN', 'CHUNGTHANG', 'KABI', 'PASSINGDONG'] south_blocks = [ 'YANGANG', 'SUMBUK', 'NAMCHI', 'RAVANGLA', 'TEMI', 'JORETHANG', 'SIKKIP', 'NAMTHANG' ] districts = ['east', 'west', 'north', 'south'] for district in districts: user_obj = User() user_obj.username = district + '_admin' user_obj.set_password('Pass@123') user_obj.is_district_admin = True user_obj.save() district_obj = District.objects.create( user=user_obj, district_name=district.upper() + ' SIKKIM') for block in eval(district + '_blocks'): b_user_obj = User() b_user_obj.username = block.lower() + '_admin' b_user_obj.set_password('Pass@123') b_user_obj.is_block_admin = True b_user_obj.save() block_obj = Block.objects.create(user=b_user_obj, block_name=block, block_district=district_obj) print("Done")
def test_account_changepassword(self): u = User(email='*****@*****.**') u.is_active = True u.set_password('Myunittest1!') u.save() self.client.force_login(u) response = self.client.get(reverse('profiles:user_change_password')) self.assertEqual(response.status_code, HTTPStatus.OK) response = self.client.post(reverse('profiles:user_change_password'), data={ 'old_password': '******', 'new_password1': 'myunittest1!passchange', 'new_password2': 'myunittest1!passchange', }) self.assertEqual(response.status_code, HTTPStatus.OK) self.assertIn('old_password', response.context['form'].errors) response = self.client.post(reverse('profiles:user_change_password'), data={ 'old_password': '******', 'new_password1': 'Myunittest1!passchange', 'new_password2': 'Myunittest1!passchange', }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:user'))
def test_delete(self): u = User(email='*****@*****.**') u.is_active = True u.save() p = Profile(contact_email='*****@*****.**', user=u) p.save() r = Recommendation(profile=p, reviewer_email='*****@*****.**') r.save() self.client.force_login(u) response = self.client.get(reverse('profiles:user_delete')) self.assertEqual(response.status_code, HTTPStatus.OK) User.objects.get(id=u.id) response = self.client.post(reverse('profiles:user_delete'), data={ 'confirm': True, }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:login')) with self.assertRaises(User.DoesNotExist): User.objects.get(id=u.id) with self.assertRaises(Profile.DoesNotExist): Profile.objects.get(id=p.id) Recommendation.objects.get(id=r.id)
def test_account_edit(self): u = User(email='*****@*****.**') u.username = '******' u.name = 'Unit Test' u.is_active = True u.set_password('Myunitarytest1!') u.save() self.client.force_login(u) with self.settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend'): response = self.client.post(reverse('profiles:user_edit'), { 'username': u.username, 'name': 'Another Unit Test', 'email': u.email, }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:user')) self.assertEqual(len(mail.outbox), 1) u.refresh_from_db() self.assertTrue(u.is_active) self.assertEqual(u.name, 'Another Unit Test')
def test_profile_delete(self): u = User(email='*****@*****.**') u.is_active = True u.save() p = Profile(contact_email='*****@*****.**', user=u) p.save() r = Recommendation(profile=p, reviewer_email='*****@*****.**') r.save() self.client.force_login(u) response = self.client.get(reverse('profiles:user_profile_delete')) self.assertEqual(response.status_code, HTTPStatus.OK) User.objects.get(id=u.id) response = self.client.post(reverse('profiles:user_profile_delete'), data={ 'confirm': True, }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:user')) p = Profile.all_objects.get(id=p.id) self.assertNotEquals(p.deleted_at, None)
def test_account_page(self): u = User(email='*****@*****.**') u.is_active = True u.save() self.client.force_login(u) response = self.client.get(reverse('profiles:user')) self.assertEqual(response.status_code, HTTPStatus.OK) self.assertContains(response, '*****@*****.**')
def create_user(self): user = User( first_name=chance.first(gender='m', language='ru'), last_name=chance.last(gender='m', language='ru'), email=chance.email(), is_active=True, is_email_confirmed=True, ) user.set_password("@120804a") user.save() self.stdout.write('Successfully created user (%s)' % (user.email, )) return user
def create_user(self): user = User( first_name="Davit", last_name="Vardanyan", email="*****@*****.**", is_active=True, is_email_confirmed=True, is_superuser=True, is_staff=True, ) user.set_password("@120804a") user.save() self.stdout.write('Successfully created user (%s)' % (user.email, )) return user
def test_account_login(self): u = User(email='*****@*****.**') u.is_active = True u.set_password('Myunitarytest1!') u.save() next = reverse('profiles:user_edit') response = self.client.get(reverse('profiles:login') + '?next=' + next) response = self.client.post(reverse('profiles:login'), { 'username': '******', 'password': '******', }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, next)
def test_account_edit_email(self): u = User(email='*****@*****.**') u.username = '******' u.name = 'Unit Test' u.is_active = True u.set_password('Myunitarytest1!') u.save() self.client.force_login(u) with self.settings( EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend'): response = self.client.post(reverse('profiles:user_edit'), { 'username': u.username, 'name': 'Another Unit Test', 'email': '*****@*****.**', }, follow=True) self.assertRedirects( response, reverse('profiles:login') + '?next=' + reverse('profiles:user')) self.assertEqual(response.status_code, HTTPStatus.OK) self.assertEqual(len(mail.outbox), 2) self.assertEqual(mail.outbox[0].to, ['*****@*****.**']) self.assertEqual(mail.outbox[1].to, ['*****@*****.**']) u.refresh_from_db() self.assertFalse(u.is_active) self.assertEqual(u.name, 'Another Unit Test') self.assertEqual(u.email, '*****@*****.**') token = mail.outbox[0].context['token'] self.assertTrue('user_confirmation_token' in self.client.session) self.assertEqual(self.client.session['user_confirmation_token'], token) response = self.client.get(reverse('profiles:signup_confirm'), data={ 'token': token, }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:user'))
def save(self, commit=True): employee = super().save(commit=False) user = User() user.username = f'emp_{randint(1000, 9999)}' user.set_password('Pass@123') user.first_name = self.cleaned_data['first_name'] user.middle_name = self.cleaned_data['middle_name'] user.last_name = self.cleaned_data['last_name'] user.date_of_birth = self.cleaned_data['date_of_birth'] user.gender = self.cleaned_data['gender'] user.is_employee = True user.is_teacher = self.cleaned_data['is_teacher'] user.save() employee.employee_user = user if commit: return employee.save() return employee
def home(request): try: matthew = User.objects.get(id=1) except User.DoesNotExist: matthew = User() try: latest_news = Article.objects.visible().latest() except Article.DoesNotExist: latest_news = {} try: latest = Production.objects.all().order_by('-id')[0] except IndexError: latest = {} try: latest_comment = Comment.objects.exclude( user=matthew).order_by('-id')[0] except IndexError: latest_comment = {} random_photo = Photo.objects.exclude( id__gte=149, id__lte=159).filter(content_type=22).order_by('?')[:1] if random.randint(1, 10) == 1: random_photo = None return render( request, 'home.html', { 'latest_production': latest, 'latest_observation': latest_comment, 'latest_news': latest_news, 'productions': Production.objects.count(), 'places': Place.objects.count(), 'plays': Play.objects.count(), 'people': Person.objects.count(), 'parts': Part.objects.count(), 'random_photo': random_photo, 'home': True, })
def test_profile_delete_new(self): u = User(email='*****@*****.**') u.is_active = True u.save() p = Profile(contact_email='*****@*****.**', user=u) p.save() self.client.force_login(u) response = self.client.get(reverse('profiles:user_profile_delete')) self.assertEqual(response.status_code, HTTPStatus.OK) User.objects.get(id=u.id) response = self.client.post(reverse('profiles:user_profile_delete'), data={ 'confirm': True, }) self.assertEqual(response.status_code, HTTPStatus.FOUND) self.assertEqual(response.url, reverse('profiles:user')) with self.assertRaises(Profile.DoesNotExist): p.refresh_from_db()
from profiles.models import User from contacts.models import Contact from companies.models import Company, Industry from emails.models import Email from hackathons.models import Hackathon, Tier, Perk, Sponsorship fake = Faker() print("Generating users...") users = [] for i in range(10): username = fake.user_name() user = User( first_name=fake.first_name(), last_name=fake.last_name(), email=fake.email(), username=username, password=username, ) user.save() users.append(user) print("Generating industries...") industries = [] colors = { "Web Dev": "blue", "ML": "green", "Software": "purple", "Systems": "orange", "Data": "yellow", }
def create_test_user(self, email=None, password=None): user = User(email=email or REGISTER_EMAIL) user.set_password(REGISTER_PASS or REGISTER_PASS) user.save() return user
created_at = post["fields"]["pub_date"] author_pk = post["fields"]["author"] # Find author by pk for user in users: if user["pk"] == author_pk: author_saved = user # Get author data username = author_saved["fields"]["username"] password = author_saved["fields"]["password"] karma = author_saved["fields"]["karma"] # Get or create author try: author = User.objects.get(username=username) except: author = User(username=username, password=password, karma=karma) author.save() series = None # Create a series for the chapter, with slug/title of a parent if post["fields"]["post_type"] == "chapter": # Find parent for p in posts: if p["pk"] == post["fields"]["parent"]: parent = p # Modify slug to parent-slug-chapter-slug. slug = parent["fields"]["slug"] + "-" + slug # Create series for a parent series_title = parent["fields"]["title"] series_slug = parent["fields"]["slug"]