Beispiel #1
0
 def refresh(self):
     stats = GlobalStats()
     data = {
         "secret": self.secret,
         "site_url": get_site_url(),
         "site_title": settings.SITE_TITLE,
         "users": User.objects.count(),
         "projects": Project.objects.count(),
         "components": Component.objects.count(),
         "languages": stats.languages,
         "source_strings": stats.source_strings,
     }
     ssh_key = get_key_data()
     if not ssh_key:
         generate_ssh_key(None)
         ssh_key = get_key_data()
     if ssh_key:
         data["ssh_key"] = ssh_key["key"]
     response = request("post", settings.SUPPORT_API_URL, data=data)
     response.raise_for_status()
     payload = response.json()
     self.name = payload["name"]
     self.expiry = dateutil.parser.parse(payload["expiry"])
     self.in_limits = payload["in_limits"]
     if payload["backup_repository"]:
         BackupService.objects.get_or_create(
             repository=payload["backup_repository"],
             defaults={"enabled": False})
Beispiel #2
0
 def refresh(self):
     stats = GlobalStats()
     data = {
         'secret': self.secret,
         'site_url': get_site_url(),
         'site_title': settings.SITE_TITLE,
         'users': User.objects.count(),
         'projects': Project.objects.count(),
         'components': Component.objects.count(),
         'languages': stats.languages,
         'source_strings': stats.source_strings,
     }
     ssh_key = get_key_data()
     if not ssh_key:
         generate_ssh_key(None)
         ssh_key = get_key_data()
     if ssh_key:
         data['ssh_key'] = ssh_key['key']
     response = request('post', settings.SUPPORT_API_URL, data=data)
     response.raise_for_status()
     payload = response.json()
     self.name = payload['name']
     self.expiry = dateutil.parser.parse(payload['expiry'])
     self.in_limits = payload['in_limits']
     if payload['backup_repository']:
         BackupService.objects.get_or_create(
             repository=payload['backup_repository'],
             defaults={"enabled": False})
Beispiel #3
0
def download_version_info():
    response = request("get", PYPI)
    result = []
    for version, info in response.json()["releases"].items():
        if not info:
            continue
        result.append(Release(version, parse(info[0]["upload_time"])))
    return sorted(result, key=lambda x: x[1], reverse=True)
Beispiel #4
0
    def request(self, method, url, skip_auth=False, **kwargs):
        """Perform JSON request."""
        # Create custom headers
        headers = {
            "Referer": get_site_url(),
            "Accept": "application/json; charset=utf-8",
        }
        if "headers" in kwargs:
            headers.update(kwargs.pop("headers"))
        # Optional authentication
        if not skip_auth:
            headers.update(self.get_authentication())

        # Fire request
        return request(method, url, headers=headers, timeout=5.0, **kwargs)
Beispiel #5
0
def cdn_parse_html(files: str, selector: str, component_id: int):
    component = Component.objects.get(pk=component_id)
    source_translation = component.source_translation
    source_units = set(
        source_translation.unit_set.values_list("source", flat=True))
    units = []
    errors = []

    for filename in files.splitlines():
        filename = filename.strip()
        try:
            if filename.startswith("http://") or filename.startswith(
                    "https://"):
                with request("get", filename) as handle:
                    content = handle.read()
            else:
                with open(os.path.join(component.full_path, filename),
                          "r") as handle:
                    content = handle.read()
        except IOError as error:
            errors.append({"filename": filename, "error": str(error)})
            continue

        document = html.fromstring(content)

        for element in document.cssselect(selector):
            text = element.text
            if (element.getchildren() or not text or text in source_units
                    or text in units):
                continue
            units.append(text)

    # Actually create units
    if units:
        source_translation.new_unit(
            request=None,
            key=None,
            value=None,
            batch={calculate_checksum(text): text
                   for text in units},
        )

    if errors:
        component.add_alert("CDNAddonError", occurrences=errors)
    else:
        component.delete_alert("CDNAddonError")
Beispiel #6
0
def get_github_email(access_token):
    """Get real e-mail from GitHub."""
    response = request(
        "get",
        "https://api.github.com/user/emails",
        headers={"Authorization": "token {0}".format(access_token)},
        timeout=10.0,
    )
    data = response.json()
    email = None
    for entry in data:
        # Skip not verified ones
        if not entry["verified"]:
            continue
        email = entry["email"]
        if entry["primary"]:
            break
    return email
Beispiel #7
0
 def refresh(self):
     stats = GlobalStats()
     data = {
         "secret": self.secret,
         "site_url": get_site_url(),
         "site_title": settings.SITE_TITLE,
         "users": User.objects.count(),
         "projects": Project.objects.count(),
         "components": Component.objects.count(),
         "languages": stats.languages,
         "source_strings": stats.source_strings,
         "strings": stats.all,
         "words": stats.all_words,
     }
     if self.discoverable:
         data["discoverable"] = 1
         data["public_projects"] = json.dumps(
             [
                 {
                     "name": project.name,
                     "url": project.get_absolute_url(),
                     "web": project.web,
                 }
                 for project in Project.objects.filter(
                     access_control=Project.ACCESS_PUBLIC
                 ).iterator()
             ]
         )
     ssh_key = get_key_data()
     if not ssh_key:
         generate_ssh_key(None)
         ssh_key = get_key_data()
     if ssh_key:
         data["ssh_key"] = ssh_key["key"]
     response = request("post", settings.SUPPORT_API_URL, data=data)
     response.raise_for_status()
     payload = response.json()
     self.name = payload["name"]
     self.expiry = dateutil.parser.parse(payload["expiry"])
     self.in_limits = payload["in_limits"]
     if payload["backup_repository"]:
         BackupService.objects.get_or_create(
             repository=payload["backup_repository"], defaults={"enabled": False}
         )
Beispiel #8
0
    def request(self, method, url, skip_auth=False, **kwargs):
        """Perform JSON request."""
        # Create custom headers
        headers = {
            "Referer": get_site_url(),
            "Accept": "application/json; charset=utf-8",
        }
        if "headers" in kwargs:
            headers.update(kwargs.pop("headers"))
        # Optional authentication
        if not skip_auth:
            headers.update(self.get_authentication())

        # Fire request
        response = request(method, url, headers=headers, timeout=5.0, **kwargs)

        # Directly raise error when response is empty
        if response.content:
            response.raise_for_status()

        return response
Beispiel #9
0
def download_avatar_image(user, size):
    """Download avatar image from remote server."""
    url = avatar_for_email(user.email, size)
    response = request("get", url, timeout=1.0)
    return response.content