コード例 #1
0
ファイル: views.py プロジェクト: pombredanne/dmirr
def mirrorlist(request):
    data = {}
    resources = []
    remote = request.environ[settings.DMIRR_REMOTE_ADDR_KEY]

    client = get_geodata_by_ip(remote)
    repo = get_object_or_404(db.ProjectRepo,
                             label=request.GET.get('repo', None))
    arch = get_object_or_404(db.Arch, label=request.GET.get('arch', None))
    protocol = get_object_or_404(db.Protocol,
                                 label=request.GET.get('protocol', 'http'))

    key = "%s-%s-%s-%s" % (remote, repo.id, arch.id, protocol.id)

    cached_data = cache.get(key)
    if cached_data:
        data = cached_data
    else:
        for resource in repo.project.resources.all():
            if not resource.include_in_mirrorlist:
                continue
            if protocol in resource.protocols.all() and \
               arch in repo.archs.all():
                full_uri = "%s://%s%s%s/" % (
                    protocol.label,
                    resource.system.label,
                    resource.path,
                    re.sub('@arch@', arch.label, repo.path.strip('/')),
                )
                if not client:
                    distance = 'unknown'
                else:
                    distance = get_distance(
                        (client['latitude'], client['longitude']),
                        (resource.system.latitude, resource.system.longitude),
                    )
                resources.append((distance, full_uri, resource))

        resources.sort()
        if not client:
            data['location'] = 'Unknown Location (%s)' % remote
        else:
            loc = get_location(client.get('city', None),
                               client.get('region_name', None),
                               client.get('country_name', None))
            data['location'] = '%s (%s)' % (loc, remote)

        data['resources'] = resources
        cache.set(key, data)

    return render(request,
                  'mirrorlist/list.html',
                  data,
                  content_type='text/plain')
コード例 #2
0
ファイル: views.py プロジェクト: derks/dmirr
def mirrorlist(request):
    data = {}
    resources = []
    remote = request.environ[settings.DMIRR_REMOTE_ADDR_KEY]

    client = get_geodata_by_ip(remote)
    repo = get_object_or_404(db.ProjectRepo, 
                             label=request.GET.get('repo', None))
    arch = get_object_or_404(db.Arch, 
                             label=request.GET.get('arch', None))
    protocol = get_object_or_404(db.Protocol, 
                             label=request.GET.get('protocol', 'http'))
    
    key = "%s-%s-%s-%s" % (remote, repo.id, arch.id, protocol.id)

    cached_data = cache.get(key)
    if cached_data:
        data = cached_data
    else:
        for resource in repo.project.resources.all():
            if not resource.include_in_mirrorlist:
                continue
            if protocol in resource.protocols.all() and \
               arch in repo.archs.all():
                full_uri = "%s://%s%s%s/" % (
                    protocol.label, 
                    resource.system.label,
                    resource.path,
                    re.sub('@arch@', arch.label, repo.path.strip('/')),
                    )
                if not client:
                    distance = 'unknown'
                else:
                    distance = get_distance( 
                        (client['latitude'], client['longitude']), 
                        (resource.system.latitude, resource.system.longitude),
                        )
                resources.append((distance, full_uri, resource))
            
        resources.sort()
        if not client:
            data['location'] = 'Unknown Location (%s)' % remote
        else:
            loc = get_location(client.get('city', None), 
                               client.get('region_name', None), 
                               client.get('country_name', None))
            data['location'] = '%s (%s)' % (loc, remote)
            
        data['resources'] = resources
        cache.set(key, data)
    
    return render(request, 'mirrorlist/list.html', data, 
                  content_type='text/plain')
コード例 #3
0
ファイル: models.py プロジェクト: jness/dmirr
    def save(self):
        '''custom save function to pull geoip'''

        # extract the domain from Repo URL and then resolve said domain
        ip = socket.gethostbyname(self.label)
        geodata = get_geodata_by_ip(ip)
        self.ip = ip
        self.country = geodata.get('country_name', None)
        self.country_code = geodata.get('country_code', None)
        self.city = geodata.get('city', None)
        self.region = geodata.get('region_name', None)
        self.longitude = geodata.get('longitude', None)
        self.latitude = geodata.get('latitude', None)
        self.postal_code = geodata.get('postal_code', None)
        super(System, self).save()
コード例 #4
0
ファイル: models.py プロジェクト: pombredanne/dmirr
                self.longitude = geodata[1][1]
            except IndexError, e:
                raise IndexError(
                    "Unable to extract longitude and latitude from %s" %
                    geodata)

            try:
                location = geodata[0].split(', ')
                self.country_code = location[2]
            except IndexError:
                raise IndexError("Unable to extract country_code from %s" %
                                 geodata)

        else:
            # Attempt lookup using GeoData
            geodata = get_geodata_by_ip(ip)
            if not geodata:
                raise ValidationError(
                    'Unable to determine location from IP address, please enter manually'
                )
            self.country = geodata.get('country_name', None)
            self.country_code = geodata.get('country_code', None)
            self.city = geodata.get('city', None)
            self.region = geodata.get('region_name', None)
            self.longitude = geodata.get('longitude', None)
            self.latitude = geodata.get('latitude', None)
            self.postal_code = geodata.get('postal_code', None)

        self.ip = ip
        super(System, self).save()
コード例 #5
0
ファイル: models.py プロジェクト: derks/dmirr
            geodata = get_geodata_by_region(*args)
            try:
                self.latitude = geodata[1][0]
                self.longitude = geodata[1][1]
            except IndexError, e:
                raise IndexError("Unable to extract longitude and latitude from %s" % geodata)

            try:
                location = geodata[0].split(', ')
                self.country_code = location[2]
            except IndexError:
                raise IndexError("Unable to extract country_code from %s" % geodata)

        else:
            # Attempt lookup using GeoData
            geodata = get_geodata_by_ip(ip)
            if not geodata:
                raise ValidationError('Unable to determine location from IP address, please enter manually')
            self.country = geodata.get('country_name', None)
            self.country_code = geodata.get('country_code', None)
            self.city = geodata.get('city', None)
            self.region = geodata.get('region_name', None)
            self.longitude = geodata.get('longitude', None)
            self.latitude = geodata.get('latitude', None)
            self.postal_code = geodata.get('postal_code', None)

        self.ip = ip
        super(System, self).save()
        
    @property
    def display_name(self):