Exemplo n.º 1
0
class Form(TendenciBaseModel):
    """
    A user-built form.
    """

    FIRST = 1
    MIDDLE = 2
    LAST = 3

    FIELD_POSITION_CHOICES = (
        (FIRST, _("First")),
        (MIDDLE, _("Middle")),
        (LAST, _("Last")),
    )

    INTRO_DEFAULT_NAME = _("Intro")
    FIELDS_DEFAULT_NAME = _("Fields")
    PRICING_DEFAULT_NAME = _("Pricings")

    title = models.CharField(_("Title"), max_length=100)
    slug = models.SlugField(max_length=100, unique=True)
    intro = models.TextField(_("Intro"), max_length=2000, blank=True)
    response = models.TextField(_("Confirmation Text"),
                                max_length=2000,
                                blank=True)
    email_text = models.TextField(
        _("Email Text to Submitter"),
        default='',
        blank=True,
        help_text=
        _("If Send email is checked, this is the text that will be sent in an email to the person submitting the form."
          ),
        max_length=2000)
    subject_template = models.CharField(
        _("Template for email subject "),
        help_text=_("""Options include [title] for form title, and
                        name of form fields inside brackets [ ]. E.x. [first name] or
                        [email address]"""),
        default="[title] - [first name]  [last name] - [phone]",
        max_length=200,
        blank=True,
        null=True)
    send_email = models.BooleanField(
        _("Send email"),
        default=False,
        help_text=_(
            "If checked, the person submitting the form will be sent an email."
        ))
    email_from = models.EmailField(
        _("Reply-To address"),
        blank=True,
        help_text=_("The address the replies to the email will be sent to"))
    email_copies = models.CharField(
        _("Send copies to"),
        blank=True,
        help_text=_("One or more email addresses, separated by commas"),
        max_length=2000)
    completion_url = models.CharField(
        _("Completion URL"),
        max_length=1000,
        blank=True,
        null=True,
        help_text=
        _("Redirect to this page after form completion. Absolute URLS should begin with http. Relative URLs should begin with a forward slash (/)."
          ))
    template = models.CharField(_('Template'), max_length=50, blank=True)
    group = models.ForeignKey(Group,
                              null=True,
                              default=None,
                              on_delete=models.SET_NULL)

    # payments
    custom_payment = models.BooleanField(
        _("Is Custom Payment"),
        default=False,
        help_text=
        _("If checked, please add pricing options below. Leave the price blank if users can enter their own amount."
          ))
    recurring_payment = models.BooleanField(
        _("Is Recurring Payment"),
        default=False,
        help_text=
        _("If checked, please add pricing options below. Leave the price blank if users can enter their own amount. Please also add an email field as a required field with type 'email'"
          ))
    payment_methods = models.ManyToManyField("payments.PaymentMethod",
                                             blank=True)

    perms = GenericRelation(ObjectPermission,
                            object_id_field="object_id",
                            content_type_field="content_type")

    # positions for displaying the fields
    intro_position = models.IntegerField(_("Intro Position"),
                                         choices=FIELD_POSITION_CHOICES,
                                         default=FIRST)
    fields_position = models.IntegerField(_("Fields Position"),
                                          choices=FIELD_POSITION_CHOICES,
                                          default=MIDDLE)
    pricing_position = models.IntegerField(_("Pricing Position"),
                                           choices=FIELD_POSITION_CHOICES,
                                           default=LAST)

    # variable name of form main sections
    intro_name = models.CharField(_("Intro Name"),
                                  max_length=50,
                                  default=INTRO_DEFAULT_NAME,
                                  blank=True)
    fields_name = models.CharField(_("Fields Name"),
                                   max_length=50,
                                   default=FIELDS_DEFAULT_NAME,
                                   blank=True)
    pricing_name = models.CharField(_("Pricing Name"),
                                    max_length=50,
                                    default=PRICING_DEFAULT_NAME,
                                    blank=True)

    objects = FormManager()

    class Meta:
        verbose_name = _("Form")
        verbose_name_plural = _("Forms")
        permissions = (("view_form", _("Can view form")), )
        app_label = 'forms'

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):
        # If this is the current contact form, update checklist
        if str(self.pk) == get_setting('site', 'global', 'contact_form'):
            checklist_update('update-contact')
        if not self.group:
            self.group_id = get_default_group()
        super(Form, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('form_detail', kwargs={"slug": self.slug})

    def get_payment_type(self):
        if self.recurring_payment and self.custom_payment:
            return _("Custom Recurring Payment")
        if self.recurring_payment:
            return _("Recurring Payment")
        if self.custom_payment:
            return _("Custom Payment")

    @mark_safe
    def admin_link_view(self):
        url = self.get_absolute_url()
        return "<a href='%s'>%s</a>" % (url, ugettext("View on site"))

    admin_link_view.short_description = ""

    @mark_safe
    def admin_link_export(self):
        url = reverse("admin:forms_form_export", args=(self.id, ))
        return "<a href='%s'>%s</a>" % (url, ugettext("Export entries"))

    admin_link_export.short_description = ""

    def has_files(self):
        for field in self.fields.all():
            if field.field_type == 'FileField':
                return True
        return False
Exemplo n.º 2
0
class Form(TendenciBaseModel):
    """
    A user-built form.
    """

    title = models.CharField(_("Title"), max_length=100)
    slug = models.SlugField(max_length=100, unique=True)
    intro = models.TextField(_("Intro"), max_length=2000, blank=True)
    response = models.TextField(_("Confirmation Text"), max_length=2000, blank=True)
    email_text = models.TextField(_("Email Text to Submitter"), default='', blank=True,
        help_text=_("If Send email is checked, this is the text that will be sent in an email to the person submitting the form."), max_length=2000)
    subject_template = models.CharField(_("Template for email subject "),
        help_text=_("""Options include [title] for form title, and
                        name of form fields inside brackets [ ]. E.x. [first name] or
                        [email address]"""),
        default="[title] - [first name]  [last name] - [phone]",
        max_length=200,
        blank=True, null=True)
    send_email = models.BooleanField(_("Send email"), default=False,
        help_text=_("If checked, the person submitting the form will be sent an email."))
    email_from = models.EmailField(_("Reply-To address"), blank=True,
        help_text=_("The address the replies to the email will be sent to"))
    email_copies = models.CharField(_("Send copies to"), blank=True,
        help_text=_("One or more email addresses, separated by commas"),
        max_length=2000)
    completion_url = models.URLField(_("Completion URL"), blank=True, null=True,
        help_text=_("Redirect to this page after form completion."))
    template = models.CharField(_('Template'), max_length=50, blank=True)

    # payments
    custom_payment = models.BooleanField(_("Is Custom Payment"), default=False,
        help_text=_("If checked, please add pricing options below. Leave the price blank if users can enter their own amount."))
    recurring_payment = models.BooleanField(_("Is Recurring Payment"), default=False,
        help_text=_("If checked, please add pricing options below. Leave the price blank if users can enter their own amount."))
    payment_methods = models.ManyToManyField("payments.PaymentMethod", blank=True)

    perms = generic.GenericRelation(ObjectPermission,
        object_id_field="object_id", content_type_field="content_type")

    objects = FormManager()

    class Meta:
        verbose_name = _("Form")
        verbose_name_plural = _("Forms")
        permissions = (("view_form", "Can view form"),)

    def __unicode__(self):
        return self.title

    @models.permalink
    def get_absolute_url(self):
        return ("form_detail", (), {"slug": self.slug})

    def get_payment_type(self):
        if self.recurring_payment and self.custom_payment:
            return "Custom Recurring Payment"
        if self.recurring_payment:
            return "Recurring Payment"
        if self.custom_payment:
            return "Custom Payment"

    def admin_link_view(self):
        url = self.get_absolute_url()
        return "<a href='%s'>%s</a>" % (url, ugettext("View on site"))
    admin_link_view.allow_tags = True
    admin_link_view.short_description = ""

    def admin_link_export(self):
        url = reverse("admin:forms_form_export", args=(self.id,))
        return "<a href='%s'>%s</a>" % (url, ugettext("Export entries"))
    admin_link_export.allow_tags = True
    admin_link_export.short_description = ""

    def has_files(self):
        for entry in self.entries.all():
            for field in entry.fields.all():
                if field.field.field_type == 'FileField':
                    return True
        return False