def to_boolean_raise(value, default=False): if is_blank(value): return default true_values = ['true', 'yes', 'x', 'y'] false_values = ['false', 'no', 'n', ''] value = str(value).strip().lower() if value in true_values: return True if value in false_values: return False raise ValidationException("{value} is not a valid boolean value. Should be one of {accepted_values}" .format(value=value, accepted_values=true_values + false_values))
def to_choice_raise(field, value): """ Rules: validate only against the display_name, the second part of a Django choice (internal, display_name) case insensitive """ if not util_model.has_choices(field): raise FieldErrorException("The field {field} has no choices".format(field.verbose_name)) if is_blank(value): return field.default value = str(value) choices = util_model.get_field_choices(field) return to_model_choice(choices, value)
def to_choice_raise(field, value): """ Rules: validate only against the display_name, the second part of a Django choice (internal, display_name) case insensitive """ if not util_model.has_choices(field): raise FieldErrorException("The field {field} has no choices".format(field.verbose_name)) if is_blank(value): return field.default value = str(value) choices = util_model.get_field_choices(field) choice = next((c[0] for c in choices if c[1].lower() == value.lower()), None) if choice is None: message = "{value} not an authorized choice. Should be one of: {values}" \ .format(value=value, values=[str(c[1]) for c in choices]) raise FieldErrorException(message) return choice
def to_date_raise(value, default=None): if is_blank(value): return default if isinstance(value, datetime.datetime): value = value.date() value = str(value).strip() valid_formats = settings.DATE_INPUT_FORMATS date = None for format_ in valid_formats: try: date = datetime.datetime.strptime(value, format_) break except ValueError: pass if date is None: message = "{value} is not a valid date.".format(value=value) raise ValidationException(message) else: return date
def to_species_observation_raise(value, site_visit, commit=True, row_data=None): """ Validate the supplied species name value. Rules: * Create a SpeciesObservation object with an input_name = value This function also try to get a name_id reference from the species data reference If a no name_id is returned it means that the species name is probably invalid but we still create the object without raising an exception :param value: :param site_visit: :param commit: save the object if True :return: the SpeciesObservation object """ if is_blank(value): message = "Species is blank." raise FieldErrorException(message) value = str(value) manager = SpeciesObservationManager(value, row_data=row_data) return manager.create(site_visit=site_visit, commit=commit)
def to_field_value_raise(field, value, commit=True, site_visit=None, row_data=None): if is_blank(value): if util_model.is_mandatory(field): message = "Mandatory field with no data {field}.".format(field=field.verbose_name) raise ValidationException(message) # Every next conversion functions should handle a blank/None value if util_model.is_lookup_field(field): return to_lookup_raise(field, value, commit=commit) if util_model.has_choices(field): return to_choice_raise(field, value) if util_model.is_boolean_field(field): return to_boolean_raise(value) if util_model.is_integer_field(field): return to_integer_raise(value) if util_model.is_float_field(field): return to_float_raise(value) if util_model.is_date_field(field): return to_date_raise(value) if util_model.is_string_field(field): return to_string(value) if util_model.is_species_observation_field(field): return to_species_observation_raise(value, site_visit, commit=commit, row_data=row_data) return value
def to_lookup_raise(field, value, commit=True, default=None): """ Rules: validate only for 'strict' lookups validate against code or values (case insensitive) deprecated lookups are rejected. """ if is_blank(value): return default if not util_model.is_lookup_field(field): raise FieldErrorException("{field} is not a lookup".format(field=field)) lookup_model = field.related_model value = str(value) if value is not None else None # operate on string only # search for lookup code first and value after (case insensitive) lookup = lookup_model.objects.filter(code__iexact=value).first() \ or lookup_model.objects.filter(value__iexact=value).first() if lookup is None: # can't find. If lookup is strict, it's an error if util_model.is_strict_lookup_field(field): codes = [str(c) for c in util_model.get_field_lookup_codes(field)] values = [str(v) for v in util_model.get_field_lookup_values(field)] accepted_values = codes + values message = "{value} is not an authorized lookup value for {field}. Should be one of: {values}" \ .format(value=value, field=field.verbose_name, values=accepted_values) raise FieldErrorException(message) elif value is not None and len(value.strip()) > 0: # if not strict we add a new lookup in the value. lookup = lookup_model(value=value) if commit: lookup.save() elif lookup.deprecated: message = "{value} is a deprecated value for {field}" \ .format(value=value, field=field.verbose_name) raise FieldErrorException(message) return lookup
def __init__(self, visit_name, site_code): # visit_name and site_code must be string or None self.visit_name = str(visit_name).strip() if not is_blank(visit_name) else None self.site_code = str(site_code).strip() if not is_blank(site_code) else None
def to_integer_raise(value, default=None): try: return int(str(value).strip()) if not is_blank(value) else default except ValueError: raise ValidationException("{value} is not a valid integer".format(value=value))
def must_validate(self): return is_blank(self.validation_status)
def listUserByUnit(): search = request.args.get('search') divisi = request.args.get('divisi') start_date = request.args.get('start_date') end_date = request.args.get('end_date') query = queryAccount(divisi=divisi) if search: query = queryAccount(search='%' + search + '%', divisi=divisi) get_users_redis = redis_client.get('users') if get_users_redis and is_blank(search): users = json.loads(get_users_redis) result = [data for data in users if data['id_divisi'] == divisi] else: users = db.session.execute(query) if users.returns_rows == False: return [] else: result_schema = UserResults() result = result_schema.dump(users, many=True) # close connection database users.close() if start_date and end_date: start_date = datetime.datetime.strptime(start_date + '-0:0:0', '%Y-%m-%d-%H:%M:%S') end_date = datetime.datetime.strptime(end_date + '-23:59:59', '%Y-%m-%d-%H:%M:%S') dateRange = np.arange(np.datetime64(start_date), np.datetime64(end_date + timedelta(days=1)), dtype='datetime64[D]') listBusday = np.busdaycalendar(holidays=dateRange, weekmask=busmask_names) listHoliday = getListHoliday(mongoClient, np, start_date, end_date) # Delete working days if there are holidays listBusday = np.array( list(filter(lambda x: x not in listHoliday, listBusday.holidays))) for user in result: listDayNoLogbook = [] dataFillingLogbook = 0 if start_date and end_date: get_data_logbook_redis = redis_client.get( keys_redis(user['id'], 'logbooks')) get_data_attendance_redis = redis_client.get( keys_redis(user['id'], 'attendances')) # Get list date weekend listWeekend = np.busdaycalendar(holidays=dateRange, weekmask=weekmask_names) # Get list date logbook from redis if get_data_logbook_redis and end_date.date( ) < datetime.datetime.today().replace(day=1).date(): # filter date logbook by query listDateLogbook = np.array([ parse_datetime(data['dateTask']).strftime('%Y-%m-%d') for data in json.loads(get_data_logbook_redis) if start_date <= parse_datetime(data['dateTask']) <= end_date ], dtype='datetime64') else: # Get list date logbook from database listDateLogbook = getListDateLogbook(mongoClient, user['id'], np, start_date, end_date) if get_data_attendance_redis and end_date.date( ) < datetime.datetime.today().replace(day=1).date(): # Get list date permit from redis listPermit = np.array([ parse_datetime(data['startDate']).strftime('%Y-%m-%d') for data in json.loads(get_data_attendance_redis) if start_date <= parse_datetime(data['startDate']) <= end_date and data['message'] in ['CUTI', 'SAKIT', 'IZIN'] ], dtype='datetime64') else: # Get list date permit from database listPermit = getListPermit(mongoClient, user['id'], np, start_date, end_date) # Join array date logbook and array date permit listDateLogbook = np.concatenate((listDateLogbook, listPermit)) # Remove duplicate date logbook listDateLogbook = [ i for j, i in enumerate(listDateLogbook) if i not in listDateLogbook[:j] ] # Delete date logbook if there are weekend and holiday listDateLogbook = list( np.array( list( filter( lambda x: x not in np.concatenate( (listWeekend.holidays, listHoliday)), listDateLogbook)))) # Logbook list empty days listDayNoLogbook = list( np.array( list(filter(lambda x: x not in listDateLogbook, listBusday)))) dataFillingLogbook = round( (len(listDateLogbook) / len(listBusday)) * 100, 2) if len(listDayNoLogbook) > 0: listDayNoLogbook = list( np.datetime_as_string(listDayNoLogbook, unit='D')) totalReport = getCountLogbook(mongoClient, user['id'], start_date, end_date) totalHours = getCountHours(mongoClient, user['id'], start_date, end_date) # add new value on user dictonary user['total_report'] = totalReport user['total_hours'] = totalHours user['precentage_logbook_data_filling'] = dataFillingLogbook user['logbook_list_empty_days'] = listDayNoLogbook return jsonify(result)