<commit_before>from django.contrib import admin
from django.conf.urls import patterns, url
from django.shortcuts import redirect
from django.http import Http404
from django.template.response import TemplateResponse

from us_ignite.hubs.forms import HubApprovalRequestForm
from us_ignite.hubs.models import Hub, HubActivity, HubRequest


def get_hub_from_request(instance):
    """Transform the instance in a request"""
    return Hub.objects.create(
        name=instance.name,
        contact=instance.user,
        summary=instance.summary,
        description=instance.description,
        website=instance.website
    )


class HubRequestAdmin(admin.ModelAdmin):
    list_display = ('name', 'status', 'created')
    search_fields = ('name', 'description')
    date_hierarchy = 'created'
    list_filter = ('status', 'created')

    def get_urls(self):
        _approve_request = self.admin_site.admin_view(self.approve_request)
        urls = patterns(
            '',
            url(r'^approve/(?P<request_id>\d+)/$', _approve_request,
                name='approve_hub_request')
        )
        urls += super(HubRequestAdmin, self).get_urls()
        return urls

    def approve_request(self, request, request_id):
        """Aprove the user request to own a community."""
        try:
            instance = HubRequest.objects.get(id=request_id)
        except HubRequest.DoesNotExist:
            raise Http404
        # Determine if this request has been processed:
        if not instance.is_pending():
            raise Http404
        if request.method == 'POST':
            form = HubApprovalRequestForm(request.POST, instance=instance)
            if form.is_valid():
                instance = form.save()
                # Process the request approval.
                if instance.is_approved():
                    hub = get_hub_from_request(instance)
                    instance.hub = hub
                    instance.save()
                self.message_user(request, 'Hub request has been updated.')
                return redirect(
                    'admin:hubs_hubrequest_change', request_id)
        else:
            form = HubApprovalRequestForm(instance=instance)
        context = {
            'object': instance,
            'form': form,
            'title': 'Resolve Ignite community request',
        }
        return TemplateResponse(
            request, 'admin/hubs/request_approval.html', context)


class HubAdmin(admin.ModelAdmin):
    raw_id_fields = ('contact', )
    list_display = ('name', 'slug', 'status', 'created')
    search_fields = ('name', 'slug', 'description', 'summary')
    date_hierarchy = 'created'
    list_filter = ('status', 'created')


class HubActivityAdmin(admin.ModelAdmin):
    list_display = ('name', 'created')
    search_fields = ('name', 'description')
    date_hierarchy = 'created'
    list_filter = ('created', )


admin.site.register(Hub, HubAdmin)
admin.site.register(HubActivity, HubActivityAdmin)
admin.site.register(HubRequest, HubRequestAdmin)
<commit_msg>Add ``NetworkSpeed`` section in the admin.<commit_after>from django.contrib import admin
from django.conf.urls import patterns, url
from django.shortcuts import redirect
from django.http import Http404
from django.template.response import TemplateResponse

from us_ignite.hubs.forms import HubApprovalRequestForm
from us_ignite.hubs.models import Hub, HubActivity, HubRequest, NetworkSpeed


def get_hub_from_request(instance):
    """Transform the instance in a request"""
    return Hub.objects.create(
        name=instance.name,
        contact=instance.user,
        summary=instance.summary,
        description=instance.description,
        website=instance.website
    )


class HubRequestAdmin(admin.ModelAdmin):
    list_display = ('name', 'status', 'created')
    search_fields = ('name', 'description')
    date_hierarchy = 'created'
    list_filter = ('status', 'created')

    def get_urls(self):
        _approve_request = self.admin_site.admin_view(self.approve_request)
        urls = patterns(
            '',
            url(r'^approve/(?P<request_id>\d+)/$', _approve_request,
                name='approve_hub_request')
        )
        urls += super(HubRequestAdmin, self).get_urls()
        return urls

    def approve_request(self, request, request_id):
        """Aprove the user request to own a community."""
        try:
            instance = HubRequest.objects.get(id=request_id)
        except HubRequest.DoesNotExist:
            raise Http404
        # Determine if this request has been processed:
        if not instance.is_pending():
            raise Http404
        if request.method == 'POST':
            form = HubApprovalRequestForm(request.POST, instance=instance)
            if form.is_valid():
                instance = form.save()
                # Process the request approval.
                if instance.is_approved():
                    hub = get_hub_from_request(instance)
                    instance.hub = hub
                    instance.save()
                self.message_user(request, 'Hub request has been updated.')
                return redirect(
                    'admin:hubs_hubrequest_change', request_id)
        else:
            form = HubApprovalRequestForm(instance=instance)
        context = {
            'object': instance,
            'form': form,
            'title': 'Resolve Ignite community request',
        }
        return TemplateResponse(
            request, 'admin/hubs/request_approval.html', context)


class HubAdmin(admin.ModelAdmin):
    raw_id_fields = ('contact', )
    list_display = ('name', 'slug', 'status', 'created')
    search_fields = ('name', 'slug', 'description', 'summary')
    date_hierarchy = 'created'
    list_filter = ('status', 'created')


class HubActivityAdmin(admin.ModelAdmin):
    list_display = ('name', 'created')
    search_fields = ('name', 'description')
    date_hierarchy = 'created'
    list_filter = ('created', )


class NetworkSpeedAdmin(admin.ModelAdmin):
    list_display = ('name', 'slug')


admin.site.register(Hub, HubAdmin)
admin.site.register(HubActivity, HubActivityAdmin)
admin.site.register(HubRequest, HubRequestAdmin)
admin.site.register(NetworkSpeed, NetworkSpeedAdmin)

{'fraction_non_alphanumeric': 0.10560792458859243, 'fraction_numerical': 0.0028758587633807317, 'mean_word_length': 2.6672524897480963, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1843332', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1682, 'n_words': 395}