def from_value(value): # value can be: # a Byline # a BylineSearch # None # an existing BylineLookup if not value: return BylineLookup(byline_search=BylineSearch('')) elif isinstance(value, BylineLookup): return value # BylineLookups are treated as immutable, so it's safe to return the initial instance elif isinstance(value, BylineSearch): return BylineLookup( byline_search=value, author_nick_selections=value.author_nick_selections, affiliation_nick_selections=value.affiliation_nick_selections) elif isinstance( value, Byline ): # TODO: can we eliminate Byline here in favour of BylineSearch? byline_search = BylineSearch.from_byline(value) return BylineLookup( byline_search=byline_search, author_nick_selections=byline_search.author_nick_selections, affiliation_nick_selections=byline_search. affiliation_nick_selections) else: raise ValidationError( "Don't know how to handle %s as a byline lookup" % repr(value))
def from_value(value): # value can be: # a Byline # a BylineSearch # None # an existing BylineLookup if not value: return BylineLookup(byline_search=BylineSearch("")) elif isinstance(value, BylineLookup): return value # BylineLookups are treated as immutable, so it's safe to return the initial instance elif isinstance(value, BylineSearch): return BylineLookup( byline_search=value, author_nick_selections=value.author_nick_selections, affiliation_nick_selections=value.affiliation_nick_selections, ) elif isinstance(value, Byline): # TODO: can we eliminate Byline here in favour of BylineSearch? byline_search = BylineSearch.from_byline(value) return BylineLookup( byline_search=byline_search, author_nick_selections=byline_search.author_nick_selections, affiliation_nick_selections=byline_search.affiliation_nick_selections, ) else: raise Exception("Don't know how to handle %s as a byline lookup" % repr(value))
def _set_byline_string(self, byline_string): from demoscene.utils.nick_search import BylineSearch byline_search = BylineSearch(byline_string) if all(byline_search.author_nick_selections) and all(byline_search.affiliation_nick_selections): self.author_nicks = [selection.id for selection in byline_search.author_nick_selections] self.author_affiliation_nicks = [selection.id for selection in byline_search.affiliation_nick_selections] self.unparsed_byline = None self.save() else: self.unparsed_byline = byline_string self.author_nicks = [] self.author_affiliation_nicks = [] self.save()
def value_from_datadict(self, data, files, name): search_term = self.search_widget.value_from_datadict( data, files, name + '_search') if not search_term: return None explicit_lookup_requested = self.lookup_widget.value_from_datadict( data, files, name + '_lookup') byline_search = BylineSearch(search_term) # byline_search now has the appropriate number of author_nick_searches and affiliation_nick_searches # for the passed search term; we can use that to construct the right number of MatchedNickFields author_matched_nick_fields = [ MatchedNickField(nick_search, required=False) for nick_search in byline_search.author_nick_searches ] affiliation_matched_nick_fields = [ MatchedNickField(nick_search, required=False) for nick_search in byline_search.affiliation_nick_searches ] # we can now use those MatchedNickFields to extract the NickSelections from the form submission - # unless explicit_lookup_requested is set, in which case those selections are discarded if explicit_lookup_requested: author_nick_selections = [] affiliation_nick_selections = [] else: author_nick_selections = [ field.widget.value_from_datadict( data, files, name + ('_author_match_%s' % i)) for i, field in enumerate(author_matched_nick_fields) ] affiliation_nick_selections = [ field.widget.value_from_datadict( data, files, name + ('_affiliation_match_%s' % i)) for i, field in enumerate(affiliation_matched_nick_fields) ] return BylineLookup( byline_search=byline_search, author_matched_nick_fields=author_matched_nick_fields, affiliation_matched_nick_fields=affiliation_matched_nick_fields, author_nick_selections=author_nick_selections, affiliation_nick_selections=affiliation_nick_selections, autoaccept=not explicit_lookup_requested)
def byline_match(request): initial_query = request.GET.get('q') autocomplete = request.GET.get('autocomplete', False) # irritating workaround for not being able to pass an "omit this parameter" value to jquery if autocomplete == 'false' or autocomplete == 'null' or autocomplete == '0': autocomplete = False byline_search = BylineSearch(search_term=initial_query, autocomplete=autocomplete) data = { 'query': byline_search.search_term, 'initial_query': initial_query, 'author_matches': byline_search.author_matches_data, 'affiliation_matches': byline_search.affiliation_matches_data, } return HttpResponse(json.dumps(data), content_type="text/javascript")
def byline_search(self): from demoscene.utils.nick_search import BylineSearch if self.unparsed_byline: return BylineSearch(self.unparsed_byline) else: return BylineSearch.from_byline(self.byline())
def test_eq(self): bl = BylineLookup.from_value(BylineSearch('Gasman/Hooy-Program')) self.assertFalse(bl == 'fish')
def test_repr(self): bl = BylineLookup.from_value(BylineSearch('Gasman/Hooy-Program')) self.assertEqual( repr(bl), '<BylineLookup: [NickSelection: 1, Gasman], [NickSelection: 5, Hooy-Program]>' )