def send_sms(text_body, phone_list, image_url=None): twilio_client = TwilioRestClient(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN) for phone in phone_list: phone = "+1" + phone try: gsm = is_gsm(text_body) if not gsm: raven_client.captureException() #max length for non gsm twilio sms is much shorter so remove non gsm text_body = remove_non_gsm(text_body) if image_url is not None: twilio_client.messages.create(body=text_body, to=phone, from_=settings.TWILIO_PHONE, media_url=image_url) else: if len(text_body) <= 160: twilio_client.messages.create(body=text_body, to=phone, from_=settings.TWILIO_PHONE) else: messages = split_message(text_body) for m in messages: twilio_client.messages.create( body=m, to=phone, from_=settings.TWILIO_PHONE) sleep(2) except Exception: raven_client.captureException() pass
def get(self, request): try: raise ValueError('An example error') except Exception: client.captureException(request=request) return Error500View.as_view()(request)
def ToJsonError(e, exc_info): """Converts an exception to an API error response.""" # Wrap some common exception types into Krest types if isinstance(e, Http404): e = krest.NotFoundError(e.message) elif isinstance(e, ValueError): e = krest.BadRequestError(str(e)) elif isinstance(e, backend.NoTokenError): e = krest.NotFoundError(e.message) # Now determine the response based on the exception type. if isinstance(e, krest.Error): code = e.__class__.__name__ http_code = e.HTTP_CODE message = e.Message() else: code = 'ServerError' http_code = 500 message = 'An internal error occurred: %s' % str(e) if settings.DEBUG: message += "\n" + "\n".join(traceback.format_exception(*exc_info)) if settings.DEBUG and settings.HAVE_RAVEN: from raven.contrib.django.models import client client.captureException() result = { 'error' : { 'code' : code, 'message' : message } } return result, http_code
def write_read_orm(): """ Write a ResponseLog object to the database to see if it is up. Returns: bool: True if we can read and write using the ORM. """ random_roundtrip = randint(1, 1000) random_available = random() > 0.5 try: connection.ensure_connection() except: raven_client.captureException() return False try: response_log = ResponseLog.objects.create( platform=GCM_PLATFORM, roundtrip_time=random_roundtrip, available=random_available, ) except DatabaseError: connection.close() raven_client.captureException() return False else: if response_log.available == random_available and response_log.roundtrip_time == random_roundtrip: response_log.delete() return True return False
def send_recommendation_notification_to_users(users): for user in users: try: send_recommendation_notification(user) except: print "Unexpected error:", sys.exc_info()[0] client.captureException()
def wrap_exception(request, exception): """Returns a HttpResponse with the exception in JSON form.""" exc_info = sys.exc_info() LOGGER.error('%s: %s' % (exception.__class__.__name__, exception), exc_info=exc_info, extra={ 'status_code': 500, 'request': request, } ) if settings.DEBUG and settings.HAVE_RAVEN: from raven.contrib.django.models import client client.captureException() # Don't wrap the exception during debugging. if settings.DEBUG and 'deb' in request.GET: return None result_data, http_code = to_json_error(exception, exc_info) result_data['meta'] = { 'result': 'error' } return build_response(result_data, response_code=http_code)
def ToJsonError(e, exc_info): """Converts an exception to an API error response.""" # Wrap some common exception types into kbapi types if isinstance(e, Http404): e = kbapi.NotFoundError(e.message) elif isinstance(e, ValueError): e = kbapi.BadRequestError(str(e)) elif isinstance(e, backend.NoTokenError): e = kbapi.NotFoundError(e.message) # Now determine the response based on the exception type. if isinstance(e, kbapi.Error): code = e.__class__.__name__ http_code = e.HTTP_CODE message = e.Message() else: code = 'ServerError' http_code = 500 message = 'An internal error occurred: %s' % str(e) if settings.DEBUG: message += "\n" + "\n".join(traceback.format_exception(*exc_info)) if settings.DEBUG and settings.HAVE_RAVEN: from raven.contrib.django.models import client client.captureException() result = {'error': {'code': code, 'message': message}} return result, http_code
def get(self, request, pk): self.object = project = get_object_or_404(Project.objects.select_related('owner'), pk=pk) moonclerk_key = settings.MOONCLERK_API_KEY customer_id = request.GET.get('customer_id', None) payment_id = request.GET.get('payment_id', None) if customer_id is None and payment_id is None: return HttpResponse('You must specify either a customer_id or a payment_id.', status=400) payment = customer = None try: if customer_id: customer = self.get_moonclerk_customer(request, customer_id, moonclerk_key) if payment_id: payment = self.get_moonclerk_payment(request, payment_id, moonclerk_key) assert customer or payment except: # Something went wrong and we weren't able to create a customer or # payment. from raven.contrib.django.models import client client.captureException() return self.get_payment_error() self.set_payment_info(project, payment, customer) return redirect('app-project-activation-success', owner_slug=project.owner.slug, project_slug=project.slug)
def send_chat_sms(text_body, to_phone, from_phone): twilio_client = TwilioRestClient(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN) to_phone = "+1" + to_phone from_phone = "+1" + from_phone try: gsm = is_gsm(text_body) if not gsm: raven_client.captureException() #max length for non gsm twilio sms is much shorter so remove non gsm text_body = remove_non_gsm(text_body) if len(text_body) <= 160: twilio_client.messages.create(body=text_body, to=to_phone, from_=from_phone) else: messages = split_message(text_body) for m in messages: twilio_client.messages.create(body=m, to=to_phone, from_=from_phone) sleep(2) except Exception: raven_client.captureException() pass
def get(self, request, pk): self.object = project = get_object_or_404( Project.objects.select_related('owner'), pk=pk) moonclerk_key = settings.MOONCLERK_API_KEY customer_id = request.GET.get('customer_id', None) payment_id = request.GET.get('payment_id', None) if customer_id is None and payment_id is None: return HttpResponse( 'You must specify either a customer_id or a payment_id.', status=400) payment = customer = None try: if customer_id: customer = self.get_moonclerk_customer(request, customer_id, moonclerk_key) if payment_id: payment = self.get_moonclerk_payment(request, payment_id, moonclerk_key) assert customer or payment except: # Something went wrong and we weren't able to create a customer or # payment. from raven.contrib.django.models import client client.captureException() return self.get_payment_error() self.set_payment_info(project, payment, customer) return redirect('app-project-activation-success', owner_slug=project.owner.slug, project_slug=project.slug)
def get_video_info(link): # Make sure video is from Ok.ru if not 'ok.ru' in link: return None response = None try: response = requests.get(link) except Exception: client.captureException() logger.log('Ok.ru status: error | Error url: ' + link, forward=True) return None duration_search_result = re.compile( 'duration\\\\":\\\\"(.*?)\\\\"', 0).search(response.text) if not duration_search_result: return None duration = duration_search_result.groups()[0] info = {'duration': int(duration), 'video_url': None} return info
def log_exceptions(exc_type=Exception, **kwargs): from raven.contrib.django.models import client as raven_client try: yield except exc_type, exc: raven_client.captureException(sys.exc_info()) print(exc)
def report_error(error, request=None, extra_data=None, level='warning', prefix='Handled exception', skip_raven=False, print_tb=False): """Wrapper for error reporting This can be used for store exceptions in error reporting solutions as rollbar while handling error gracefully and giving user cleaner message. """ if HAS_ROLLBAR and hasattr(settings, 'ROLLBAR'): rollbar.report_exc_info(request=request, extra_data=extra_data, level=level) if not skip_raven and HAS_RAVEN and hasattr(settings, 'RAVEN_CONFIG'): raven_client.captureException(request=request, extra=extra_data, level=level) LOGGER.error('%s: %s: %s', prefix, error.__class__.__name__, force_text(error)) if print_tb: LOGGER.exception(prefix)
def new_execute(self, *args, **kwargs): try: return original_func(self, *args, **kwargs) except Exception: from raven.contrib.django.models import client client.captureException(extra={'argv': sys.argv}) raise
def __call__(self, *args, **kwargs): try: return self.func(*args, **kwargs) except: if hasattr(settings, 'RAVEN_CONFIG'): from raven.contrib.django.models import client client.captureException() raise
def load_extra_properties(self): client = FoursquareClient.shared_client try: venue_data = client.venues(self.foursquare_venue_id)['venue'] self.location = venue_data['location'] self.venue_name = venue_data['name'] except: raven_client.captureException()
def wrapped(*args, **kwargs): try: return func(*args, **kwargs) except exclude: raise except on as exc: Raven.captureException() current.retry(exc=exc)
def obj_create(self, bundle, **kwargs): try: raise Exception('oops') except: client.captureException() bundle.obj = Item(**kwargs) bundle = self.full_hydrate(bundle) return bundle
def update_checks(ids): data = Cycle.objects.filter(id__in=ids).defer("state").all() logger.info("update checks for ids", extra={"ids": ids}) for cycle in data: try: data_import = DataImport(None, cycle.title).build_form_db(cycle) calculate_scores_for_checks_in_cycle(data_import) except Exception as e: logger.error("error", extra={"exception": e.message, "cycle": cycle.title}) client.captureException()
def contacts_from_data(user, data): phone_numbers = [] for c_json in data: try: cobj = json.loads(c_json) phone = normalize_phone_number(cobj['phone']) phone_numbers.append(phone) except Exception: client.captureException() return Contact.objects.filter(user=user, normalized_phone__in=phone_numbers)
def new_execute(self, *args, **kwargs): try: return original_func(self, *args, **kwargs) except Exception: from raven.contrib.django.models import client client.captureException(extra={ 'argv': sys.argv }) raise
def log_exceptions(exc_type=Exception, **kwargs): try: from raven.contrib.django.models import client as raven_client except ImportError: raven_client = None try: yield except exc_type, exc: if raven_client is not None: raven_client.captureException(sys.exc_info()) print(exc)
def _task(): try: close_old_connections() fn(*args, **kwargs) except Exception: msg = f'Error running thread pool task {fn.__name__}' logger.exception(msg) client.captureException(msg) raise finally: close_old_connections()
def _send_slack(data): if not (settings.SLACK_ENABLE and settings.SLACK_WEBHOOK): logger.info('Slack is not enabled.') return try: response = requests.post(settings.SLACK_WEBHOOK, data=data.encode('utf-8'), headers={'Content-Type': 'application/json'}, timeout=4) response.raise_for_status() except requests.exceptions.RequestException: sentry_client.captureException()
def sentry_exc(): """ Attempt to send an exception to sentry. This is "soft coupled" in that it will gracefully fail if raven is not installed. """ try: from raven.contrib.django.models import client # send the exception to raven client.captureException() except: logger.debug("raven is not installed, so we could not notify sentry of the exception.") pass
def server_error(self, request, exception): exc_info = sys.exc_info() client.captureException() if settings.DEBUG: (exc_type, exc_info, tb) = exc_info message = "%s\n" % exc_type.__name__ message += "%s\n\n" % exc_info message += "TRACEBACK:\n" for tb in traceback.format_tb(tb): message += "%s\n" % tb return self.serialize_error(500, message) else: return self.serialize_error(500, _('Internal error'))
def _save(self, name, content): name = name.replace('\\', '/') key = Key(self.bucket, name) try: if hasattr(content, 'temporary_file_path'): key.set_contents_from_filename(content.temporary_file_path()) elif isinstance(content, File): key.set_contents_from_file(content) else: key.set_contents_from_string(content) key.make_public() return name except (BotoClientError, BotoServerError), e: client.captureException() raise S3StorageError(*e.args)
def oauth_credentials(request): """ How do we correlate Planbox and Shareabouts users? Username is faulty but easy. With normal OAuth we wouldn't have this issue because the user would specify their own account. But for now, there's only one user to support. """ host = 'https://' + settings.SHAREABOUTS_HOST client_id = settings.SHAREABOUTS_CLIENT_ID client_secret = settings.SHAREABOUTS_CLIENT_SECRET # Make sure a project ID is specified project_id = request.GET.get('project_id') try: project = Project.objects.all().get(pk=project_id) except Project.DoesNotExist: return bad_request([{'project_id': 'Project does not exist.'}]) # Make sure the user has edit permission on the project. if not project.editable_by(request.user): return HttpResponse('Unauthorized', status=401) # Get the preauthorization object for the project. try: auth = Preauthorization.objects.get(project=project) except Preauthorization.DoesNotExist: raise Http404 username = auth.username # Get the requested credentials from the Shareabouts API server session = requests.session() try: auth_header = get_auth_header(client_id, client_secret, username) authorization_code = get_authorization_code(session, host, client_id, auth_header) credentials = get_credentials(session, host, authorization_code, client_id, client_secret) except AssertionError: if settings.DEBUG: raise client.captureException() return HttpResponse('Upstream error occurred.', status=502, content_type='text/plain') return HttpResponse(json.dumps(credentials, indent=2, sort_keys=True), status=200, content_type='application/json')
def sentry_exc(): """ Attempt to send an exception to sentry. This is "soft coupled" in that it will gracefully fail if raven is not installed. """ try: from raven.contrib.django.models import client # send the exception to raven client.captureException() except: logger.debug( "raven is not installed, so we could not notify sentry of the exception." ) pass
def send_aps_notification_to_users(users, **kwargs): apns = APNService.objects.filter(hostname=settings.APNS_HOSTNAME)[0] print str(apns) devices = APSDevice.objects.filter(users__in=users, service=apns) print str(devices) payload_json = dumps(aps_payload(**kwargs)) try: payload_json = payload_json[0:239] notification = Notification.objects.create(custom_payload=payload_json, service=apns) #seems like something goes wrong if there's a bad device. just send to one device at a time for now #apns.push_notification_to_devices(notification, devices, chunk_size=200) for device in devices: apns.push_notification_to_devices(notification, [device], chunk_size=200) except: print "Failed to send push" print str(client.captureException()) client.captureException()
def report_error(error, request=None, extra_data=None): """Wrapper for error reporting This can be used for store exceptions in error reporting solutions as rollbar while handling error gracefully and giving user cleaner message. """ if HAS_ROLLBAR and hasattr(settings, 'ROLLBAR'): rollbar.report_exc_info(request=request, extra_data=extra_data, level='warning') if HAS_RAVEN and hasattr(settings, 'RAVEN_CONFIG'): raven_client.captureException(request=request, extra_data=extra_data, level='warning') LOGGER.error('Handled exception %s: %s', error.__class__.__name__, force_text(error).encode('utf-8'))
def stripe_hooks(request): try: payload = json.loads(request.body) except ValueError: return HttpResponseBadRequest("Invalid JSON payload") try: event = stripe.Event.construct_from(payload, stripe.api_key) except stripe.error.StripeError: raven_client.captureException() return HttpResponseBadRequest() # Generally, for this list of if-statements, see the create_missing_stripe_webhook # function. # The list of events there ought to at least minimally match what we're prepared # to deal with here. if event.type == "invoice.payment_succeeded": payment_intent = event.data.object send_payment_received_email.delay( payment_intent.customer, request.LANGUAGE_CODE, payment_intent.created, payment_intent.invoice_pdf, ) track_event( CATEGORY_MONTHLY_PAYMENTS, ACTION_SUBSCRIPTION_CREATED, f"{settings.CONTRIBUTION_AMOUNT_USD:.2f}", ) elif event.type == "customer.subscription.deleted": obj = event.data.object for user in User.objects.filter(stripe_customer_id=obj.customer): UserSubscription.set_canceled(user, obj.id) track_event(CATEGORY_MONTHLY_PAYMENTS, ACTION_SUBSCRIPTION_CANCELED, "webhook") else: return HttpResponseBadRequest( f"We did not expect a Stripe webhook of type {event.type!r}") return HttpResponse()
def send_in_channel(channel, topic, payload): message = { 'type': 'message.send', 'text': json.dumps({ 'topic': topic, 'payload': payload, }), } try: channel_layer_send_sync(channel, message) except ChannelFull: # TODO investigate this more # maybe this means the subscription is invalid now? sentry_client.captureException() except RuntimeError: # TODO investigate this more (but let the code continue in the meantime...) sentry_client.captureException() else: stats.pushed_via_websocket(topic)
def create_stripe_subscription(request): user = request.user if not flag_is_active(request, "subscription"): return HttpResponseForbidden( "subscription flag not active for this user") has_stripe_error = False try: email = request.POST.get("stripe_email", "") stripe_token = request.POST.get("stripe_token", "") create_stripe_customer_and_subscription_for_user( user, email, stripe_token) except stripe.error.StripeError: raven_client.captureException() has_stripe_error = True query_params = "?" + urlencode({"has_stripe_error": has_stripe_error}) return redirect( reverse("users.user_edit", args=[user.username]) + query_params)
def parse_json_into_users_and_contact_lists(user, list): new_list = [] for c_json in list: c = json.loads(json.dumps(c_json)) new_list.append(c) user_list = [] contact_list = [] for c in new_list: cobj = json.loads(c) if Profile.objects.filter(normalized_phone=normalize_phone_number( cobj['phone'])).exists(): p = Profile.objects.get( normalized_phone=normalize_phone_number(cobj['phone'])) user_list.append(p.user) else: if not Contact.objects.filter( user=user, name=cobj['name'], phone_number=cobj['phone'], normalized_phone=normalize_phone_number( cobj['phone'])).exists(): contact = Contact(user=user, name=cobj['name'], phone_number=cobj['phone'], normalized_phone=normalize_phone_number( cobj['phone'])) try: contact.save() except DatabaseError: client.captureException() else: contact = Contact.objects.filter( user=user, name=cobj['name'], phone_number=cobj['phone'], normalized_phone=normalize_phone_number( cobj['phone'])).latest('date_created') contact_list.append(contact) return user_list, contact_list
def _transform_events_response(data): serializer = TimelineEventSerializer(data=data.get('events', []), many=True) try: serializer.is_valid(raise_exception=True) except ValidationError as exc: event_id = client.captureException() raise APIException( f'Unexpected response data format received from the company ' f'timeline API. Error reference: {event_id}.', ) from exc return serializer.validated_data
def report_error(error, request=None, extra_data=None): """Wrapper for error reporting This can be used for store exceptions in error reporting solutions as rollbar while handling error gracefully and giving user cleaner message. """ if HAS_ROLLBAR and hasattr(settings, 'ROLLBAR'): rollbar.report_exc_info( request=request, extra_data=extra_data, level='warning' ) if HAS_RAVEN and hasattr(settings, 'RAVEN_CONFIG'): raven_client.captureException( request=request, extra=extra_data, level='warning' ) LOGGER.error( 'Handled exception %s: %s', error.__class__.__name__, force_text(error) )
def update_url_info(url,timeout=10): """ Update url status from URLCHECKER_SERVER. It update URLStatusHistory table and URLCHECKER_SERVER """ server = getattr(settings,'URLCHECKER_SERVER') key = getattr(settings,'URLCHECKER_KEY') # it already is checked on __init__.py if not server or not key: raise ImproperlyConfigured(u"URLCHECKER: Configure URLCHECKER_SERVER "\ "and URLCHECKER_KEY on settings.py.") try: URLValidator()(url) except ValidationError: raise ValidationError(u"'%s' is one invalid URL. Update aborted.") try: url_request = requests.post(server,data={'key':key,'url':url}, timeout=timeout) except request.TIMEOUT as err: # capture exception to raven and try again sentry_id = client.get_ident(client.captureException()) logger.error(u"Timeout processing url %s [Sentry ID '%s']", url,sentry_id) else: if url_request.status_code in (200,304): # request without errors urldata = url_request.json['data'] print urldata urlstatus, created = URLStatusHistory.objects.get_or_create(url=url) urlstatus.update_time = datetime.datetime.now(pytz.utc) urlstatus.site_ip = urldata['site_ip'] urlstatus.site_fqdn = urldata['site_fqdn'] urlstatus.web_server_name = urldata['web_server_name'] urlstatus.html_status_code = urldata['html_status_code'] urlstatus.save() cache_key = 'urlchecker.client_%s' % md5(url).hexdigest() cache.set(cache_key,urlstatus,300) else: try: url_request.raise_for_status() except requests.HTTPError, err: # something happened, create a error in sentry if error is # unknow send_to_sentry=True if url_request.status_code == 412 and \ url_request.json.has_key('msg') and \ "is a PRIVATE IP and" in url_request.json['msg']: # know error, is a internal IP, we just get url info update_internal_url_info(url) send_to_sentry=False if send_to_sentry: import ipdb ipdb.set_trace() extra = { 'url': url, 'urlobj_id': getattr(urlobj,'id',None), 'code': url_request.status_code, 'method': 'post', 'json': url_request.json, 'text': url_request.text, 'request_dict': url_request.__dict__ } sentry_id = client.get_ident(client.captureException(extra=extra)) logger.error(u"HTTP error %s on update_url_info. Msg was '%s'", url_request.status_code,url_request.json.get('msg'))
def update_status(self): """ Update URL fields. This method uses socket to get IP of url and python requests to get request info. PRIVATE IPs will not be checked by default. To check PRIVATE IPs set URLCHECK_ALLOW_PRIVATE_IPS to True in settings.python RESERVED, SPECIALPURPOSE and LOOPBACK IPs will not be checked aniway. """ try: logger.debug(u"Checking url %s for user %s.",self.url,self.user.username) url = urlparse.urlparse(self.url) self.hostname = url.hostname if self.hostname: self.site_ip = socket.gethostbyname(self.hostname) self.site_fqdn = socket.getfqdn(self.hostname) if self.get_ip_type() in ["RESERVED","SPECIALPURPOSE","LOOPBACK"]: logger.warn(u"%s is a %s IP and will not be checked.",self.site_ip, self.get_ip_type()) return if not getattr(settings,"URLCHECK_ALLOW_PRIVATE_IPS",False) and \ self.get_ip_type() == "PRIVATE": logger.warn(u"%s is a %s IP and will not be checked. Set "\ "URLCHECK_ALLOW_PRIVATE_IPS in settings.py to check this IPs.", self.site_ip,self.get_ip_type()) return try: request = requests.get(self.url) self.web_server_name = request.headers.get('server','') self.html_status_code = request.status_code except requests.exceptions.Timeout: self.html_status_code = request.codes.timeout self.last_time_checked = timezone.now() old = URL.objects.get(pk=self.pk) # some field is different, update if self.site_ip != old.site_ip or \ self.site_fqdn != old.site_fqdn or \ self.web_server_name != old.web_server_name or \ self.html_status_code != old.html_status_code: self.update_time = self.last_time_checked # create a new history entry self.history.create( update_time = self.update_status, site_ip = self.site_ip, site_fqdn = self.site_fqdn, web_server_name = self.web_server_name, html_status_code = self.html_status_code ) logger.info(u"Updated url %s for user %s.",self.url,self.user.username) self.save() except Exception as err: sentry_id = client.get_ident(client.captureException()) logger.error(u"Error on update url status [Sentry ID '%s']", sentry_id) # remove key from cache so next request to status cache response again cache_key = "%s:%s" % (self.user.username,self.url) cache.delete(cache_key)
#!/usr/bin/env python # -*- coding: utf-8 -*- from django.conf import settings from django.conf.urls import url, patterns, include from django.conf.urls.static import static from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.http import HttpResponse, HttpResponsePermanentRedirect from ratelimitbackend import admin from ratelimitbackend.forms import AuthenticationForm from raven.contrib.django.models import client admin.autodiscover() client.captureException() # raven (sentry client) robots = lambda _: HttpResponse('User-agent: *\nDisallow:\n', mimetype='text/plain') humans = lambda _: HttpResponse(u"""/* TEAM */ Main developer: Mathieu Agopian Contact: mathieu.agopian [at] gmail.com Twitter: @magopian From: France /* SITE */ Language: English Backend: Django, PostgreSQL Frontend: SCSS, Compass
def obj_get_list(self, bundle, **kwargs): try: raise Exception('oops') except: client.captureException() return []
def query_url(request): """ This view returns status for url. This view returns appropriate responses for each possible status of a query. Remarkably 304 and 200 for success responses and 400, 403 and 500 for errors. You would like to read it: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes """ messages = { 'key_not_send': u"You need to send a key in POST data to be valid "\ "request. The key should be requested to system administrator.", 'key_invalid': u"Your key is invalid or not found. Contact system "\ "administrator for more information.", 'url_invalid_parameter': u"You need to send a url in POST data to be tested. "\ "Use 'url' field for that.", 'url_malformed': u"You sent a malformed url in POST. Check and try again.", } if not request.method == 'POST': if not request.is_ajax(): return HttpResponseBadRequest('This url should be requested via'\ ' Ajax POST requests ') return HttpResponseNotAllowed(['POST']) # TODO: if you have more views that use key put it in a decorator request.key = None key = request.POST.get('key',None) if not key: return HttpResponseForbidden(simplejson.dumps( {'code':403,'msg':messages['key_not_send']}), mimetype="application/json") else: try: keyobj = Key.objects.get(key=key,active=True, valid_until__gte=timezone.now()) except Key.DoesNotExist: return HttpResponseForbidden(simplejson.dumps( {'code':403,'msg':messages['key_invalid']}), mimetype="application/json") else: request.user = keyobj.user request.key = keyobj if request.key: if 'url' not in request.POST: return HttpResponseBadRequest(simplejson.dumps( {'code':400,'msg':messages['url_invalid_parameter']}), mimetype="application/json") url = request.POST['url'] url_validator = URLValidator() try: url_validator(url) except ValidationError: return HttpResponseBadRequest(simplejson.dumps( {'code':400,'msg':messages['url_malformed']}), mimetype='application/json') # try to get from cache try: return response_from_cache(url,request.user.username) except CacheKeyError: pass urlobj, created = URL.objects.get_or_create(user=request.user,url=url) if not urlobj.last_time_checked: try: urlobj.update_status() except Exception, err: from .tasks import update_urls update_urls.delay(urlobj.url) return cache_and_send(urlobj, {'code':500, 'msg':'Server error. Task was sent to queue. '\ 'Try again in some minutes.', 'error':unicode(err)},500) if urlobj.get_ip_type() in ["RESERVED","SPECIALPURPOSE","LOOPBACK"]: msg = u"%s is a %s IP and will not be checked." % ( urlobj.site_ip, urlobj.get_ip_type()) return cache_and_send(urlobj,{'code':412,'msg':msg},412) if not getattr(settings,"URLCHECK_ALLOW_PRIVATE_IPS",False) and \ urlobj.get_ip_type() == "PRIVATE": msg = u"%s is a %s IP and will not be checked. Set "\ "URLCHECK_ALLOW_PRIVATE_IPS in settings.py to check this IPs." % ( urlobj.site_ip,urlobj.get_ip_type()) return cache_and_send(urlobj,{'code':412,'msg':msg},412) try: url_data = urlobj.get_status() if not created and urlobj.update_time < urlobj.last_time_checked: return cache_and_send(urlobj,{'code': 304, 'msg': 'OK', 'data': url_data},304) else: return cache_and_send(urlobj,{'code': 200, 'msg': 'OK', 'data': url_data},200) except Exception, err: sentry_id = client.get_ident(client.captureException()) logger.error(u"Error processing url %s [Sentry ID '%s']", url,sentry_id) return cache_and_send(urlobj,{'code':500,'msg':'Server error', 'error':unicode(err)},500)
def save(self, commit=True): # set user if self.user.is_authenticated(): self.instance.user = self.user else: user = User.objects.filter( email=self.email_form.cleaned_data['email'].lower(), ).first() or User( email=self.email_form.cleaned_data['email'].lower(), ) while not user.pk: user.username = get_random_string() user.set_password(get_random_string()) try: user.save() except IntegrityError: # on duplicit username try again pass self.instance.user = user # set price self.instance.price = ( self.instance.subject_variant.price if self.instance.subject_variant else self.instance.subject.price ) self.instance.answers = dumps(self.questions_form.cleaned_data) super(RegistrationForm, self).save(commit) # send mail try: self.instance.send_mail() except: import traceback from raven.contrib.django.models import client traceback.print_exc() client.captureException() # save / update participant if self.participant_select_form.cleaned_data['participant'] == 'new': try: participant = Participant.objects.get( user = self.instance.user, birth_num = self.instance.participant.birth_num, ) except Participant.DoesNotExist: participant = Participant() participant.user = self.instance.user else: participant = Participant.objects.get(id=self.participant_select_form.cleaned_data['participant']) for attr in ['first_name', 'last_name', 'birth_num', 'age_group', 'street', 'city', 'postal_code', 'citizenship', 'phone', 'email', 'school', 'school_other', 'school_class', 'health']: setattr(participant, attr, getattr(self.instance.participant, attr)) participant.save() # save / update first parent if self.instance.parent1: if self.parent1_select_form.cleaned_data['parent'] == 'new': parent = Parent() parent.user = self.instance.user else: parent = Parent.objects.get(id=self.parent1_select_form.cleaned_data['parent']) for attr in ['first_name', 'last_name', 'street', 'city', 'postal_code', 'phone', 'email']: setattr(parent, attr, getattr(self.instance.parent1, attr)) parent.save() # save / update second parent if self.instance.parent2: if self.parent2_select_form.cleaned_data['parent'] == 'new': parent = Parent() parent.user = self.instance.user else: parent = Parent.objects.get(id=self.parent2_select_form.cleaned_data['parent']) for attr in ['first_name', 'last_name', 'street', 'city', 'postal_code', 'phone', 'email']: setattr(parent, attr, getattr(self.instance.parent2, attr)) parent.save() for agreement_form in self.agreement_forms: for option_id, checked in agreement_form.cleaned_data.items(): if checked: self.instance.agreement_options.add(agreement_form.options[option_id])