def dump_json(data): try: return json.dumps(data) except Exception as e: log_info('dump_json() errors(): %s, %s' % (data, str(e))) return None
def english_info(request): id = 0 try: id = int(request.GET.get('id', 0)) except Exception as e: log_info('english_info() errors: %s ' % str(e)) data_dict = {} if not id: data_dict.update({'msg': ErrorCodeMsgList.INVALID_PARAMETER.message}) else: item = English.get_dict_by_id(id) if item: item = format_english_dict(item, wechat=True) if not item: data_dict.update({'msg': ErrorCodeMsgList.INVALID_PARAMETER.message}) else: data_dict.update(item) context = RequestContext(request) context.update(get_seo_context(request)) context.update({'data': data_dict}) return template_response('english/info.html', context_instance=context)
def redirect_response(url, reverse_url=True): try: return redirect(reverse(url) if reverse_url else url) except Exception as e: log_info('redirect_response() errors: %s, %s' % (url, str(e))) return http_response('Fail to redirect to %s, %s' % (url, str(e)))
def get_by_id(cls, item_id): if not item_id: return None try: return English.objects.get(id=item_id) except English.DoesNotExist: log_info("English.get_by_id() does not exist: %s" % item_id) except Exception as e: log_info("English.get_by_id() errors: %s, %s" % (item_id, str(e))) return None
def valid_password(password): if not password or not isinstance(password, basestring): return False # password pattern = re.compile("^(?=.*\d)(?=.*[a-zA-Z]).{6,16}$") try: password = str(password.strip()) return pattern.match(password) is not None except Exception as e: log_info('valid_password() errors: %s, %s' % (password, str(e))) return False
def load_json(json_str): if not json_str or not isinstance(json_str, basestring): return None try: json_dict = json.loads(json_str) except Exception as e: log_info('load_json() errors: %s, %s' % (json_str, str(e))) return None if not (isinstance(json_dict, dict) or isinstance(json_dict, list)): log_info('load_json() invalid: %s' % json_str) return None return json_dict
def valid_email(email): if not email or not isinstance(email, basestring) or len(email) < 5: return False pattern = re.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?") try: email = str(email) return pattern.match(email) is not None except Exception as e: log_info( 'valid_email(): %s, errors: %s' % (email, str(e))) return False
def valid_mobile_number(mobile_number): if not mobile_number or not isinstance(mobile_number, basestring)\ or len(mobile_number) != 11: return False # Mobile pattern = re.compile("^(13[0-9]|15[012356789]|18[0-9]|14[57]|17[0-9])[0-9]{8}$") try: mobile_number = str(mobile_number) return pattern.match(mobile_number) is not None except Exception as e: log_info( 'valid_mobile_number(): %s, errors: %s' % (mobile_number, str(e))) return False
def english_list(request): page = 0 pagesize = 0 type = 0 dev = 0 try: page = int(request.GET.get('page', 0)) pagesize = int(request.GET.get('pagesize', 0)) type = int(request.GET.get('type', 0)) dev = int(request.GET.get('dev', 0)) except Exception as e: log_info('english_list() errors: %s ' % str(e)) page = max(1, page) pagesize = max(10, min(pagesize, 20)) items = English.objects.filter(is_archived=False, type=type).distinct() item_count = items.count() or pagesize pagesize = item_count # currently all page_count = int((item_count + pagesize - 1) / pagesize) items = items[(page - 1) * pagesize : page * pagesize] item_id_list = list(items.values_list('id', flat=True)) items = English.get_list_dict_by_ids(item_id_list) items = format_english_dict_list(items, wechat=True) data_dict = { 'type': type, 'page': page, 'pagesize': pagesize, 'page_count': page_count, 'item_count': item_count, 'items': items, } context = RequestContext(request) context.update(get_seo_context(request)) context.update({'data': data_dict}) return template_response('english/list.html', context_instance=context)
def is_valid_url(url): if not (url and isinstance(url, basestring)): return False # url pattern = re.compile( r'^https?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' # domain... r'localhost|' # localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) try: return pattern.match(url) is not None except Exception as e: log_info( 'is_valid_url(): %s, errors: %s' % (url, str(e))) return False
def valid_phone_number(phone_number): if not phone_number or not isinstance(phone_number, basestring)\ or len(phone_number) < 7: return False # Telephone pattern = re.compile("^(0([3-9]\d{2}|[1-9]\d{1})(-?))?[1-9](\d{7}|\d{6})$") try: phone_number = str(phone_number) valid = pattern.match(phone_number) is not None except Exception as e: log_info( 'valid_phone_number(): %s, errors: %s' % (phone_number, str(e))) return False if not valid: # Mobile phone valid = valid_mobile_number(phone_number) return valid