]> git.ipfire.org Git - ipfire.org.git/commitdiff
donation: Allow to pass pre-selected arguments
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 23 Jul 2018 17:34:26 +0000 (18:34 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 23 Jul 2018 17:34:26 +0000 (18:34 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/templates/donate.html
src/web/handlers.py
src/web/handlers_base.py

index 616027c79ab51a03a74aed1099fc45c4e3bea7e7..d54bffbde5178181cb52fd94c2d921289a67654a 100644 (file)
                                                                                        <h6>Choose a currency</h6>
 
                                                                                        <div class="form-check form-check-inline">
-                                                                                               <input class="form-check-input" type="radio" name="currency" id="EUR" value="EUR" checked>
+                                                                                               <input class="form-check-input" type="radio" name="currency" id="EUR" value="EUR"
+                                                                                                       {% if currency == "EUR" %}checked{% end %}>
                                                                                                <label class="form-check-label" for="EUR">{{ _("Euro") }}</label>
                                                                                        </div>
 
                                                                                        <div class="form-check form-check-inline">
-                                                                                               <input class="form-check-input" type="radio" name="currency" id="USD" value="USD">
+                                                                                               <input class="form-check-input" type="radio" name="currency" id="USD" value="USD"
+                                                                                                       {% if currency == "USD" %}checked{% end %}>
                                                                                                <label class="form-check-label" for="USD">{{ _("US Dollar") }}</label>
                                                                                        </div>
                                                                                </div>
                                                                                        <h6>Frequency</h6>
 
                                                                                        <div class="form-check form-check-inline">
-                                                                                               <input class="form-check-input" type="radio" name="frequency" id="frequency-single" value="single" checked>
-                                                                                               <label class="form-check-label" for="frequency-single">{{ _("One Time") }}</label>
+                                                                                               <input class="form-check-input" type="radio" name="frequency" id="frequency-one-time"
+                                                                                                       value="one-time" {% if frequency == "one-time" %}checked{% end %}>
+                                                                                               <label class="form-check-label" for="frequency-one-time">{{ _("One Time") }}</label>
                                                                                        </div>
 
                                                                                        <div class="form-check form-check-inline">
-                                                                                               <input class="form-check-input" type="radio" name="frequency" id="frequency-monthly" value="monthly">
+                                                                                               <input class="form-check-input" type="radio" name="frequency" id="frequency-monthly"
+                                                                                                       value="monthly" {% if frequency == "monthly" %}checked{% end %}>
                                                                                                <label class="form-check-label" for="frequency-monthly">{{ _("Monthly") }}</label>
                                                                                        </div>
                                                                                </div>
                                                                        <div class="my-5 p-5 bg-light">
                                                                                <h6>{{ _("Choose an amount") }}</h6>
 
-                                                                               <input type="hidden" name="amount" required>
+                                                                               <input type="hidden" name="amount" {% if amount %}value="{{ amount }}"{% end %} required>
 
                                                                                <div class="btn-group-toggle flex-wrap mb-3" data-toggle="buttons">
-                                                                                       {% for amount in amounts %}
+                                                                                       {% for a in amounts %}
                                                                                                <label class="btn btn-secondary btn-lg mb-2">
-                                                                                                       <input type="radio" name="amount-selector" value="{{ amount }}"
-                                                                                                               autocomplete="off"> <span class="USD">$</span>{{ amount }} <span class="EUR">€</span>
+                                                                                                       <input type="radio" name="amount-selector" value="{{ a }}" {% if amount == a %}checked{% end %}
+                                                                                                               autocomplete="off"> <span class="USD">$</span>{{ a }} <span class="EUR">€</span>
                                                                                                </label>
                                                                                        {% end %}
                                                                                </div>
@@ -67,8 +71,8 @@
                                                                                        </label>
 
                                                                                        <div class="col-sm-5">
-                                                                                               <input type="number" class="form-control form-control-lg"
-                                                                                                       name="amount-input" min="1">
+                                                                                               <input type="number" class="form-control form-control-lg" name="amount-input" min="1"
+                                                                                                       {% if not amount in amounts %}value="{{ amount }}"{% end %}>
                                                                                        </div>
                                                                                </div>
 
                $(document).ready(function() {
                        var amount = $("input[name='amount']");
 
-                       $(".USD").hide();
+                       // Adjust form to default currency
+                       var currency = $("input[name='currency']:checked").val();
+                       if (currency == "EUR") {
+                               $(".USD").hide();
+                       } else if (currency == "USD") {
+                               $(".EUR").hide();
+                       }
 
                        $("input[name='currency']").on("change", function() {
                                var currency = $(this).val();
                        $("#donate").click(function (event) {
                                var frequency = $("input[name='frequency']").val();
 
-                               if (frequency == "single") {
+                               if (frequency == "one-time") {
                                        event.preventDefault();
 
                                        $("#modal-upsell").modal("show");
index dfa05369cc4051307aa1d5dc212772c085b31d07..b7805ba1345a800ea8cca57beeee4e0c0c4ac704 100644 (file)
@@ -62,7 +62,21 @@ class DonateHandler(BaseHandler):
                else:
                        country = None
 
-               self.render("donate.html", countries=iso3166.countries, country=country)
+               # Get defaults
+               amount    = self.get_argument_int("amount", None)
+               currency  = self.get_argument("currency", None)
+               frequency = self.get_argument("frequency", None)
+
+               # Set default currency
+               if not currency in ("EUR", "USD"):
+                       currency = "EUR"
+
+               # Set default frequency
+               if not frequency in ("one-time", "monthly"):
+                       frequency = "one-time"
+
+               self.render("donate.html", countries=iso3166.countries,
+                       country=country, amount=amount, currency=currency, frequency=frequency)
 
        @tornado.gen.coroutine
        def post(self):
index 13b0fe5caacfeda462d8e629e9ab7b416ec056e6..2dd0b66b5f2cfcfa535582417e739288088f120a 100644 (file)
@@ -62,6 +62,17 @@ class BaseHandler(tornado.web.RequestHandler):
 
                return self.__remote_location
 
+       def get_argument_int(self, *args, **kwargs):
+               arg = self.get_argument(*args, **kwargs)
+
+               if arg is None or arg == "":
+                       return
+
+               try:
+                       return int(arg)
+               except ValueError:
+                       raise tornado.web.HTTPError(400)
+
        def get_argument_date(self, arg, *args, **kwargs):
                value = self.get_argument(arg, *args, **kwargs)
                if value is None: