Пример #1
0
def index(request):
    import re

    deploy = False
    #deploy = True

    if request.user.is_authenticated():
        currentProfile = Profile.getProfile(request.user)
        
        #if logger.isEnabledFor(logging.INFO):
            #logger.info('AUTHORIZED user(%s) from IP: %s, HOST: %s' % (request.user, request.META['REMOTE_ADDR'],  request.META['HTTP_HOST']))


        role = currentProfile.role
        # if role == 1 - Admin
        if role == '1':
            main_css_path = ''
            return render(request, 'admin_app/index.html', {'role': role, 'main_css_path': main_css_path,  'username':request.user.username, 'user_id':request.user.id}, content_type="text/html")
        # role = 5 - Vendor Salesman
        if role == '5':
            main_css_path = 'vs_resources/'
            return render(request, 'vsalesman_app/index.html', {'profile': currentProfile, 'role': role, 'main_css_path': main_css_path, 'email': request.user.email, 'username':request.user.username, 'user_id':request.user.id}, content_type="text/html")
        if role == '2' or role == '3' or role == '4': # role = 4 - Vendor
            try:
                s = Settings.objects.get(user=currentProfile.user)
            except ObjectDoesNotExist, err:
                s = Settings.createDefaultSettings(currentProfile.user)

            main_css_path = 'mobile_resources/'
            if re.search( r'iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm|silk', request.META['HTTP_USER_AGENT'].lower(), re.I):
                return render(request, 'touch/index.html', {'profile': currentProfile,'settings': Settings.objects.get(user=request.user)}, content_type="text/html")
            else:
                main_css_path = ''
                return render(request, 'cust_app/index.html', {'profile': currentProfile,'settings': s,'role': role, 'main_css_path': main_css_path, 'email': request.user.email, 'username':request.user.username, 'user_id':request.user.id}, content_type="text/html")
Пример #2
0
def createProfile(parent_user = None, params = None):
    if params == None:
        logger.error('No params specified')
        raise Exception('No params specified')

    try:
        # check required param company_name
        if 'p_company_name' not in params:
            raise Exception('Company Name is Required')
        else:
            company_name = replace(params['p_company_name']).strip()
            if company_name == '':
                raise Exception('Company Name is Required')
                
        if parent_user == None:
            """Create User"""
            # check not required param username
            if 'username' not in params:
                username = company_name.strip(' ') + '_' + str(random.randint(1,9999))
            else:
                username = replace(params['username']).strip(' ') # remove all spaces, even between words
                if username == '':
                    username = company_name.replace(" ", "") # remove all spaces, even between words
                    username = username + '_' + str(random.randint(1,9999))

            # check param password
            if 'password' not in params:
                password = '******' # 'password'
            else:
                password = replace(params['password']).strip()
                if password == None or password.strip() == '':
                    password = '******'


            if User.objects.filter(username=username).exists():
                raise Exception("User with User Name: '" + username + "' already Exist, Please choose another User Name")

            user = User.objects.create(username=username, password=password, date_joined=datetime.now())




        """Create Profile"""
        if parent_user == None:
            parent_user = user
        else:
            user = parent_user

        # check not requried params
        if 'email' not in params: email = ''
        else: email = replace(params['email']).strip()

        if 'description' not in params: description = ''
        else: description = replace(params['description']).strip()

        if 'first_name' not in params: first_name = ''
        else: first_name = replace(params['first_name']).strip()

        if 'last_name' not in params: last_name = ''
        else: last_name = replace(params['last_name']).strip()

        if 'phone_num_1' not in params: phone_num_1 = ''
        else: phone_num_1 = replace(params['phone_num_1']).strip()

        if 'phone_num_2' not in params: phone_num_2 = ''
        else: phone_num_2 = replace(params['phone_num_2']).strip()

        if 'web_page' not in params: web_page = ''
        else: web_page = replace(params['web_page']).strip()

        if 'fax_num' not in params: fax_num = ''
        else: fax_num = replace(params['fax_num']).strip()

        if 'address_1' not in params: address_1 = ''
        else: address_1 = replace(params['address_1']).strip()

        if 'address_2' not in params: address_2 = ''
        else: address_2 = replace(params['address_2']).strip()

        if 'address_3' not in params: address_3 = ''
        else: address_3 = replace(params['address_3']).strip()

        if 'address_4' not in params: address_4 = ''
        else: address_4 = replace(params['address_4']).strip()

        if 'address_5' not in params: address_5 = ''
        else: address_5 = replace(params['address_5']).strip()

        if 'mobile_phone_num' not in params: mobile_phone_num = ''
        else: mobile_phone_num = replace(params['mobile_phone_num']).strip()

        if 'notes' not in params: notes = ''
        else: notes = replace(params['notes']).strip()

        if 'category' not in params: category = ''
        else: category = replace(params['category']).strip()

        # check required param role
        if 'role' not in params:
            raise Exception('role is Required')
        else:
            role = replace(params['role']).strip()
            if role.strip() == '':
                raise Exception('role is Required')

        # create default Settings for this profile
        settings = Settings.createDefaultSettings(user)

        newProfile = Profile(                       \
            user = user                             \
            ,parent_user = parent_user              \
            ,settings = settings                    \
            ,role = role                            \
            ,company_name = company_name            \
            ,email = email                          \
            ,description = description              \
            ,first_name = first_name                \
            ,last_name = last_name                  \
            ,phone_num_1 = phone_num_1              \
            ,phone_num_2 = phone_num_2              \
            ,web_page = web_page                    \
            ,fax_num = fax_num                      \
            ,address_1 = address_1                  \
            ,address_2 = address_2                  \
            ,address_3 = address_3                  \
            ,address_4 = address_4                  \
            ,address_5 = address_5                  \
            ,mobile_phone_num = mobile_phone_num    \
            ,notes = notes                          \
            ,category = category                    \
        )

        newProfile.save()


        return newProfile

    except Exception, err:
        logger.error(err)
        raise err # throw error farther to caller
Пример #3
0
                        # name attribute is the only one that we save to Vendor Table
                        if 'name' in attrs:
                            vendor.name = replace(attrs['name'])
                            vendor.save()
                            
                    except Exception, err:
                        logger.error(err)
                        ready_data_false['error'] = err
                        return ready_data_false

                    try:
                        # vendor was updated, need to change settings vendors_date_modified field
                        s = Settings.objects.get(user=currentProfile.parent_user)
                    except ObjectDoesNotExist, err:
                        s = Settings.createDefaultSettings(currentProfile.parent_user)
    
                    s.vendors_date_modified  = datetime.now()
                    s.time_modified          = str(time.time())
                    s.save()

                     # success
                    return self.read(request, id)

                else:
                    reason.append(u'Vendor ID is required')
                    logger.error(reason)
                    ready_data_false['errors'] = reason
                    return ready_data_false
            except Exception, err:
                logger.error(err)
Пример #4
0
    def create(self, request):
        # this method only for user with role ADMIN

        ready_data_true =  {'success': True }
        ready_data_false = {'success': False}

        if not request.user.is_authenticated():
            #return rc.FORBIDDEN
            ready_data_false["error"] = "Authorization is failed"
            return ready_data_false

        try:
            # let's check if the user is Admin, nobody else is allowed to create profiles
            currentProfile = Profile.getProfile(request.user)
            if currentProfile.get_role_display() != 'Admin':
                ready_data_false['error'] = 'Not Allowed'
                return ready_data_false

            ext_posted_data = simplejson.loads(request.POST.get('items'))
            attrs = self.flatten_dict(ext_posted_data)

            try:
                del attrs['id']
            except KeyError:
                pass

            reason=[]
            # first need to create user
            #user = User.objects.get(id=148)

            userName = attrs['username']
            userPass = attrs['password']
            userMail = attrs['email']
            del attrs['username']
            del attrs['password']

            if userName and userPass and userMail:
                user,created = User.objects.get_or_create(username=userName, email=userMail)
                if created:
                    # user was created
                    # set the password here
                    user.userPass = userPass
                    user.save()
                else:
                    # user was retrieved
                    pass
            else:
                # request was empty
                ready_data_false['error'] = 'Username, Password or Email fields are missing'
                return ready_data_false

            if user == None:
                ready_data_false['error'] = 'User is not created'
                return ready_data_false


            profile,created = self.model.objects.get_or_create( \
                account_num         = attrs['account_num'],     \
                address_1           = attrs['address_1'],       \
                address_2           = attrs['address_2'],       \
                address_3           = attrs['address_3'],       \
                address_4           = attrs['address_4'],       \
                address_5           = attrs['address_5'],       \
                category            = attrs['category'],        \
                company_name        = attrs['company_name'],    \
                description         = attrs['description'],     \
                email               = attrs['email'],           \
                fax_num             = attrs['fax_num'],         \
                first_name          = attrs['first_name'],      \
                last_name           = attrs['last_name'],       \
                mobile_phone_num    = attrs['mobile_phone_num'],\
                notes               = attrs['notes'],           \
                phone_num_1         = attrs['phone_num_1'],     \
                phone_num_2         = attrs['phone_num_2'],     \
                role                = attrs['role'],            \
                web_page            = attrs['web_page'],        \
                user                = user,                     \
                parent_user         = user                      \
                )

            # create Settings
            try:
                s = Settings.objects.get(user=user)
            except ObjectDoesNotExist, err:
                Settings.createDefaultSettings(user)


            ready_data_true['items'] = profile.list()
            return ready_data_true
Пример #5
0
                ready_data_false['error'] = 'Not Allowed'
                return ready_data_false
            try:
                ready_data_true['data'] =  self.model.objects.get(pk=id).list()
                return ready_data_true
            except Exception, err:
                ready_data_false['errors'] = err
                return ready_data_false

        else:
            try:
                ready_data_true['data'] =  Settings.objects.get(user=request.user).list()
                return ready_data_true

            except ObjectDoesNotExist:
                ready_data_true['data'] = Settings.createDefaultSettings(request.user).list()
                return ready_data_true
            except Exception, err:
                ready_data_false['errors'] = err
                return ready_data_false


    def update(self, request, id=None):

        ready_data_true =  {'success': True }
        ready_data_false = {'success': False}

        ready_data_false['error'] = 'update is not implemented yet'
        return ready_data_false