def update_basics(request: HttpRequest) -> HttpResponse: if request.method != 'POST': raise Http404 profile = request.user.profile profile.playa_name = request.POST['playa_name'] zipcode = request.POST['zipcode'] if zipcode: if len(zipcode) != 5: return HttpResponseBadRequest('Invalid zipcode') profile.zipcode = zipcode links_by_account_type = {} # type: Dict[str, str] for account_type, verbose_type in SocialMediaLink.ACCOUNT_TYPES: link = request.POST['social_link_{}'.format(account_type)] links_by_account_type[account_type] = link for social_link in profile.social_media_links.all(): new_value = links_by_account_type[social_link.account_type] social_link.link = new_value social_link.save() links_by_account_type.pop(social_link.account_type) for account_type in links_by_account_type: new_link = links_by_account_type[account_type] if not new_link: continue social_link = SocialMediaLink() social_link.account_type = account_type social_link.link = new_link social_link.user_profile = profile social_link.save() phone = request.POST['phone'] if phone: try: parsed_number = phonenumbers.parse(phone, 'US') formatted_number = phonenumbers.format_number( parsed_number, phonenumbers.PhoneNumber()) number_chunks = formatted_number.split('-') if list(map(len, number_chunks)) != [3, 3, 4]: return HttpResponseBadRequest('Error parsing phone number') profile.phone_number = formatted_number except Exception: return HttpResponseBadRequest('Error parsing phone number') years_on_playa = request.POST['years_on_playa'] try: profile.years_on_playa = int( years_on_playa) if years_on_playa else None except ValueError: return HttpResponseBadRequest('Invalid value for years on playa') profile.biography = request.POST['bio'] profile.save() return redirect('user-profile-me')
def formatted_phone_number(self) -> Optional[str]: if self.phone_number: try: parsed_number = phonenumbers.parse(self.phone_number, 'US') return phonenumbers.format_number(parsed_number, phonenumbers.PhoneNumber()) except Exception as e: import traceback traceback.print_exc() print('Error parsing phone number: {}'.format( self.phone_number)) return None return None
def parse_phone_number(cls, raw_number: str) -> str: try: parsed_number = phonenumbers.parse(raw_number, 'US') formatted_phone = phonenumbers.format_number( parsed_number, phonenumbers.PhoneNumber()) if len(formatted_phone) != 12: raise ValidationError( 'Invalid phone number. Expected xxx-xxx-xxxx') chunks = formatted_phone.split('-') if len(chunks) != 3 or len(chunks[0]) != 3 or len( chunks[1]) != 3 or len(chunks[2]) != 4: raise ValidationError( 'Invalid phone number. Expected xxx-xxx-xxxx') except Exception: raise ValidationError( 'Invalid phone number. Expected xxx-xxx-xxxx') return formatted_phone
def process_item(self, item, spider): if 'auctions' not in getattr(spider, 'pipelines', []): return item item['id'] = int(item['id'][0]) item['auctioneer'] = ' '.join(item['auctioneer']) item['contact_number'] = ' '.join(item['contact_number']) item['date'] = '%s %s' % (' '.join(item['date']), ' '.join( item['time'])) item['location'] = ' '.join(item['location']) item['link'] = ' '.join(item['link']) item['listing'] = ' '.join(item['listing']) #format phonenumber parsed_number = phonenumbers.parse(item['contact_number'], 'US') item['contact_number'] = phonenumbers.format_number( parsed_number, phonenumbers.PhoneNumber()) # format listing / remove any html cludge soup_listing = BeautifulSoup(item['listing']) item['listing'] = soup_listing.get_text() # format date and time to standard format dt = parse(item['date']) item['date'] = dt.datetime.strftime('%Y-%m-%d %H:%M:%S') if item['id'] in self.ids: raise DropItem('Dupe auction stored, ignoring listing: %s' % item) else: self.dt.insert( { 'id': item['id'], 'auctioneer': item['auctioneer'], 'contact_number': item['contact_number'], 'date': item['date'], 'location': item['location'], 'link': item['link'], 'listing': item['listing'], }, 'auctions') return item
def invalid_number(): # 7 digit number, which is not allowed by NKOM return phonenumbers.PhoneNumber(country_code=47, national_number=2285505)
def valid_number(): # +4722855050 belongs to the University of Oslo return phonenumbers.PhoneNumber(country_code=47, national_number=22855050)