def __init__(self, prefix: str, endpoint: str, entity_count: int, default_per_page: int = 12) -> "Pagination": """ Initiate pagination object with values. """ super().__init__() self.endpoint = endpoint self.entity_count = entity_count # Set prefix value names self.prefixed_page_index = '%sPageIndex' % prefix self.prefixed_per_page = '%sPerPage' % prefix # Get page_index and per_page from request self.page_index = blueprints.get_value(self.prefixed_page_index, int, 1) self.per_page = blueprints.get_value(self.prefixed_per_page, int, default_per_page) # Calculate page_count self.page_count = int(math.modf(entity_count / self.per_page)[1]) if self.page_count < self.entity_count / self.per_page: self.page_count = self.page_count + 1 # Check page_index and per_page validity if self.page_index < 1: self.page_index = 1 elif self.page_index > self.page_count: self.page_index = self.page_count if self.per_page < 1: self.per_page = 1 # Store arguments in session blueprints.set_value(self.prefixed_page_index, self.page_index) blueprints.set_value(self.prefixed_per_page, self.per_page)
def define_fields(self) -> None: """ Set form fields to values from request. """ if blueprints.get_value(self.prefix + 'FilterReset', bool, False): for field in self: if field.name != 'csrf_token': if type(field) is StringField or type( field) is BooleanField: field.data = None blueprints.set_value(self.prefix + field.label.text, None) elif type(field) is SelectField: field.data = field.choices[0][0] blueprints.set_value(self.prefix + field.label.text, field.choices[0][0]) else: for field in self: if field.name != 'csrf_token': data = field.data if type(field) is StringField: field.data = \ blueprints.get_value(self.prefix + field.label.text, str, None) elif type(field) is SelectField: field.data = \ blueprints.get_value( self.prefix + field.label.text, str, field.choices[0][0] \ if field.choices[0][0] != '' else None ) elif type(field) is BooleanField: field.data = \ blueprints.get_value(self.prefix + field.label.text, bool, False)
def sign_in(): """ Return sign in page. """ if current_user.is_authenticated: return redirect(url_for('base.get_home')) form = SignInForm() if form.validate_on_submit() and form.pin.data is not None: if form.password.data is None: password = IdenticaManager.get_password(form.pin.data) if password is None: return redirect(url_for('base.sign_in')) form.password.data = password else: verify_data = IdenticaManager.verify_pin(form.pin.data) if verify_data is None: return {'redirect': url_for('base.sign_in')} elif verify_data.get('from'): user = UserStore.get_or_create_user( verify_data['from']['id'], verify_data['from'].get('first_name'), verify_data['from'].get('last_name'), verify_data['from'].get('username')) login_user(SignedInUser(user), remember=True) logging.debug('Sign in as user %s (%s)' % \ (' '.join([user.first_name, user.last_name]), user.from_id)) blueprints.set_value('mobile', form.touch.data == 'true') return {'redirect': url_for('base.get_home')} return {'wait': True} with open('source/static/face.txt', 'r') as file: ascii_img = file.read().split('\n') return render_template('base/sign_in.html', form=form, ascii_img=ascii_img)
def reset_fields(self) -> None: """ Reset all fields values to None. """ for field in self: if field.name != 'csrf_token' and field.name != 'form_name': field.data = None blueprints.set_value(field.label.text, field.data)
def authenticate(self) -> bool: """ Authenticate user by form data and return True on success. """ if self.url_token.data is not None: verify_data = IdenticaPlugin.verify_url(self.url_token.data) if verify_data is not None and verify_data.get('from'): user = UserStore().read_or_create_user( verify_data['from']['id'], verify_data['from'].get('first_name'), verify_data['from'].get('last_name'), verify_data['from'].get('username')) login_user(SignedInUser(user), remember=True) logging.debug('Sign in as user %s (%s)' % \ ( ' '.join([str(user.first_name), str(user.last_name)]), user.from_id ) ) blueprints.set_value('mobile', self.has_touch_screen.data == 'true') return redirect(url_for('root.get_home')) elif self.pin.data is not None: if self.password.data is None: password = IdenticaPlugin.get_password(self.pin.data) if password is None: return redirect(url_for('root.get_home')) self.password.data = password else: verify_data = IdenticaPlugin.verify_pin(self.pin.data) if verify_data is None: return {'redirect': url_for('root.get_home')} elif verify_data.get('from'): user = UserStore().read_or_create_user( verify_data['from']['id'], verify_data['from'].get('first_name'), verify_data['from'].get('last_name'), verify_data['from'].get('username')) login_user(SignedInUser(user), remember=True) logging.debug('Sign in as user %s (%s)' % \ ( ' '.join([str(user.first_name), str(user.last_name)]), user.from_id ) ) blueprints.set_value('mobile', self.has_touch_screen.data == 'true') return {'redirect': url_for('root.get_home')} return {'wait': True} return # Return None to render template
def store_fields(self) -> None: """ Set form fields to values from request. """ for field in self: if type(field) is StringField or type(field) is BooleanField: blueprints.set_value(self.prefix + field.label.text, field.data) elif type(field) is SelectField: blueprints.set_value( self.prefix + field.label.text, field.data \ if field.data is not None and \ field.data != field.choices[0] else None )
def __init__(self) -> "PollFilter": """ Initiate object with values from request. """ super(PollFilter, self).__init__('pollFilter') if request.method == 'GET': self.title.data = blueprints.get_value(self.title.label.text, str, None) self.description.data = blueprints.get_value( self.description.label.text, str, None) elif request.method == 'POST': blueprints.set_value(self.title.label.text, self.title.data) blueprints.set_value(self.description.label.text, self.description.data) self.form_valid = True
def get_identica(url_token): """ Return identica page. """ if current_user.is_authenticated: return redirect(url_for('base.get_home')) if request.method == 'POST': verify_data = IdenticaManager.verify_url(url_token) if verify_data is not None and verify_data.get('from'): user = UserStore.get_or_create_user( verify_data['from']['id'], verify_data['from'].get('first_name'), verify_data['from'].get('last_name'), verify_data['from'].get('username')) login_user(SignedInUser(user), remember=True) logging.debug('Sign in as user %s (%s)' % \ (' '.join([user.first_name, user.last_name]), user.from_id)) blueprints.set_value('mobile', request.form.get('touch') == 'true') return redirect(url_for('base.get_home')) with open('source/static/face.txt', 'r') as file: ascii_img = file.read().split('\n') return render_template('base/home.html', url_token=url_token, ascii_img=ascii_img)
def validate(self, entity_count: int) -> "Pagination": """ Initiate pagination object with values. """ is_valid = True self.entity_count = entity_count # Calculate page_count self.page_count = int(math.modf(entity_count / self.per_page)[1]) if self.page_count < self.entity_count / self.per_page: self.page_count = self.page_count + 1 # Check page_index and per_page validity if self.page_index < 1: self.page_index = 1 is_valid = False elif self.page_index > self.page_count: self.page_index = self.page_count is_valid = False if self.per_page < 1: self.per_page = 1 is_valid = False # Store arguments in session blueprints.set_value(self.name_page_index, self.page_index) blueprints.set_value(self.name_per_page, self.per_page) return is_valid
def __init__(self) -> "OptionHistoryFilter": """ Initiate object with values from request. """ super(OptionHistoryFilter, self).__init__('optionHistoryFilter') if request.method == 'GET': self.username.data = blueprints.get_value(self.username.label.text, str, None) self.title.data = blueprints.get_value(self.title.label.text, str, None) self.event.data = blueprints.get_value(self.event.label.text, str, None) elif request.method == 'POST': blueprints.set_value(self.username.label.text, self.username.data) blueprints.set_value(self.title.label.text, self.title.data) blueprints.set_value(self.event.label.text, self.event.data) self.form_valid = True