def fetch_key(self, fingerprint, user): """Fetch given GPG key. This function will retry the task if fetching fails and inform the user of any errors. If the final retry fails (or an error not related to fetching the key is raised), the function does not handle the exception. Parameters ---------- fingerprint : str user : User The user to logg error messages to. """ try: return gpg_backend.fetch_key('0x%s' % fingerprint).decode('utf-8') except (URLError, socket.timeout) as e: retries = self.request.retries # Log exception using standard logging log.info('Failed fetching key %s: %s/%s tries', fingerprint, retries, self.max_retries) log.exception(e) delta = timedelta(seconds=60 * 10) if retries == self.max_retries: msg = gettext_noop( 'Unable to fetch GPG key. Giving up and sending mail unencrypted. Sorry.') user.log(msg) user.message(messages.ERROR, msg) raise else: delta_formatted = format_timedelta(delta) payload = { 'max': self.max_retries + 1, 'retry': retries + 1, 'time': delta_formatted, } msg = gettext_noop( 'Unable to fetch GPG key (%(retry)s of %(max)s tries). ' 'Will try again in %(time)s.') user.log(msg, **payload) user.message(messages.ERROR, msg, **payload) self.retry(exc=e, countdown=delta.seconds)
def fetch_key(self, fingerprint, user): """Fetch given GPG key. This function will retry the task if fetching fails and inform the user of any errors. If the final retry fails (or an error not related to fetching the key is raised), the function does not handle the exception. Parameters ---------- fingerprint : str user : User The user to logg error messages to. """ try: return gpg_backend.fetch_key('0x%s' % fingerprint).decode('utf-8') except (URLError, socket.timeout) as e: retries = self.request.retries # Log exception using standard logging log.info('Failed fetching key %s: %s/%s tries', fingerprint, retries, self.max_retries) log.exception(e) delta = timedelta(seconds=60 * 10) if retries == self.max_retries: msg = ugettext_noop( 'Unable to fetch GPG key. Giving up and sending mail unencrypted. Sorry.') user.log(msg) user.message(messages.ERROR, msg) raise else: delta_formatted = format_timedelta(delta) payload = { 'max': self.max_retries + 1, 'retry': retries + 1, 'time': delta_formatted, } msg = ugettext_noop( 'Unable to fetch GPG key (%(retry)s of %(max)s tries). ' 'Will try again in %(time)s.') user.log(msg, **payload) user.message(messages.ERROR, msg, **payload) self.retry(exc=e, countdown=delta.seconds)
def get_context_data(self, **kwargs): context = super(HttpUploadView, self).get_context_data(**kwargs) user = context['object'] qs = Upload.objects.filter(jid=user.username) # If a client requests an upload slot but never uploads the file, file field will be empty context['uploads'] = qs.exclude(file='') limit_config = get_config(user.username) if limit_config is False: context['limits'] = False elif limit_config == {}: context['limits'] = 'no-limits' else: context['limits'] = [] context['can_upload'] = True now = timezone.now() for key, value in limit_config.items(): if key == 'max_total_size': current = qs.aggregate(total=Sum('size'))['total'] or 0 limit = max(value - current, 0) row = { 'header': _('Total bytes'), 'value': filesizeformat(value), 'current': filesizeformat(current), 'left': filesizeformat(limit), } if limit == 0: context['can_upload'] = False row['exceeded'] = True elif key == 'max_file_size': row = { 'header': _('Maximum file size'), 'value': filesizeformat(value), } elif key == 'bytes_per_timedelta': delta = value['delta'] quota = value['bytes'] current = qs.filter(created__gt=now - delta).aggregate(total=Sum('size'))['total'] or 0 limit = max(quota - current, 0) row = { 'header': _('Bytes/%(delta)s') % {'delta': format_timedelta(delta), }, 'value': filesizeformat(quota), 'current': filesizeformat(current), 'left': filesizeformat(limit), } if limit == 0: context['can_upload'] = False row['exceeded'] = True elif key == 'uploads_per_timedelta': delta = value['delta'] quota = value['uploads'] current = qs.filter(created__gt=now - delta).count() limit = max(quota - current, 0) row = { 'header': _('Uploads/%(delta)s') % {'delta': format_timedelta(delta), }, 'value': quota, 'current': current, 'left': limit, } if limit == 0: context['can_upload'] = False row['exceeded'] = True else: log.error('Unknown limit encountered: %s', key) continue context['limits'].append(row) return context