from django import forms
from django.contrib import admin, messages
from django.db.models import ForeignKey

from cms.admin.placeholderadmin import PlaceholderAdmin

from widgetry import fk_lookup
from widgetry.tabs.placeholderadmin import ModelAdminWithTabsAndCMSPlaceholder

from arkestra_utilities.widgets.wym_editor import WYMEditor
from arkestra_utilities import admin_tabs_extension
from arkestra_utilities.admin_mixins import GenericModelAdmin, GenericModelForm, HostedByFilter, fieldsets

from links.admin import ExternalLinkForm, ObjectLinkInline

from models import Vacancy, Studentship

class VacancyStudentshipForm(GenericModelForm):
    # a shared form for vacancies & studentships
    pass

class VacancyStudentshipAdmin(GenericModelAdmin, ModelAdminWithTabsAndCMSPlaceholder):
    # inlines = (ObjectLinkInline,)
    exclude = ('description',)
    search_fields = ['short_title','title','summary', 'slug']
    list_display = ('short_title', 'hosted_by', 'date',)
    list_display = ('short_title', 'date', 'hosted_by',)
    list_filter = ('date', HostedByFilter)
    related_search_fields = [
        'hosted_by',
        'please_contact',
        'external_url',
        ]
    filter_horizontal = (
        'please_contact',
        'publish_to',
        )
    prepopulated_fields = {
        'slug': ('title',)
            }

    def _media(self):
        return super(ModelAdminWithTabsAndCMSPlaceholder, self).media
    media = property(_media)


class VacancyForm(VacancyStudentshipForm):
    class Meta(VacancyStudentshipForm.Meta):
        model = Vacancy

class VacancyAdmin(VacancyStudentshipAdmin):
    # def __init__(self):
    #     super(VacancyAdmin, self).__init__(self)
    #     search_fields.append('job_number')

    form = VacancyForm
    fieldset_vacancy = ('', {'fields': ('salary', 'job_number')})

    tabs = (
            ('Basic', {'fieldsets': (fieldsets["basic"], fieldsets["host"], fieldset_vacancy, fieldsets["image"], fieldsets["publishing_control"],)}),
            ('Date & significance', {'fieldsets': (fieldsets["date"], fieldsets["importance"])}),
            ('Body', {'fieldsets': (fieldsets["body"],)}),
            ('Where to Publish', {'fieldsets': (fieldsets["where_to_publish"],),}),
            ('Please contact', {'fieldsets': (fieldsets["people"],)}),
            ('Links', {'inlines': (ObjectLinkInline,),}),
            ('Advanced Options', {'fieldsets': (fieldsets["url"], fieldsets["slug"],)}),
        )


class StudentshipForm(VacancyStudentshipForm):
    class Meta(VacancyStudentshipForm.Meta):
        model = Studentship


# class StudentshipAdmin(admin_tabs_extension.ModelAdminWithTabs):
class StudentshipAdmin(VacancyStudentshipAdmin):
    form = StudentshipForm
    filter_horizontal = (
        'publish_to',
        'supervisors',
        'please_contact',
    )

    fieldset_supervision = ('', {'fields': ('supervisors',)})
    tabs = (
            ('Basic', {'fieldsets': (fieldsets["basic"], fieldsets["host"], fieldsets["image"], fieldsets["publishing_control"],)}),
            ('Date & significance', {'fieldsets': (fieldsets["date"], fieldsets["importance"])}),
            ('Body', {'fieldsets': (fieldsets["body"],)}),
            ('Where to Publish', {'fieldsets': (fieldsets["where_to_publish"],),}),
            ('Supervisors', {'fieldsets': (fieldset_supervision,)}),
            ('Please contact', {'fieldsets': (fieldsets["people"],)}),
            ('Links', {'inlines': (ObjectLinkInline,),}),
            ('Advanced Options', {'fieldsets': (fieldsets["url"], fieldsets["slug"],)}),
        )

    # autocomplete fields
    related_search_fields = [
        'hosted_by',
        'please_contact',
        ]

admin.site.register(Vacancy,VacancyAdmin)
admin.site.register(Studentship,StudentshipAdmin)

@@ -1,29 +1,4 @@
-from django import forms%0A
 from
@@ -29,339 +29,89 @@
 dmin
-, messages%0Afrom django.db.models import ForeignKey%0A%0Afrom cms.admin.placeholderadmin import PlaceholderAdmin%0A%0Afrom widgetry import fk_lookup%0Afrom widgetry.tabs.placeholderadmin import ModelAdminWithTabsAndCMSPlaceholder%0A%0Afrom arkestra_utilities.widgets.wym_editor import WYMEditor%0Afrom arkestra_utilities import admin_tabs_extension
+%0A%0Afrom widgetry.tabs.placeholderadmin import ModelAdminWithTabsAndCMSPlaceholder%0A
 %0Afro
@@ -242,26 +242,8 @@
 port
- ExternalLinkForm,
  Obj
@@ -405,16 +405,17 @@
   pass%0A%0A
+%0A
 class Va
@@ -1280,16 +1280,17 @@
 acancy%0A%0A
+%0A
 class Va
@@ -1552,36 +1552,32 @@
 abs = (%0A        
-    
 ('Basic', %7B'fiel
@@ -1699,36 +1699,32 @@
 %22%5D,)%7D),%0A        
-    
 ('Date & signifi
@@ -1793,36 +1793,32 @@
 e%22%5D)%7D),%0A        
-   
- 
 ('Body', %7B'field
@@ -1840,36 +1840,32 @@
 ts%5B%22body%22%5D,)%7D),%0A
-    
         ('Where 
@@ -1928,36 +1928,32 @@
 %5D,),%7D),%0A        
- 
-   
 ('Please contact
@@ -1995,36 +1995,32 @@
 %22%5D,)%7D),%0A        
-  
-  
 ('Links', %7B'inli
@@ -2041,36 +2041,32 @@
 inkInline,),%7D),%0A
-    
         ('Advanc
@@ -2594,28 +2594,24 @@
 = (%0A        
-    
 ('Basic', %7B'
@@ -2711,36 +2711,32 @@
 _control%22%5D,)%7D),%0A
-    
         ('Date &
@@ -2817,28 +2817,24 @@
 %7D),%0A        
-    
 ('Body', %7B'f
@@ -2860,36 +2860,32 @@
 ts%5B%22body%22%5D,)%7D),%0A
-    
         ('Where 
@@ -2956,20 +2956,16 @@
         
-    
 ('Superv
@@ -3017,28 +3017,24 @@
 %7D),%0A        
-  
-  
 ('Please con
@@ -3084,28 +3084,24 @@
 %7D),%0A        
-   
- 
 ('Links', %7B'
@@ -3130,28 +3130,24 @@
 nline,),%7D),%0A
-    
         ('Ad
@@ -3234,121 +3234,8 @@
  )%0A%0A
-    # autocomplete fields%0A    related_search_fields = %5B%0A        'hosted_by',%0A        'please_contact',%0A        %5D%0A
 %0Aadm
@@ -3259,16 +3259,17 @@
 Vacancy,
+ 
 VacancyA
@@ -3306,16 +3306,17 @@
 entship,
+ 
 Students

fix broken studentship external URL autocomplete