1- import models
1+ import re
22import django .utils .html
33from django .shortcuts import render_to_response as render
44from django .utils .html import escape
5+ from ietf .idtracker .models import IETFWG , InternetDraft , Rfc
6+ from ietf .ipr .models import IprRfc , IprDraft , IprDetail , SELECT_CHOICES , LICENSE_CHOICES
57from ietf .ipr .view_sections import section_table
68from ietf .ipr .view_new import new
7- from ietf .idtracker . models import IETFWG
9+ from ietf .utils import log
810
911def linebreaks (value ):
1012 if value :
@@ -18,15 +20,15 @@ def default(request):
1820
1921def showlist (request ):
2022 """Display a list of existing disclosures"""
21- return list (request , 'ipr/list.html' )
23+ return list_all (request , 'ipr/list.html' )
2224
2325def updatelist (request ):
2426 """Display a list of existing disclosures, with links to update forms"""
25- return list (request , 'ipr/update_list.html' )
27+ return list_all (request , 'ipr/update_list.html' )
2628
27- def list (request , template ):
29+ def list_all (request , template ):
2830 """Display a list of existing disclosures, using the provided template"""
29- disclosures = models .IprDetail .objects .all ()
31+ disclosures = ietf . ipr . models .IprDetail .objects .all ()
3032 generic_disclosures = disclosures .filter (status__in = [1 ,3 ], generic = 1 )
3133 specific_disclosures = disclosures .filter (status__in = [1 ,3 ], generic = 0 , third_party = 0 )
3234 thirdpty_disclosures = disclosures .filter (status__in = [1 ,3 ], generic = 0 , third_party = 1 )
@@ -43,7 +45,7 @@ def list(request, template):
4345def show (request , ipr_id = None ):
4446 """Show a specific IPR disclosure"""
4547 assert ipr_id != None
46- ipr = models . IprDetail .objects .get (ipr_id = ipr_id )
48+ ipr = IprDetail .objects .get (ipr_id = ipr_id )
4749 section_list = get_section_list (ipr )
4850 contacts = ipr .contact .all ()
4951 for contact in contacts :
@@ -63,11 +65,11 @@ def show(request, ipr_id=None):
6365 ipr .other_notes = linebreaks (escape (ipr .other_notes ))
6466
6567 if ipr .licensing_option :
66- ipr .licensing_option = dict (models . LICENSE_CHOICES )[ipr .licensing_option ]
68+ ipr .licensing_option = dict (LICENSE_CHOICES )[ipr .licensing_option ]
6769 if ipr .selecttype :
68- ipr .selecttype = dict (models . SELECT_CHOICES )[ipr .selecttype ]
70+ ipr .selecttype = dict (SELECT_CHOICES )[ipr .selecttype ]
6971 if ipr .selectowned :
70- ipr .selectowned = dict (models . SELECT_CHOICES )[ipr .selectowned ]
72+ ipr .selectowned = dict (SELECT_CHOICES )[ipr .selectowned ]
7173 return render ("ipr/details.html" , {"ipr" : ipr , "section_list" : section_list })
7274
7375def update (request , ipr_id = None ):
@@ -76,10 +78,127 @@ def update(request, ipr_id=None):
7678 return show (request , ipr_id )
7779
7880
79- def search (request , option = "" , search = "" , submit = "" ):
81+ inverse = {
82+ 'updates' : 'is_updated_by' ,
83+ 'is_updated_by' : 'updates' ,
84+ 'obsoletes' : 'is_obsoleted_by' ,
85+ 'is_obsoleted_by' : 'obsoletes' ,
86+ 'replaces' : 'is_replaced_by' ,
87+ 'is_replaced_by' : 'replaces' ,
88+ 'is_rfc_of' : 'is_draft_of' ,
89+ 'is_draft_of' : 'is_rfc_of' ,
90+ }
91+
92+ display_relation = {
93+ 'updates' : 'that updates' ,
94+ 'is_updated_by' : 'that was updated by' ,
95+ 'obsoletes' : 'that obsoleted' ,
96+ 'is_obsoleted_by' : 'that was obsoleted by' ,
97+ 'replaces' : 'that replaced' ,
98+ 'is_replaced_by' : 'that was replaced by' ,
99+ 'is_rfc_of' : 'which came from' ,
100+ 'is_draft_of' : 'that was published as' ,
101+ }
102+
103+ def set_related (obj , rel , target ):
104+ print obj , rel , target
105+ # remember only the first relationship we find.
106+ if not hasattr (obj , "related" ):
107+ obj .related = target
108+ obj .relation = display_relation [rel ]
109+
110+ def set_relation (first , rel , second ):
111+ set_related (first , rel , second )
112+ set_related (second , inverse [rel ], first )
113+
114+ def related_docs (doc , found = []):
115+ print "\n related_docs(%s, %s)" % (doc , found )
116+ found .append (doc )
117+ if isinstance (doc , Rfc ):
118+ try :
119+ item = InternetDraft .objects .get (rfc_number = doc .rfc_number )
120+ if not item in found :
121+ set_relation (doc , 'is_rfc_of' , item )
122+ found = related_docs (item , found )
123+ except InternetDraft .DoesNotExist :
124+ pass
125+ for entry in doc .updated_or_obsoleted_by .all ():
126+ item = entry .rfc
127+ if not item in found :
128+ action = inverse [entry .action .lower ()]
129+ set_relation (doc , action , item )
130+ found = related_docs (item , found )
131+ for entry in doc .updates_or_obsoletes .all ():
132+ item = entry .rfc_acted_on
133+ if not item in found :
134+ action = entry .action .lower ()
135+ set_relation (doc , action , item )
136+ found = related_docs (item , found )
137+ if isinstance (doc , InternetDraft ):
138+ if doc .replaced_by_id :
139+ item = doc .replaced_by_id
140+ if not item in found :
141+ set_relation (doc , 'replaced_by' , item )
142+ found = related_docs (item , found )
143+ for item in doc .replaces_set .all ():
144+ if not item in found :
145+ set_relation (doc , 'replaces' , item )
146+ found = related_docs (item , found )
147+ if doc .rfc_number :
148+ item = Rfc .objects .get (rfc_number = doc .rfc_number )
149+ if not item in found :
150+ set_relation (doc , 'is_draft_of' , item )
151+ found = related_docs (item , found )
152+ return found
153+
154+ def search (request , type = "" , q = "" , id = "" ):
80155 wgs = IETFWG .objects .filter (group_type__group_type_id = 1 ).exclude (group_acronym__acronym = '2000' ).select_related ().order_by ('acronym.acronym' )
156+ args = request .REQUEST .items ()
157+ if args :
158+ for key , value in args :
159+ if key == "option" :
160+ type = value
161+ if re .match (".*search" , key ):
162+ q = value
163+ if re .match (".*id" , key ):
164+ id = value
165+ if type and q or id :
166+ log ("Got query: type=%s, q=%s, id=%s" % (type , q , id ))
167+ if type == "document_search" :
168+ log ("Got document_search" )
169+ if q :
170+ start = InternetDraft .objects .filter (filename__contains = q )
171+ log ("Found %s drafts containing %s" % (start .count (), q ))
172+ if id :
173+ start = InternetDraft .objects .filter (id_document_tag = id )
174+ log ("Found %s drafts with id %s" % (start .count (), id ))
175+ if start .count () == 1 :
176+ first = start [0 ]
177+ # get all related drafts, then search for IPRs on all
178+ docs = related_docs (first , [])
179+ iprs = []
180+ for doc in docs :
181+ if isinstance (doc , InternetDraft ):
182+ disclosures = [ item .ipr for item in IprDraft .objects .filter (document = doc ) ]
183+ elif isinstance (doc , Rfc ):
184+ disclosures = [ item .ipr for item in IprRfc .objects .filter (document = doc ) ]
185+ else :
186+ raise ValueError ("Neither draft nor rfc: %s" % doc )
187+ if disclosures :
188+ doc .iprs = disclosures
189+ iprs += disclosures
190+ iprs = list (set (iprs ))
191+ return render ("ipr/search_result.html" , {"first" : first , "iprs" : iprs , "docs" : docs })
192+ elif start .count ():
193+ return render ("ipr/search_list.html" , {"docs" : start })
194+ return django .http .HttpResponseRedirect (request .path )
81195 return render ("ipr/search.html" , {"wgs" : wgs })
82196
197+ def form (request ):
198+ wgs = IETFWG .objects .filter (group_type__group_type_id = 1 ).exclude (group_acronym__acronym = '2000' ).select_related ().order_by ('acronym.acronym' )
199+ log ("Search form" )
200+ return render ("ipr/search.html" , {"wgs" : wgs })
201+
83202
84203
85204# ---- Helper functions ------------------------------------------------------
0 commit comments