<commit_before>import datetime

from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models
from django.utils import timezone
from django.utils.deconstruct import deconstructible

import requests

from storages.backends.s3boto import S3BotoStorage

User = settings.AUTH_USER_MODEL


@deconstructible
class MyS3BotoStorage(S3BotoStorage):
    pass


class DataFile(models.Model):
    user = models.ForeignKey(User)
    datafile = models.FileField(
        storage=MyS3BotoStorage(acl='private',
                                querystring_auth=True,
                                querystring_expire=600))
    description = models.TextField(default='')

    def __unicode__(self):
        return ':'.join([self.user.email, self.datafile.name])


class DataLink(models.Model):
    user = models.ForeignKey(User)
    url = models.TextField(default='')
    name = models.TextField(default='')
    description = models.TextField(default='')

    def __unicode__(self):
        return ':'.join([self.user.email, self.name])


class SiteConfig(models.Model):
    """
    Site configuration, customize with additional information and descriptions.
    """
    site = models.OneToOneField(Site)
    source_name = models.CharField(max_length=40)
    data_page_explanation = models.TextField(default='')


class OpenHumansConfig(models.Model):
    """
    Site configuration for data export to Open Humans. Only one should exist.
    """
    site = models.OneToOneField(Site)
    source_name = models.CharField(max_length=255)

    token_url = settings.OPEN_HUMANS_SERVER + '/oauth2/token/'

    @property
    def auth_url(self):
        return (settings.OPEN_HUMANS_SERVER +
                '/oauth2/authorize?client_id={}&response_type=code'.format(
                    settings.OPEN_HUMANS_CLIENT_ID))

    @property
    def return_url(self):
        return '{}/study/{}/return/'.format(
            settings.OPEN_HUMANS_SERVER, self.source_name)


class OpenHumansUser(models.Model):
    """
    Handle data for a user's Open Humans account connection.
    """
    user = models.OneToOneField(User)
    openhumans_userid = models.PositiveIntegerField(null=True)
    access_token = models.CharField(max_length=60, blank=True)
    refresh_token = models.CharField(max_length=60, blank=True)
    token_expiration = models.DateTimeField(null=True)

    def _refresh_tokens(self):
        site = Site.objects.get_current()

        response_refresh = requests.post(
            site.openhumansconfig.token_url,
            data={
                'grant_type': 'refresh_token',
                'refresh_token': self.refresh_token,
                'client_id': settings.OPEN_HUMANS_CLIENT_ID,
                'client_secret': settings.OPEN_HUMANS_CLIENT_SECRET,
            })
<commit_msg>Fix bad URL for Open Humans interaction<commit_after>import datetime

from django.conf import settings
from django.contrib.sites.models import Site
from django.db import models
from django.utils import timezone
from django.utils.deconstruct import deconstructible

import requests

from storages.backends.s3boto import S3BotoStorage

User = settings.AUTH_USER_MODEL


@deconstructible
class MyS3BotoStorage(S3BotoStorage):
    pass


class DataFile(models.Model):
    user = models.ForeignKey(User)
    datafile = models.FileField(
        storage=MyS3BotoStorage(acl='private',
                                querystring_auth=True,
                                querystring_expire=600))
    description = models.TextField(default='')

    def __unicode__(self):
        return ':'.join([self.user.email, self.datafile.name])


class DataLink(models.Model):
    user = models.ForeignKey(User)
    url = models.TextField(default='')
    name = models.TextField(default='')
    description = models.TextField(default='')

    def __unicode__(self):
        return ':'.join([self.user.email, self.name])


class SiteConfig(models.Model):
    """
    Site configuration, customize with additional information and descriptions.
    """
    site = models.OneToOneField(Site)
    source_name = models.CharField(max_length=40)
    data_page_explanation = models.TextField(default='')


class OpenHumansConfig(models.Model):
    """
    Site configuration for data export to Open Humans. Only one should exist.
    """
    site = models.OneToOneField(Site)
    source_name = models.CharField(max_length=255)

    token_url = settings.OPEN_HUMANS_SERVER + '/oauth2/token/'

    @property
    def auth_url(self):
        return (settings.OPEN_HUMANS_SERVER +
                '/oauth2/authorize?client_id={}&response_type=code'.format(
                    settings.OPEN_HUMANS_CLIENT_ID) +
                '&scope=wildlife%20read%20write')

    @property
    def return_url(self):
        return '{}/study/{}/return/'.format(
            settings.OPEN_HUMANS_SERVER, self.source_name)


class OpenHumansUser(models.Model):
    """
    Handle data for a user's Open Humans account connection.
    """
    user = models.OneToOneField(User)
    openhumans_userid = models.PositiveIntegerField(null=True)
    access_token = models.CharField(max_length=60, blank=True)
    refresh_token = models.CharField(max_length=60, blank=True)
    token_expiration = models.DateTimeField(null=True)

    def _refresh_tokens(self):
        site = Site.objects.get_current()

        response_refresh = requests.post(
            site.openhumansconfig.token_url,
            data={
                'grant_type': 'refresh_token',
                'refresh_token': self.refresh_token,
                'client_id': settings.OPEN_HUMANS_CLIENT_ID,
                'client_secret': settings.OPEN_HUMANS_CLIENT_SECRET,
            })

{'fraction_non_alphanumeric': 0.10117071466014328, 'fraction_numerical': 0.007338808317316093, 'mean_word_length': 3.0973514674302076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4584648', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1644, 'n_words': 317}