Esempio n. 1
0
 def authenticate(self, fb_user=None):
     try:
         profile = UserProfile.objects.get(fb_username=fb_user.username)
         profile.user.backend = \
                     'ohibokapatterns.common.utils.FacebookBackend'
         return profile.user
     except UserProfile.DoesNotExist:
         # Create a new user. Note that we can set password
         # to anything, because it won't be checked; the password
         # from settings.py will.
         username = fb_user.username
         users = User.objects.filter(username__startswith=username + '-').order_by('id').reverse()
         if not users:
             users = User.objects.filter(username=username).order_by('id').reverse()
             if users:
                 username += '-1'
         else:
             users = users[0]
             username += '-' + str(int(users.url.split('-')[1]) + 1)
         user = User(username=username, password=hashlib.sha224(username + \
                                             str(date.today())).hexdigest())
         user.is_staff = False
         user.is_superuser = False
         user.save()
         userprofile = UserProfile(fb_username=fb_user.username, fb_name=fb_user.name, user=user)
         userprofile.save()
         user.backend = 'ohibokapatterns.common.utils.FacebookBackend'
         return user
Esempio n. 2
0
    def setUpTestData(cls):
        cls.user: User = User.objects.create_user("test", "*****@*****.**",
                                                  "password")
        cls.user.is_staff = True
        cls.user.save()
        user_profile: UserProfile = UserProfile(user=cls.user)
        user_profile.save()
        teacher: Teacher = Teacher.objects.create(user=user_profile,
                                                  new_user=cls.user,
                                                  title="Mx")
        teacher.save()
        cls.klass, _, _ = create_class_directly(cls.user.email)
        cls.klass.save()
        cls.worksheet = Worksheet.objects.create(name="test worksheet",
                                                 starter_code="test code")
        cls.game = models.Game(id=1,
                               name="test",
                               game_class=cls.klass,
                               worksheet=cls.worksheet)
        cls.game.save()

        cls.EXPECTED_GAME_DETAIL = {
            "era": "1",
            "name": "test",
            "status": "r",
            "settings":
            '{"GENERATOR": "Main", "OBSTACLE_RATIO": 0.1, "PICKUP_SPAWN_CHANCE": 0.1, "SCORE_DESPAWN_CHANCE": 0.05, "START_HEIGHT": 31, "START_WIDTH": 31, "TARGET_NUM_CELLS_PER_AVATAR": 16.0, "TARGET_NUM_PICKUPS_PER_AVATAR": 1.2, "TARGET_NUM_SCORE_LOCATIONS_PER_AVATAR": 0.5}',
            "worksheet_id": str(cls.worksheet.id),
        }
        cls.EXPECTED_GAME_LIST = {
            "1": cls.EXPECTED_GAME_DETAIL,
            "2": cls.EXPECTED_GAME_DETAIL
        }
Esempio n. 3
0
    def save(self, user):
        user.first_name = self.cleaned_data["first_name"]
        user.last_name = self.cleaned_data["last_name"]
        # this will only be done one time, so we need to create a user profile
        profile = UserProfile()
        profile.user = user
        profile.occupation = self.cleaned_data["occupation"]
        profile.reason_of_visit = self.cleaned_data["purpose_of_visit"]
        profile.country_of_residence = self.cleaned_data["country_of_residence"]

        profile.save()

        user.save()
Esempio n. 4
0
    def test_delete_games(self):
        # Create a new teacher with a game to make sure it's not affected
        new_user: User = User.objects.create_user("test2", "*****@*****.**",
                                                  "password")
        new_user.is_staff = True
        new_user.save()
        new_user_profile: UserProfile = UserProfile(user=new_user)
        new_user_profile.save()
        new_teacher: Teacher = Teacher.objects.create(user=new_user_profile,
                                                      new_user=new_user)
        new_teacher.save()
        new_klass, _, _ = create_class_directly(new_user.email)
        new_user.save()
        new_game = models.Game(name="test2",
                               game_class=new_klass,
                               worksheet=self.worksheet)
        new_game.save()

        # Create a game for the second class
        game2 = models.Game(name="test",
                            game_class=self.klass2,
                            worksheet=self.worksheet)
        game2.save()

        data = {"game_ids": [self.game.id, game2.id, new_game.id]}

        # Try to login as a student and delete games - they shouldn't have access
        _, student_password, student = create_school_student_directly(
            self.klass.access_code)
        client = self.login(username=student.new_user.username,
                            password=student_password)
        response = client.post(reverse("game-delete-games"), data)
        assert response.status_code == 403
        assert Game.objects.count() == 3

        # Login as initial teacher and delete games - only his games should be deleted
        client = self.login()
        response = client.post(reverse("game-delete-games"), data)
        assert response.status_code == 204
        assert Game.objects.count() == 1
        assert Game.objects.get(pk=new_game.id)
Esempio n. 5
0
    def post(self, request):
        register_form = RegisterForm(request.POST)
        if register_form.is_valid():
            user_name = request.POST.get("email", "")
            if UserProfile.objects.filter(email=user_name):
                return render(request, "users/register.html", {
                    "register_form": register_form,
                    "msg": "该账户已被注册"
                })
            pass_word = request.POST.get("password", "")
            user_profile = UserProfile()
            user_profile.username = user_name
            user_profile.email = user_name
            user_profile.is_active = False
            user_profile.password = make_password(pass_word)
            user_profile.save()

            # send_register_email(user_name, 'register')
            send_register_email(email=user_name, send_type=0)
            return render(request, "users/login.html")
            # return  redirect('/')
        else:
            return render(request, "users/register.html",
                          {"register_form": register_form})
Esempio n. 6
0
 def setUpTestData(cls):
     cls.user: User = User.objects.create_user("test", "*****@*****.**",
                                               "password")
     cls.user.is_staff = True
     cls.user.save()
     user_profile: UserProfile = UserProfile(user=cls.user)
     user_profile.save()
     teacher: Teacher = Teacher.objects.create(user=user_profile,
                                               new_user=cls.user)
     teacher.save()
     cls.klass, _, _ = create_class_directly(cls.user.email)
     cls.klass.save()
     cls.klass2, _, _ = create_class_directly(cls.user.email)
     cls.klass2.save()
     cls.worksheet: Worksheet = Worksheet.objects.create(
         name="test worksheet", starter_code="test code 1")
     cls.worksheet2: Worksheet = Worksheet.objects.create(
         name="test worksheet 2", starter_code="test code 2")
     cls.game = models.Game(id=1,
                            name="test",
                            game_class=cls.klass,
                            worksheet=cls.worksheet)
     cls.game.save()
Esempio n. 7
0
        register_password = body[u'registerPassword'].encode('utf-8')
        register_code = body[u'registerCode'].encode('utf-8')
    except Exception, e:
        return render(request, 'user/ErrorUrl.html', {'error': '此地址异常'})
    try:
        mail_activate_info = {
            'email': code_encrypt(register_email),
            'verify_code': code_encrypt(random_str(4))
        }
        url = url_covenrt(request=request,
                          dict=mail_activate_info,
                          path='users/email_activate')
        password = make_password(register_password, None, 'pbkdf2_sha256')
        user = UserProfile(email=register_email,
                           password=password,
                           username=register_username,
                           is_active=False,
                           is_staff=False)
        user.email_user('激活你的用户', '请点击链接:' + url, '*****@*****.**')
        request.session['email_activate'] = mail_activate_info['email']
        request.session['verify_code_activate'] = mail_activate_info[
            'verify_code']
        user.save()
        msg['isSuccess'] = True
    except Exception, e:
        msg['isSuccess'] = False
    return HttpResponse(json.dumps(msg), content_type="application/json")


#注册成功或注册邮箱发送后回执
def registerorretrieve_sucess(request, param):