Example #1
0
File: texoid.py Project: DMOJ/site
    def get_result(self, formula):
        hash = hashlib.sha1(utf8bytes(formula)).hexdigest()

        if self.cache.has_file(hash, 'svg'):
            return self.query_cache(hash)
        else:
            return self.query_texoid(formula, hash)
Example #2
0
 def update_codesize(self):
     src_byte = utf8bytes(self.source.source)
     if src_byte:
         self.codesize = len(src_byte) / 1024
     else:
         self.codesize = 0
     if self.case_points < self.case_total or self.points == 0:
         self.codesize += 1
     self.save()
Example #3
0
def pwned_password(password):
    """
    Checks a password against the Pwned Passwords database.
    """
    if not isinstance(password, str):
        raise TypeError('Password values to check must be strings.')
    password_hash = hashlib.sha1(utf8bytes(password)).hexdigest().upper()
    prefix, suffix = password_hash[:5], password_hash[5:]
    results = _get_pwned(prefix)
    if results is None:
        # Gracefully handle timeouts and HTTP error response codes.
        return None
    return results.get(suffix, 0)
Example #4
0
File: mailgun.py Project: DMOJ/site
        def post(self, request, *args, **kwargs):
            params = request.POST
            timestamp = params.get('timestamp', '')
            token = params.get('token', '')
            signature = params.get('signature', '')

            logger.debug('Received request: %s', params)

            if signature != hmac.new(key=utf8bytes(settings.MAILGUN_ACCESS_KEY),
                                     msg=utf8bytes('%s%s' % (timestamp, token)), digestmod=hashlib.sha256).hexdigest():
                logger.info('Rejected request: signature: %s, timestamp: %s, token: %s', signature, timestamp, token)
                raise PermissionDenied()
            _, sender = parseaddr(params.get('from'))
            if not sender:
                logger.info('Rejected invalid sender: %s', params.get('from'))
                return HttpResponse(status=406)
            try:
                user = User.objects.get(email__iexact=sender)
            except (User.DoesNotExist, User.MultipleObjectsReturned):
                logger.info('Rejected unknown sender: %s: %s', sender, params.get('from'))
                return HttpResponse(status=406)
            try:
                registration = RegistrationProfile.objects.get(user=user)
            except RegistrationProfile.DoesNotExist:
                logger.info('Rejected sender without RegistrationProfile: %s: %s', sender, params.get('from'))
                return HttpResponse(status=406)
            if registration.activated:
                logger.info('Rejected activated sender: %s: %s', sender, params.get('from'))
                return HttpResponse(status=406)

            key = registration.activation_key
            if key in params.get('body-plain', '') or key in params.get('body-html', ''):
                if RegistrationProfile.objects.activate_user(key, get_current_site(request)):
                    logger.info('Activated sender: %s: %s', sender, params.get('from'))
                    return HttpResponse('Activated', status=200)
                logger.info('Failed to activate sender: %s: %s', sender, params.get('from'))
            else:
                logger.info('Activation key not found: %s: %s', sender, params.get('from'))
            return HttpResponse(status=406)
Example #5
0
File: pwned.py Project: DMOJ/site
def pwned_password(password):
    """
    Checks a password against the Pwned Passwords database.
    """
    if not isinstance(password, string_types):
        raise TypeError('Password values to check must be strings.')
    password_hash = hashlib.sha1(utf8bytes(password)).hexdigest().upper()
    prefix, suffix = password_hash[:5], password_hash[5:]
    results = _get_pwned(prefix)
    if results is None:
        # Gracefully handle timeouts and HTTP error response codes.
        return None
    return results.get(suffix, 0)
Example #6
0
def gravatar(email, size=80, default=None):
    if isinstance(email, Profile):
        if default is None:
            default = email.mute
        email = email.user.email
    elif isinstance(email, AbstractUser):
        email = email.email

    gravatar_url = '//www.gravatar.com/avatar/' + hashlib.md5(utf8bytes(email.strip().lower())).hexdigest() + '?'
    args = {'d': 'identicon', 's': str(size)}
    if default:
        args['f'] = 'y'
    gravatar_url += urlencode(args)
    return gravatar_url
Example #7
0
def gravatar(email, size=80, default=None):
    if isinstance(email, Profile):
        if default is None:
            default = email.mute
        email = email.user.email
    elif isinstance(email, AbstractUser):
        email = email.email

    gravatar_url = 'https://www.gravatar.com/avatar/' + hashlib.md5(utf8bytes(email.strip().lower())).hexdigest() + '?'
    args = {'d': 'identicon', 's': str(size)}
    if default:
        args['f'] = 'y'
    gravatar_url += urlencode(args)
    return gravatar_url
Example #8
0
File: texoid.py Project: DMOJ/site
    def query_texoid(self, document, hash):
        self.cache.create(hash)

        try:
            response = requests.post(settings.TEXOID_URL, data=utf8bytes(document), headers={
                'Content-Type': 'application/x-tex'
            })
            response.raise_for_status()
        except requests.HTTPError as e:
            if e.response.status == 400:
                logger.error('Texoid failed to render: %s\n%s', document, e.response.text)
            else:
                logger.exception('Failed to connect to texoid for: %s' % document)
            return
        except Exception:
            logger.exception('Failed to connect to texoid for: %s' % document)
            return

        try:
            data = response.json()
        except ValueError:
            logger.exception('Invalid texoid response for: %s\n%s', document, response.text)
            return

        if not data['success']:
            logger.error('Texoid failure for: %s\n%s', document, data)
            return {'error': data['error']}

        meta = data['meta']
        self.cache.cache_data(hash, 'meta', utf8bytes(json.dumps(meta)), url=False, gzip=False)

        result = {
            'png': self.cache.cache_data(hash, 'png', b64decode(data['png'])),
            'svg': self.cache.cache_data(hash, 'svg', data['svg'].encode('utf-8')),
            'meta': meta,
        }
        return result
Example #9
0
    def query_texoid(self, document, hash):
        self.cache.create(hash)

        try:
            response = requests.post(
                settings.TEXOID_URL,
                data=utf8bytes(document),
                headers={'Content-Type': 'application/x-tex'})
            response.raise_for_status()
        except requests.HTTPError as e:
            if e.response.status == 400:
                logger.error('Texoid failed to render: %s\n%s', document,
                             e.response.text)
            else:
                logger.exception('Failed to connect to texoid for: %s' %
                                 document)
            return
        except Exception:
            logger.exception('Failed to connect to texoid for: %s' % document)
            return

        try:
            data = response.json()
        except ValueError:
            logger.exception('Invalid texoid response for: %s\n%s', document,
                             response.text)
            return

        if not data['success']:
            logger.error('Texoid failure for: %s\n%s', document, data)
            return {'error': data['error']}

        meta = data['meta']
        self.cache.cache_data(hash,
                              'meta',
                              json.dumps(meta),
                              url=False,
                              gzip=False)

        result = {
            'png': self.cache.cache_data(hash, 'png',
                                         data['png'].decode('base64')),
            'svg': self.cache.cache_data(hash, 'svg',
                                         data['svg'].encode('utf-8')),
            'meta': meta,
        }
        return result
Example #10
0
    def get_result(self, formula):
        if self.type == 'tex':
            return

        hash = hashlib.sha1(utf8bytes(formula)).hexdigest()
        formula = utf8text(formula)
        if self.cache.has_file(hash, 'css'):
            result = self.query_cache(hash)
        else:
            result = self.query_mathoid(formula, hash)

        if not result:
            return None

        result['tex'] = formula
        result['display'] = formula.startswith(r'\displaystyle')
        return {
            'mml': self.output_mml,
            'msp': self.output_msp,
            'svg': self.output_svg,
            'jax': self.output_jax,
            'png': self.output_png,
            'raw': lambda x: x,
        }[self.type](result)
Example #11
0
def gravatar(email, size=80, default=None):
    if isinstance(email, Profile):
        if default is None:
            default = email.mute
        email = email.user.email
    elif isinstance(email, AbstractUser):
        email = email.email

    email_hash = hashlib.md5(utf8bytes(email.strip().lower())).hexdigest()
    gravatar_url = 'http://www.gravatar.com/avatar/' + email_hash + '?'
    args = {'d': 'identicon', 's': str(size)}
    if default:
        args['f'] = 'y'
    gravatar_url += urlencode(args)
    gravatar_dir = os.path.join("gravatar", str(size))
    gravatar_cache_dir = os.path.join(settings.STATIC_ROOT, gravatar_dir)
    if not os.path.exists(gravatar_cache_dir):
        os.makedirs(gravatar_cache_dir)
    gravatar_cache_file = os.path.join(gravatar_cache_dir, email_hash)
    if not os.path.exists(gravatar_cache_file):
        image = requests.get(gravatar_url)
        with open(gravatar_cache_file, "wb") as f:
            f.write(image.content)
    return static(os.path.join(gravatar_dir, email_hash))
Example #12
0
 def image_url(self, url):
     return '%s/%s/%s' % (self.server,
                          hmac.new(utf8bytes(self.key), utf8bytes(url),
                                   sha1).hexdigest(), utf8bytes(url).hex())
Example #13
0
 def get_id_secret(cls, sub_id):
     return (hmac.new(utf8bytes(settings.EVENT_DAEMON_SUBMISSION_KEY), b'%d' % sub_id, hashlib.sha512)
                 .hexdigest()[:16] + '%08x' % sub_id)
Example #14
0
File: user.py Project: ziap/tgboj
def prepare_user_data(self, profile_id, options):
    options = json.loads(options)
    with Progress(self, 2, stage=_('Applying filters')) as p:
        # Force an update so that we get a progress bar.
        p.done = 0
        submissions = apply_submission_filter(
            Submission.objects.select_related(
                'problem', 'language', 'source').filter(user_id=profile_id),
            options,
        )
        p.did(1)
        comments = apply_comment_filter(
            Comment.objects.filter(author_id=profile_id), options)
        p.did(1)

    with zipfile.ZipFile(os.path.join(settings.DMOJ_USER_DATA_CACHE,
                                      '%s.zip' % profile_id),
                         mode='w') as data_file:
        submission_count = len(submissions)
        if submission_count:
            submission_info = {}
            with Progress(self,
                          submission_count,
                          stage=_('Preparing your submission data')) as p:
                prepared = 0
                interval = max(submission_count // 10, 1)
                for submission in submissions:
                    submission_info[submission.id] = {
                        'problem': submission.problem.code,
                        'date': submission.date.isoformat(),
                        'time': submission.time,
                        'memory': submission.memory,
                        'language': submission.language.key,
                        'status': submission.status,
                        'result': submission.result,
                        'case_points': submission.case_points,
                        'case_total': submission.case_total,
                    }
                    with data_file.open(
                            'submissions/%s.%s' %
                        (submission.id, submission.language.extension),
                            'w',
                    ) as f:
                        f.write(utf8bytes(submission.source.source))

                    prepared += 1
                    if prepared % interval == 0:
                        p.done = prepared

                with data_file.open('submissions/info.json', 'w') as f:
                    f.write(
                        utf8bytes(
                            json.dumps(submission_info,
                                       sort_keys=True,
                                       indent=4)))

        comment_count = len(comments)
        if comment_count:
            comment_info = {}
            with Progress(self,
                          comment_count,
                          stage=_('Preparing your comment data')) as p:
                prepared = 0
                interval = max(comment_count // 10, 1)
                for comment in comments:
                    related_object = {
                        'b': 'blog post',
                        'c': 'contest',
                        'p': 'problem',
                        's': 'problem editorial',
                    }
                    comment_info[comment.id] = {
                        'date': comment.time.isoformat(),
                        'related_object': related_object[comment.page[0]],
                        'page': comment.page[2:],
                        'score': comment.score,
                    }
                    with data_file.open('comments/%s.txt' % comment.id,
                                        'w') as f:
                        f.write(utf8bytes(comment.body))

                    prepared += 1
                    if prepared % interval == 0:
                        p.done = prepared

                with data_file.open('comments/info.json', 'w') as f:
                    f.write(
                        utf8bytes(
                            json.dumps(comment_info, sort_keys=True,
                                       indent=4)))

    return submission_count + comment_count
Example #15
0
File: camo.py Project: DMOJ/site
 def image_url(self, url):
     return '%s/%s/%s' % (self.server,
                          hmac.new(utf8bytes(self.key), utf8bytes(url), sha1).hexdigest(),
                          utf8bytes(url).hex())
Example #16
0
 def get_id_secret(cls, sub_id):
     return (hmac.new(utf8bytes(settings.EVENT_DAEMON_SUBMISSION_KEY), b'%d' % sub_id, hashlib.sha512).hexdigest()[:16] +
             '%08x' % sub_id)