示例#1
0
def projects_for_object(context, obj):
    ct = ContentType.objects.get_for_model(obj)
    projects = [p.project for p in
        ProjectRelatedObject.objects.filter(content_type=ct, object_id=obj.id)]
    project_widget = Autocomplete(Project, '/project/search_by_name')
    project_widget = "%s \n %s" % (str(project_widget.media),
                                     project_widget.render('project'))
    return dict(projects=projects, project_widget=project_widget,
                contenttype=ct.id, object_id=obj.id)
def _get_widgets_dict(obj):
    tag_widget = TaggitWidget(autocomplete_url="/%s/search_tags/" % obj)
    tag_widget = "%s \n %s" % (str(tag_widget.media),
                               tag_widget.render('tags'))
    # tag_widget = "<input id='id_tags' type='text'/>"

    community_widget = Autocomplete(Community, '/community/search_by_name')
    community_widget = "%s \n %s" % (str(community_widget.media),
                                     community_widget.render('community'))

    ##### NEED CATEGORIES FILTER #####
    # FIXME: this code should be somewhere else.
    need_categories_widget = "<input id='need_categories' type='hidden'/>\n"
    for nc in NeedCategory.objects.all().order_by('name'):
        need_categories_widget += """
    <img src='/static/{0}' title='{3}' class='nc_filter' ncid='{2}'/>
    <img src='/static/{1}' title='{3}' class='nc_filter hidden' ncid='{2}'/>
        """.format(nc.image_off, nc.image, nc.id, nc.name)
    need_categories_widget += """
        <script type="text/javascript">
            var nc_filter_arr = [];

            $("img.nc_filter").on("click", function (evt) {
                var id = $(evt.target).attr('ncid');

                var pos = nc_filter_arr.indexOf(id);
                if (pos != -1)
                    nc_filter_arr.splice(pos, 1); // removes the element
                else
                    nc_filter_arr.push(id); // appends element
                $("#need_categories").val(nc_filter_arr.join()).change;
                $(".nc_filter[ncid='"+id+"']").toggle();
            });
        </script>
    """
    ##################################

    target_audience_widget = TaggitWidget(
        autocomplete_url="/need/target_audience_search/"
    )
    target_audience_widget = "%s \n %s" % (
        str(target_audience_widget.media),
        target_audience_widget.render('target_audiences')
    )

    # filters
    return {
        'tags': tag_widget,
        'community': community_widget,
        'need_categories': need_categories_widget,
        'target_audiences': target_audience_widget,
    }
示例#3
0
def _get_widgets_dict(obj):
    tag_widget = TaggitWidget(autocomplete_url="/%s/search_tags/" % obj)
    tag_widget = "%s \n %s" % (str(
        tag_widget.media), tag_widget.render('tags'))
    # tag_widget = "<input id='id_tags' type='text'/>"

    community_widget = Autocomplete(Community, '/community/search_by_name')
    community_widget = "%s \n %s" % (str(
        community_widget.media), community_widget.render('community'))

    ##### NEED CATEGORIES FILTER #####
    # FIXME: this code should be somewhere else.
    need_categories_widget = "<input id='need_categories' type='hidden'/>\n"
    for nc in NeedCategory.objects.all().order_by('name'):
        need_categories_widget += """
            <img src='/static/{0}' title='{3}' class='nc_filter' ncid='{2}'/>
            <img src='/static/{1}' title='{3}' class='nc_filter hidden' ncid='{2}'/>
        """.format(nc.image_off, nc.image, nc.id, nc.name)
    need_categories_widget += """
        <script type="text/javascript">
            var nc_filter_arr = [];

            $("img.nc_filter").on("click", function (evt) {
                var id = $(evt.target).attr('ncid');

                var pos = nc_filter_arr.indexOf(id);
                if (pos != -1)
                    nc_filter_arr.splice(pos, 1); // removes the element
                else
                    nc_filter_arr.push(id); // appends element
                $("#need_categories").val(nc_filter_arr.join()).change;
                $(".nc_filter[ncid='"+id+"']").toggle();
            });
        </script>
    """
    ##################################

    target_audience_widget = TaggitWidget(
        autocomplete_url="/need/target_audience_search/")
    target_audience_widget = "%s \n %s" % (
        str(target_audience_widget.media),
        target_audience_widget.render('target_audiences'))
    # target_audience_widget = target_audience_widget.render('target_audiences')
    # target_audience_widget = "<input id='id_target_audiences' type='text'/>"

    # filters
    return {
        'tags': tag_widget,
        'community': community_widget,
        'need_categories': need_categories_widget,
        'target_audiences': target_audience_widget,
    }
示例#4
0
class InvestmentForm(AjaxModelForm):
    class Meta:
        model = Investment
        fields = ('title', 'description', 'investor_type',
                  'anonymous_investor', 'investor_organization',
                  'investor_person', 'over_period', 'date', 'end_date',
                  'currency', 'value', 'tags')

    _field_labels = {
        'title': _('Title'),
        'description': _('Description'),
        'investor_type': _('Investor type'),
        'anonymous_investor': _('Anonymous investor'),
        'investor_organization': _('Investor organization'),
        'investor_person': _('Investor person'),
        'over_period': _('Over period'),
        'date': _('Date'),
        'end_date': _('End date'),
        'currency': _('Currency'),
        'value': _('Value'),
        'tags': _('Tags'),
    }

    title = forms.CharField()
    description = forms.CharField(widget=MarkItUpWidget())

    investor_type = forms.ChoiceField(choices=Investor.TYPE_CHOICES,
                                      initial='ORG',
                                      widget=forms.RadioSelect,
                                      required=True)

    anonymous_investor = forms.BooleanField(widget=ConditionalField(
        hide_on_active="#investment_form .investor_fields"),
                                            required=False)

    # FIXME: the urls below should not be hardcoded. They should be calculated
    # with reverse_lazy function, which is not implemented in Django 1.3 yet.

    investor_organization = forms.ModelChoiceField(
        queryset=Organization.objects.all(),
        widget=Autocomplete(Organization, "/organization/search_by_name"),
        required=False,
        label="Investor")
    investor_person = forms.CharField(label="Investor", required=False)

    # this field is needed to avoid creating duplicated instances
    investor = forms.ModelChoiceField(queryset=Investor.objects.all(),
                                      required=False)

    date = forms.DateField(widget=Datepicker())
    end_date = forms.DateField(widget=Datepicker(), required=False)
    over_period = forms.BooleanField(
        widget=ConditionalField(show_on_active="#div_id_end_date"),
        required=False)

    tags = forms.Field(
        widget=TaggitWidget(autocomplete_url="/investment/tag_search"),
        required=False)

    # TODO: value just validates if currency is set

    def __init__(self, *a, **kw):
        # Crispy forms configuration
        self.helper = MooHelper(form_id="investment_form")
        self.helper.layout = Layout(
            "id",
            "title",
            "description",
            "investor_type",
            "anonymous_investor",
            Div("investor_organization",
                "investor_person",
                css_class="investor_fields"),
            "over_period",
            Row(
                "date",
                "end_date",
            ),
            Row(
                "currency",
                "value",
            ),
            "tags",
        )

        return super(InvestmentForm, self).__init__(*a, **kw)

    def clean_end_date(self):
        data = self.cleaned_data['end_date']
        if not self.cleaned_data['over_period']:
            data = None
        return data

    def clean(self):
        cleaned_data = super(InvestmentForm, self).clean()

        # investor validation
        current_investor = self.cleaned_data.pop("investor")
        anonymous_investor = self.cleaned_data.pop("anonymous_investor")
        investor_type = self.cleaned_data.pop("investor_type")
        investor_organization = self.cleaned_data.pop("investor_organization")
        investor_person = self.cleaned_data.pop("investor_person")
        if not anonymous_investor:
            self.validation(
                'investor_organization', _("Invalid investor."),
                investor_type == "ORG" and not investor_organization)
            self.validation('investor_person', _("Invalid investor."),
                            investor_type == "PER" and not investor_person)
        # investor coercing
        if anonymous_investor:
            investor = ""
        elif investor_type == 'ORG':
            investor = investor_organization
        elif investor_type == 'PER':
            investor = investor_person

        if investor != None:
            investor, created = Investor.get_or_create_for(
                investor, current=current_investor)
            self.cleaned_data['investor'] = investor
        else:
            created = False

        if created:
            investor.save()

        return cleaned_data

    @notify_on_update
    def save(self, *args, **kwargs):
        investment = super(InvestmentForm, self).save(commit=False)
        investor = self.cleaned_data['investor']
        investor.save()
        investment.investor = investor

        # why need to explicit save tags here?
        tags = self.cleaned_data['tags']
        investment.tags.set(*tags)

        investment.save()
        return investment
示例#5
0
def filtered_query_to_project(context, type, params, filtered=False):
    project_widget = Autocomplete(Project, '/project/search_by_name')
    project_widget = "%s \n %s" % (str(project_widget.media),
                                     project_widget.render('project'))
    return dict(project_widget=project_widget, type=type, params=json.dumps(params), filtered=filtered)