Beispiel #1
0
def agents_add_process(request):
    """ Admin: Process add agent form"""
    args = _get_args(request)
    
    if not user_logged_in(request):
        return redirect_login()
    
    if request.POST.get('form_name') == 'add_agent':
        agent = Agent()
        agent.first_name = request.POST.get('first_name')
        agent.last_name = request.POST.get('last_name')
        agent.username = request.POST.get('username')
        agent.phone = request.POST.get('phone')
        agent.address = request.POST.get('address')
        
        try:
            agent.id_filename = util.handle_uploaded_file(request.FILES.get('id'))
            agent.sig_filename = util.handle_uploaded_file(request.FILES.get('sig'))
            password = "******"
            agent.password_salt = util.generate_uuid()
            agent.password = hashlib.md5(password + agent.password_salt) \
                                .hexdigest()
            agent.save()
            return redirect('/admin/manage/agents?msg=1')
        except ValueError, ex:
            return redirect('/admin/manage/agents/add?msg=' + str(ex))
Beispiel #2
0
def change_password_process(request):
    """
    Process change_password page
    """
    args = _get_args(request)
    
    if not user_logged_in(request):
        return redirect_login()
    
    if request.POST.get('form_name') == 'change_password':
        new_password = request.POST.get('password')
        confirm_new_password = request.POST.get('confirm_password')
        
        if new_password == confirm_new_password:
            agent = request.session.get('agent')
            password_salt = util.generate_uuid()
            password = hashlib.md5( new_password + password_salt) \
                                .hexdigest()
                                
            Agent.objects.filter(pk=agent.pk) \
                .update(password_salt=password_salt, password=password)
                
            return redirect('/agent/register/user?msg=3')
        
        else:
            return redirect('/agent/change_password?msg=4')
Beispiel #3
0
    def post(self):  # 测试通过
        """对应 /api/users -post
        :param req: 添加的用户数据
        :return: 添加后的用户数据包括id
        """
        req = request.json
        user_result = add_user(
            User(generate_uuid(), req.get('username'), req.get('name'),
                 req.get('password'),
                 req.get('email'), req.get('day_of_birth'), req.get('gender'),
                 req.get('academy'), req.get('major'), req.get('bio'),
                 req.get('phone'), req.get('year_of_enrollment')))
        user_dto = UsersDto(user_id=user_result.user_id,
                            username=user_result.username,
                            name=user_result.name,
                            email=user_result.email,
                            day_of_birth=user_result.day_of_birth,
                            gender=user_result.gender,
                            academy=user_result.academy,
                            major=user_result.major,
                            bio=user_result.bio,
                            phone=user_result.phone,
                            year_of_enrollment=user_result.year_of_enrollment,
                            club_ids=[],
                            request_ids=[])
        res = user_dto
        # res = flask.make_response(res)
        # res.headers['Access-Control-Allow-Origin'] = "*"
        # res.headers['Access-Control-Allow-Headers'] = "content-type, x-auth-token"
        # res.headers['Access-Control-Allow-Methods'] = "GET, PUT, POST, DELETE, OPTIONS, HEAD"

        return obj2json(res)
Beispiel #4
0
 def get_current_user_uuid(self, quizz_short_name):
     cookies = self.get_cookies()
     if cookies.has_key(quizz_short_name):
         return cookies.get(quizz_short_name)
     else:
         user_uuid = util.generate_uuid()
         self.set_cookie(quizz_short_name, user_uuid)
         return user_uuid
Beispiel #5
0
def convert_from_request_to_model(req_body: 'json', club_id: str = util.generate_uuid()) -> Club:
    """把请求中的json转为数据库操作需要的Club对象

    :param req_body: 请求中的json数据
    :param club_id: 社团Id
    :return: 填充数据的Club对象
    """
    data = json.loads(req_body)
    date_str = data.get("created_date")
    created_date = util.str2date(date_str) if date_str is not None else None
    return build_club(data.get("club_name"), data.get("club_bio"), data.get("member_number", -1), data.get("tags"),
                      data.get("addresses"), data.get("master_id"), created_date, club_id)
Beispiel #6
0
def build_image(club_id: str, origin_name: str) -> Image:
    """Build a new image according to the club id.

    :param club_id: the id of the club that owns the image.
    :param origin_name: the origin name of the image.
    :return: the instance of Image.
    """
    image = Image()
    image.club_id = club_id
    suffix = origin_name.split('.')[-1]
    image.image_id = util.generate_uuid()
    image_name = image.image_id + '.' + suffix
    image.image_path = DESTINATION_DIR + image_name
    return image
Beispiel #7
0
def build_club(club_name: str = None, club_bio: str = None, member_number: int = -1, tags: str = None,
               addresses: str = None, master_id: str = None, created_date: date = None,
               club_id: str = util.generate_uuid()) -> Club:
    """Build club.

    :return: club instance
    """
    club = Club()
    club.club_id = club_id
    util.add_attribute(club, "club_name", club_name)
    util.add_attribute(club, "club_bio", club_bio)
    util.add_attribute(club, "member_number", member_number, default_value=-1)
    util.add_attribute(club, "tags", tags)
    util.add_attribute(club, "addresses", addresses)
    util.add_attribute(club, "master_id", master_id)
    util.add_attribute(club, "created_date", created_date)
    return club
Beispiel #8
0
 def post(self, user_id):
     parser = reqparse.RequestParser()
     parser.add_argument('file',
                         type=werkzeug.datastructures.FileStorage,
                         location='files',
                         required=True,
                         help="图片不能为空")
     args = parser.parse_args()
     file = args.get('file')
     user = user_dao.get_user_by_id(user_id)
     suffix = file.filename.split('.')[-1]
     image_id = util.generate_uuid()
     image_name = image_id + '.' + suffix
     image_path = DESTINATION_DIR + image_name
     file.save(image_path)
     user.avatar_path = image_path
     user_dao.update_user(user)
     return {'upload', 'successful'}
Beispiel #9
0
 def post(self):  # 测试通过
     req = request.json
     request_result = add_request(Request(
         request_id=generate_uuid(),
         user_id=req.get('user_id'),
         club_id=req.get('club_id'),
         # time=time.time(),
         time=datetime.datetime.now(),
         extra=req.get('extra'),
         is_read=False
     ))
     request_dto = RequestDto(
         request_id=request_result.request_id,
         user_id=request_result.user_id,
         club_id=request_result.club_id,
         time=request_result.time,
         extra=request_result.extra,
         is_read=request_result.is_read,
     )
     res = request_dto
     return obj2json(res)
Beispiel #10
0
    def post(self):  # 测试通过
        '''对应 /api/users -post
        :param req: 添加的用户数据
        :return: 添加后的用户数据包括id
        '''
        req = request.json
        user_result = add_user(User(
            generate_uuid(),
            req.get('username'),
            req.get('name'),
            req.get('password'),
            req.get('email'),
            req.get('day_of_birth'),
            req.get('gender'),
            req.get('academy'),
            req.get('major'),
            req.get('bio'),
            req.get('phone'),
            req.get('year_of_enrollment')
        ))
        user_dto = UsersDto(
            user_id=user_result.user_id,
            username=user_result.username,
            name=user_result.name,
            email=user_result.email,
            day_of_birth=user_result.day_of_birth,
            gender=user_result.gender,
            academy=user_result.academy,
            major=user_result.major,
            bio=user_result.bio,
            phone=user_result.phone,
            year_of_enrollment=user_result.year_of_enrollment,
            club_ids=[],
            request_ids=[]
        )
        res = user_dto

        return obj2json(res)
Beispiel #11
0
def register_user_process(request):
    """
    Process user registration
    """
    args = _get_args(request)
    
    if not user_logged_in(request):
        return redirect_login()

    #check if form any field is empty
    __phone = request.POST.get('phone')
    __firstname = request.POST.get('firstname')
    __lastname = request.POST.get('lastname')
    __address = request.POST.get('address')

    if __phone == '' or __firstname == '' or \
            __lastname == '' or __address == '':
        return redirect('/agent/register/user?msg=8')
    
    if request.POST.get('form_name') == 'register_user':
        #check for already existing user
        try:
            _user = User.objects.get(phone=request.POST.get('phone'))
            if hashlib.md5("0000" + _user.pin_salt).hexdigest() == _user.pin:
                #user has not activate his account
                _user.first_name = request.POST.get('firstname')
                _user.last_name = request.POST.get('lastname')
                _user.address = request.POST.get('address')
                _user.phone = request.POST.get('phone')
                # send sms to new user
                msg = ("Please respond to this sms with your new PIN, in the format"
                       " 'register [new pin] [new pin]' to complete the registration.")
                sms = OutgoingMessage(receiver=_user.phone, body=msg,
                                      timestamp=time.time(), type='user_reg')
                messages = [sms]
                actions.send_sms(messages, use_render=False)
                return redirect('/agent/register/user?msg=5')
            else:
                # this account is already activated
                return redirect('/agent/register/user?msg=7')
        except User.DoesNotExist:
            user = User()
            user.first_name = request.POST.get('firstname')
            user.last_name = request.POST.get('lastname')
            user.address = request.POST.get('address')
            user.phone = request.POST.get('phone')
            
            default_pin = '0000'
            user.pin_salt = util.generate_uuid()
            user.pin = hashlib.md5(default_pin + user.pin_salt).hexdigest()
            
            # send sms to new user
            msg = ("Please respond to this sms with your new PIN, in the format"
                   "'register [new pin] [new pin]' to complete the registration.")
            sms = OutgoingMessage(receiver=user.phone, body=msg,
                                  timestamp=time.time(), type='user_reg')
            messages = [sms]
            actions.send_sms(messages, use_render=False)
            user.save()
            
            return redirect('/agent/register/user?msg=5')