예제 #1
0
 def add_account(self, email, password):
     account = self.model(email = email, password = password) #创建account实例
     account.code = get_random_string(32)
     account.code_expire_date = timezone.now() + datetime.timedelta(days = self.CODE_EXPIRE_DAYS)
     account.save() #将实例写入数据库
     #TODO create user
     return account.code
예제 #2
0
def resend_email_token(request):
    email_verify = get_object_or_none(EmailVerify, pk=request.user)
    if email_verify:
        if email_verify.was_verified_email():
            return redirect('user:info')
        email_token = email_verify.token
    else:
        email_token = get_random_string(32)
        EmailVerify(user = request.user, token = email_token).save()
    send_mail_verification_code(request.user.email, email_token)
    return redirect('user:info')
예제 #3
0
def user_register(request):
    form = RegistrationForm()
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = form.save()
            email_token = get_random_string(32)
            email_verify = EmailVerify(user = user, token = email_token)
            email_verify.save()
            send_mail_verification_code(form.cleaned_data['email'], email_token)
            send_notification_by_system(user, 'Chào mừng đến với Chợ Sách.')
            login(request, user)
            return redirect('home:index')
    return render(request, 'user/register.html', {'form': form})
예제 #4
0
    def run(self):
        for i in range(self.count):
            initial_dt = dt.now()
            send(self.conn, get_random_string(PACKAGE_LEN))
            receive(self.conn, ACK_LEN)
            final_dt = dt.now()

            delta = final_dt - initial_dt
            if delta.seconds >= TIMEOUT_SECONDS:
                send(self.conn, str(DISCARDED_PCK_RTT))
                continue

            delta = round(
                (delta.seconds + delta.microseconds / 1000000.0) * 1000, 1)
            delta = "{:07.1f}".format(delta)
            send(self.conn, delta)
        self.conn.close()
예제 #5
0
    def run_direct_ping(self):
        """
        Executes direct ping count times and prints output
        """

        # handshaking
        send(self.sock, DIRECT_PING)
        rcv = receive(self.sock, ACK_LEN)
        if rcv != ACK_MSG:
            raise ValueError(
                str.format("Expected %s, received %s", (ACK_MSG, rcv)))

        self.out_mgr.print_file_version()
        self.out_mgr.print_operation(True, False, False)
        self.out_mgr.print_server(self.dest_address)
        self.out_mgr.print_client(self.address)

        # direct ping
        packet_loss = 0
        rtt_list = []
        for i in range(self.count):
            before = dt.now()
            send(self.sock, get_random_string(PACKAGE_LEN))
            rcv = receive(self.sock, ACK_LEN)
            delta = dt.now() - before

            if rcv != ACK_MSG:
                raise ValueError(f"Expected {ACK_MSG}, received {rcv}")

            delta = round(
                (delta.seconds + delta.microseconds / 1000000.0) * 1000, 1)

            if delta >= (TIMEOUT_SECONDS * 1000):
                packet_loss += 1
                continue

            rtt_list.append(delta)
            if self.verbose:
                self.out_mgr.print_latest_message(PACKAGE_LEN,
                                                  self.dest_address, i + 1,
                                                  delta)

        self.out_mgr.print_statistics(self.dest_address, self.count,
                                      self.count - packet_loss, rtt_list)
예제 #6
0
    def run(self):
        for i in range(self.count):

            initial_dt = dt.now()

            try:
                send(self.proxy_conn, get_random_string(PACKAGE_LEN))
                receive(self.proxy_conn, ACK_LEN)
            except ConnectionClosedException as e:
                send(self.client_conn, ERR_MSG)
                raise ConnectionClosedException(str(e))

            final_dt = dt.now()
            send(self.client_conn, ACK_MSG)

            delta = final_dt - initial_dt
            if delta.seconds >= TIMEOUT_SECONDS:
                send(self.client_conn, str(DISCARDED_PCK_RTT))
                continue

            delta = round(
                (delta.seconds + delta.microseconds / 1000000.0) * 1000, 1)
            delta = "{:07.1f}".format(delta)
            send(self.client_conn, delta)
예제 #7
0
 def retrieve(self):
     self.code = get_random_string(32)
     self.code_expire_date = timezone.now() + datetime.timedelta(days = self.CODE_EXPIRE_DAYS)
     self.save()
     return self.code