from django.contrib import admin

from .models import Partner, Stream, Contact, Language


class LanguageAdmin(admin.ModelAdmin):
    search_fields = ('language',)
    list_display = ('language',)

admin.site.register(Language, LanguageAdmin)


class PartnerAdmin(admin.ModelAdmin):
    search_fields = ('company_name',)
    list_display = ('company_name', 'description', 'id', 'get_languages')

admin.site.register(Partner, PartnerAdmin)



class StreamAdmin(admin.ModelAdmin):
    search_fields = ('partner__company_name', 'name',)
    list_display = ('id', 'partner', 'name', 'description', 'get_languages')

admin.site.register(Stream, StreamAdmin)



class ContactAdmin(admin.ModelAdmin):
    search_fields = ('partner__company_name', 'full_name', 'short_name',)
    list_display = ('id', 'title', 'full_name', 'partner', 'email',)

admin.site.register(Contact, ContactAdmin)

from django import forms
from django.contrib import admin
from TWLight.users.groups import get_coordinators

from .models import Partner, Stream, Contact, Language


class LanguageAdmin(admin.ModelAdmin):
    search_fields = ('language',)
    list_display = ('language',)

admin.site.register(Language, LanguageAdmin)


class PartnerAdmin(admin.ModelAdmin):
    class CustomModelChoiceField(forms.ModelChoiceField):
        """
        This lets us relabel the users in the dropdown with their recognizable
        wikipedia usernames, rather than their cryptic local IDs. It should be
        used only for the coordinator field.
        """
        def label_from_instance(self, obj):
            return '{editor.wp_username} ({editor.home_wiki})'.format(
                editor=obj.editor)


    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        """
        The coordinator dropdown should limit choices to actual coordinators,
        for admin ease of use.
        """
        if db_field.name == "coordinator":
            return self.CustomModelChoiceField(
                queryset=get_coordinators().user_set.all())
        return super(PartnerAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs)


    search_fields = ('company_name',)
    list_display = ('company_name', 'description', 'id', 'get_languages')

admin.site.register(Partner, PartnerAdmin)



class StreamAdmin(admin.ModelAdmin):
    search_fields = ('partner__company_name', 'name',)
    list_display = ('id', 'partner', 'name', 'description', 'get_languages')

admin.site.register(Stream, StreamAdmin)



class ContactAdmin(admin.ModelAdmin):
    search_fields = ('partner__company_name', 'full_name', 'short_name',)
    list_display = ('id', 'title', 'full_name', 'partner', 'email',)

admin.site.register(Contact, ContactAdmin)

Improve usability of coordinator designation interaction
Improve usability of coordinator designation interaction

I'm limiting the dropdown to actual coordinators so that admins don't
have to scroll through a giant list. I don't want to enforce/validate
this on the database level, though, as people may proceed through the
coordinator designation process in different orders, and also the exact
set of coordinators may change over time (and therefore the database
might be left with a previously-valid coordinator in an instance
record).

+ from django import forms
  from django.contrib import admin
+ from TWLight.users.groups import get_coordinators
  
  from .models import Partner, Stream, Contact, Language
  
  
  class LanguageAdmin(admin.ModelAdmin):
      search_fields = ('language',)
      list_display = ('language',)
  
  admin.site.register(Language, LanguageAdmin)
  
  
  class PartnerAdmin(admin.ModelAdmin):
+     class CustomModelChoiceField(forms.ModelChoiceField):
+         """
+         This lets us relabel the users in the dropdown with their recognizable
+         wikipedia usernames, rather than their cryptic local IDs. It should be
+         used only for the coordinator field.
+         """
+         def label_from_instance(self, obj):
+             return '{editor.wp_username} ({editor.home_wiki})'.format(
+                 editor=obj.editor)
+ 
+ 
+     def formfield_for_foreignkey(self, db_field, request, **kwargs):
+         """
+         The coordinator dropdown should limit choices to actual coordinators,
+         for admin ease of use.
+         """
+         if db_field.name == "coordinator":
+             return self.CustomModelChoiceField(
+                 queryset=get_coordinators().user_set.all())
+         return super(PartnerAdmin, self).formfield_for_foreignkey(
+             db_field, request, **kwargs)
+ 
+ 
      search_fields = ('company_name',)
      list_display = ('company_name', 'description', 'id', 'get_languages')
  
  admin.site.register(Partner, PartnerAdmin)
  
  
  
  class StreamAdmin(admin.ModelAdmin):
      search_fields = ('partner__company_name', 'name',)
      list_display = ('id', 'partner', 'name', 'description', 'get_languages')
  
  admin.site.register(Stream, StreamAdmin)
  
  
  
  class ContactAdmin(admin.ModelAdmin):
      search_fields = ('partner__company_name', 'full_name', 'short_name',)
      list_display = ('id', 'title', 'full_name', 'partner', 'email',)
  
  admin.site.register(Contact, ContactAdmin)
  
Improve usability of coordinator designation interaction
## Code Before:
from django.contrib import admin

from .models import Partner, Stream, Contact, Language


class LanguageAdmin(admin.ModelAdmin):
    search_fields = ('language',)
    list_display = ('language',)

admin.site.register(Language, LanguageAdmin)


class PartnerAdmin(admin.ModelAdmin):
    search_fields = ('company_name',)
    list_display = ('company_name', 'description', 'id', 'get_languages')

admin.site.register(Partner, PartnerAdmin)



class StreamAdmin(admin.ModelAdmin):
    search_fields = ('partner__company_name', 'name',)
    list_display = ('id', 'partner', 'name', 'description', 'get_languages')

admin.site.register(Stream, StreamAdmin)



class ContactAdmin(admin.ModelAdmin):
    search_fields = ('partner__company_name', 'full_name', 'short_name',)
    list_display = ('id', 'title', 'full_name', 'partner', 'email',)

admin.site.register(Contact, ContactAdmin)

## Instruction:
Improve usability of coordinator designation interaction
## Code After:
from django import forms
from django.contrib import admin
from TWLight.users.groups import get_coordinators

from .models import Partner, Stream, Contact, Language


class LanguageAdmin(admin.ModelAdmin):
    search_fields = ('language',)
    list_display = ('language',)

admin.site.register(Language, LanguageAdmin)


class PartnerAdmin(admin.ModelAdmin):
    class CustomModelChoiceField(forms.ModelChoiceField):
        """
        This lets us relabel the users in the dropdown with their recognizable
        wikipedia usernames, rather than their cryptic local IDs. It should be
        used only for the coordinator field.
        """
        def label_from_instance(self, obj):
            return '{editor.wp_username} ({editor.home_wiki})'.format(
                editor=obj.editor)


    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        """
        The coordinator dropdown should limit choices to actual coordinators,
        for admin ease of use.
        """
        if db_field.name == "coordinator":
            return self.CustomModelChoiceField(
                queryset=get_coordinators().user_set.all())
        return super(PartnerAdmin, self).formfield_for_foreignkey(
            db_field, request, **kwargs)


    search_fields = ('company_name',)
    list_display = ('company_name', 'description', 'id', 'get_languages')

admin.site.register(Partner, PartnerAdmin)



class StreamAdmin(admin.ModelAdmin):
    search_fields = ('partner__company_name', 'name',)
    list_display = ('id', 'partner', 'name', 'description', 'get_languages')

admin.site.register(Stream, StreamAdmin)



class ContactAdmin(admin.ModelAdmin):
    search_fields = ('partner__company_name', 'full_name', 'short_name',)
    list_display = ('id', 'title', 'full_name', 'partner', 'email',)

admin.site.register(Contact, ContactAdmin)
