예제 #1
0
class PartyWidget(forms.Widget):
	def __init__(self, attrs=None):
		self.search_widget = forms.TextInput(attrs={'class': 'party_field_search'})
		self.lookup_widget = SubmitButtonInput(button_text='Find party', attrs={'class': 'party_field_lookup'})
		self.party_id_widget = forms.HiddenInput(attrs={'class': 'party_field_party_id'})
		super(PartyWidget, self).__init__(attrs=attrs)

	def render(self, name, value, attrs=None):
		party_lookup = PartyLookup.from_value(value)

		output = [
			self.search_widget.render(name + '_search', party_lookup.search_term, attrs=attrs),
			self.lookup_widget.render(name + '_lookup', None, attrs=attrs),
			self.party_id_widget.render(name + '_party_id', party_lookup.party_id, attrs=attrs),
			'<div class="help_text">(if the party doesn\'t exist yet, <a href="/parties/new/" target="_blank">create it first</a>!)</div>'
		]

		return mark_safe(u'<div class="party_field">' + u''.join(output) + u'</div>')

	def value_from_datadict(self, data, files, name):
		search_term = self.search_widget.value_from_datadict(data, files, name + '_search')
		redisplay = self.lookup_widget.value_from_datadict(data, files, name + '_lookup')
		party_id = self.party_id_widget.value_from_datadict(data, files, name + '_party_id')

		party_lookup = PartyLookup(search_term=search_term, redisplay=redisplay, party_id=party_id)

		if party_lookup.is_empty:
			return None
		else:
			return party_lookup

	def _has_changed(self, initial, data):
		initial = PartyLookup.from_value(initial)
		data = PartyLookup.from_value(data)
		return data != initial
예제 #2
0
class PartyWidget(forms.Widget):
    def __init__(self, attrs=None):
        self.search_widget = forms.TextInput(attrs={'class': 'party_field_search'})
        self.lookup_widget = SubmitButtonInput(button_text='Find party', attrs={'class': 'party_field_lookup'})
        self.party_id_widget = forms.HiddenInput(attrs={'class': 'party_field_party_id'})
        super(PartyWidget, self).__init__(attrs=attrs)

    def render(self, name, value, attrs=None):
        party_lookup = PartyLookup.from_value(value)

        output = [
            self.search_widget.render(name + '_search', party_lookup.search_term, attrs=attrs),
            self.lookup_widget.render(name + '_lookup', None, attrs=attrs),
            self.party_id_widget.render(name + '_party_id', party_lookup.party_id, attrs=attrs),
            '<div class="help_text">(if the party doesn\'t exist yet, <a href="/parties/new/" target="_blank">create it first</a>!)</div>'
        ]

        return mark_safe(u'<div class="party_field">' + u''.join(output) + u'</div>')

    def value_from_datadict(self, data, files, name):
        search_term = self.search_widget.value_from_datadict(data, files, name + '_search')
        redisplay = self.lookup_widget.value_from_datadict(data, files, name + '_lookup')
        party_id = self.party_id_widget.value_from_datadict(data, files, name + '_party_id')

        party_lookup = PartyLookup(search_term=search_term, redisplay=redisplay, party_id=party_id)

        if party_lookup.is_empty:
            return None
        else:
            return party_lookup
예제 #3
0
class BylineWidget(forms.Widget):
    def __init__(self, attrs=None):
        self.search_widget = forms.TextInput(attrs=attrs)
        self.lookup_widget = SubmitButtonInput(button_text='Find names')
        super().__init__(attrs=attrs)

    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 id_for_label(self, id_):
        if id_:
            id_ += '_search'
        return id_
    id_for_label = classmethod(id_for_label)

    def render(self, name, value, attrs=None, renderer=None):
        byline_lookup = BylineLookup.from_value(value)

        search_attrs = self.build_attrs(attrs)
        search_attrs['id'] += '_search'
        lookup_attrs = self.build_attrs(attrs)
        lookup_attrs['id'] += '_lookup'

        search_html_output = [
            self.search_widget.render(
                name + '_search', byline_lookup.search_term, attrs=search_attrs, renderer=renderer
            ),
            self.lookup_widget.render(
                name + '_lookup', None, attrs=lookup_attrs, renderer=renderer
            ),
        ]

        if byline_lookup.search_term:
            matched_nick_html = byline_lookup.render_match_fields(name, renderer=renderer)
        else:
            matched_nick_html = ''

        output = [
            u'<div class="byline_search">' + u''.join(search_html_output) + u'</div>',
            u'<div class="byline_match_container">' + matched_nick_html + u'</div>'
        ]

        root_classname = u'byline_field'
        return mark_safe(u'<div class="' + root_classname + u'">' + u''.join(output) + u'</div>')
예제 #4
0
class NickWidget(forms.Widget):
    def __init__(self,
                 attrs=None,
                 sceners_only=False,
                 groups_only=False,
                 prefer_members_of=None):
        self.sceners_only = sceners_only
        self.groups_only = groups_only
        self.prefer_members_of = prefer_members_of
        self.search_widget = forms.TextInput(attrs=attrs)
        self.lookup_widget = SubmitButtonInput(button_text='Find name')
        super(NickWidget, self).__init__(attrs=attrs)

    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
        search_term = search_term.strip()
        if not search_term:
            return None

        explicit_lookup_requested = self.lookup_widget.value_from_datadict(
            data, files, name + '_lookup')

        nick_lookup = NickLookup(search_term=search_term,
                                 autoaccept=not explicit_lookup_requested,
                                 sceners_only=self.sceners_only,
                                 groups_only=self.groups_only,
                                 prefer_members_of=self.prefer_members_of)

        if not explicit_lookup_requested:
            nick_lookup.nick_selection = nick_lookup.matched_nick_field.widget.value_from_datadict(
                data, files, name + '_match')

        return nick_lookup

    def id_for_label(self, id_):
        if id_:
            id_ += '_search'
        return id_

    id_for_label = classmethod(id_for_label)

    def render(self, name, value, attrs=None, renderer=None):
        nick_lookup = NickLookup.from_value(
            value,
            sceners_only=self.sceners_only,
            groups_only=self.groups_only,
            prefer_members_of=self.prefer_members_of)

        search_html_output = [
            self.search_widget.render(name + '_search',
                                      nick_lookup.search_term,
                                      attrs=attrs,
                                      renderer=renderer),
            '<input type="submit" style="display: none;">',  # extra submit button so that whenever browsers insist on pretending a button was pressed when actually the user submitted the form with the enter key, they'll choose this button rather than the 'lookup' one
            self.lookup_widget.render(name + '_lookup',
                                      None,
                                      attrs=attrs,
                                      renderer=renderer),
        ]

        if nick_lookup.search_term:
            matched_nick_html = nick_lookup.matched_nick_field.widget.render(
                name + '_match',
                nick_lookup.nick_selection,
                attrs=attrs,
                renderer=renderer)
        else:
            matched_nick_html = ''

        if self.sceners_only:
            root_classname = u'nick_field sceners_only'
        elif self.groups_only:
            root_classname = u'nick_field groups_only'
        else:
            root_classname = u'nick_field'

        if self.prefer_members_of:
            root_attrs = u' data-group_ids="%s"' % u','.join(
                [str(group.id) for group in self.prefer_members_of])
        else:
            root_attrs = u''

        output = [
            u'<div class="nick_search">' + u''.join(search_html_output) +
            u'</div>', u'<div class="nick_match_container">' +
            matched_nick_html + u'</div>'
        ]
        return mark_safe(u'<div class="' + root_classname + u'"' + root_attrs +
                         u'>' + u''.join(output) + u'</div>')
예제 #5
0
class BylineWidget(forms.Widget):
    def __init__(self, attrs=None):
        self.search_widget = forms.TextInput(attrs=attrs)
        self.lookup_widget = SubmitButtonInput(button_text="Find names")
        super(BylineWidget, self).__init__(attrs=attrs)

    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, None) for nick_search in byline_search.author_nick_searches
        ]
        affiliation_matched_nick_fields = [
            MatchedNickField(nick_search, None) 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 id_for_label(self, id_):
        if id_:
            id_ += "_search"
        return id_

    id_for_label = classmethod(id_for_label)

    def render(self, name, value, attrs=None):
        byline_lookup = BylineLookup.from_value(value)

        search_attrs = self.build_attrs(attrs)
        search_attrs["id"] += "_search"
        lookup_attrs = self.build_attrs(attrs)
        lookup_attrs["id"] += "_lookup"

        search_html_output = [
            self.search_widget.render(name + "_search", byline_lookup.search_term, attrs=search_attrs),
            self.lookup_widget.render(name + "_lookup", None, attrs=lookup_attrs),
        ]

        if byline_lookup.search_term:
            matched_nick_html = byline_lookup.render_match_fields(name)
        else:
            matched_nick_html = ""

        output = [
            u'<div class="byline_search">' + u"".join(search_html_output) + u"</div>",
            u'<div class="byline_match_container">' + matched_nick_html + u"</div>",
        ]

        root_classname = u"byline_field"
        return mark_safe(u'<div class="' + root_classname + u'">' + u"".join(output) + u"</div>")

    def _has_changed(self, initial, data):
        initial = BylineLookup.from_value(initial)
        data = BylineLookup.from_value(data)
        return initial != data
예제 #6
0
파일: nick_field.py 프로젝트: Bombe/demozoo
class NickWidget(forms.Widget):
	def __init__(self, attrs=None, sceners_only=False, groups_only=False):
		self.sceners_only = sceners_only
		self.groups_only = groups_only
		self.search_widget = forms.TextInput(attrs=attrs)
		self.lookup_widget = SubmitButtonInput(button_text='Find name')
		super(NickWidget, self).__init__(attrs=attrs)

	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')

		nick_lookup = NickLookup(
			search_term=search_term,
			autoaccept=not explicit_lookup_requested,
			sceners_only=self.sceners_only, groups_only=self.groups_only)

		if not explicit_lookup_requested:
			nick_lookup.nick_selection = nick_lookup.matched_nick_field.widget.value_from_datadict(data, files, name + '_match')

		return nick_lookup

	def id_for_label(self, id_):
		if id_:
			id_ += '_search'
		return id_
	id_for_label = classmethod(id_for_label)

	def render(self, name, value, attrs=None):
		nick_lookup = NickLookup.from_value(value, sceners_only=self.sceners_only, groups_only=self.groups_only)

		search_html_output = [
			self.search_widget.render(name + '_search', nick_lookup.search_term, attrs=attrs),
			'<input type="submit" style="display: none;">', # extra submit button so that whenever browsers insist on pretending a button was pressed when actually the user submitted the form with the enter key, they'll choose this button rather than the 'lookup' one
			self.lookup_widget.render(name + '_lookup', None, attrs=attrs)
		]

		if nick_lookup.search_term:
			matched_nick_html = nick_lookup.matched_nick_field.widget.render(name + '_match', nick_lookup.nick_selection, attrs=attrs)
		else:
			matched_nick_html = ''

		if self.sceners_only:
			root_classname = u'nick_field sceners_only'
		elif self.groups_only:
			root_classname = u'nick_field groups_only'
		else:
			root_classname = u'nick_field'

		output = [
			u'<div class="nick_search">' + u''.join(search_html_output) + u'</div>',
			u'<div class="nick_match_container">' + matched_nick_html + u'</div>'
		]
		return mark_safe(u'<div class="' + root_classname + u'">' + u''.join(output) + u'</div>')

	def _has_changed(self, initial, data):
		initial = NickLookup.from_value(initial)
		data = NickLookup.from_value(data)
		return data != initial