<commit_before># -*- coding: utf-8 -*-
from django.shortcuts import render
from django.core.urlresolvers import reverse
from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView
from django.conf import settings

from paypal.standard.forms import PayPalPaymentsForm
from braces.views import LoginRequiredMixin

from .models import (Plans, UserPlanData, PaymentMethods)
from accounts.models import TimtecUser

from django.forms import ModelForm

from paypal.standard.ipn.signals import (payment_was_successful,
                                            subscription_signup,
                        payment_was_flagged, payment_was_refunded,
                        payment_was_reversed)
from django.core.mail import send_mail


class AccountPaymentView(LoginRequiredMixin, TemplateView):
    template_name = "payment.html"
    context_object_name = 'context'
    base_url = "https://escolamupi.com.br"

    if settings.DEBUG:
        base_url = "http://localhost:8000"

    def get(self, request, **kwargs):
        # Se chegou aqui, o cara tem plano, entao, vamo renderizar o que ele
        # pode usar pra pagar:
        # ~~Get all possible providers~~ In context
        # Create forms for each provider
        # Render to response

        ## por enquanto, so paypal


        return super(AccountPaymentView,
                        self).get(request, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(AccountPaymentView, self).get_context_data(**kwargs)
        upd = UserPlanData.objects.get(user=self.request.user)
        pm_list = PaymentMethods.objects.filter(plans=upd.plan)

        ## For each payment method, creates payment form. For now, just paypal

        import ast
        for pm in pm_list:
            # TODO: test payment_method type. (for now, all paypal)
            # make the form creation a function for each payment type
            try:
                data = ast.literal_eval(pm.data)
            except:
                pass
            else:
                pm.data = data
                u = self.request.user

                pm.data['custom'] = "user_mail=" + u.email + "&user_id=" \
                                        + str(u.id)
                pm.data['notify_url'] = settings.SITE_URL + \
                                            reverse('paypal-ipn')
                pm.data['return_url'] = settings.SITE_URL + "/my-courses/"
                pm.data['cancel_return'] = settings.SITE_URL \
                                            + "/accounts/payment"
                pm.data['business'] = settings.PAYPAL_RECEIVER_EMAIL

                try:
                    pm.data['cmd'] == '_xclick-subscriptions'
                except:
                    form = PayPalPaymentsForm(initial=pm.data)
                else:
                    form = PayPalPaymentsForm(initial=pm.data,
                                                button_type="subscribe")

                if settings.PAYPAL_TEST:
                    context = { "form" : form.sandbox(), }
                    pm.form = form.sandbox()
                else:
                    context = { "form" : form, }
                    pm.form = form.sandbox()

        context['payment_methods'] = pm_list
        return context

    def dispatch(self, *args, **kwargs):
        from django.shortcuts import redirect
        try:
            upd = UserPlanData.objects.get(user_id=self.request.user.id)
        except:
            return redirect('/accounts/plans', *args, **kwargs)
        else:
            if upd.user_status:
                return redirect('/my-courses', *args, **kwargs)<commit_msg>Payment: fix for render always sandbox form<commit_after># -*- coding: utf-8 -*-
from django.shortcuts import render
from django.core.urlresolvers import reverse
from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView
from django.conf import settings

from paypal.standard.forms import PayPalPaymentsForm
from braces.views import LoginRequiredMixin

from .models import (Plans, UserPlanData, PaymentMethods)
from accounts.models import TimtecUser

from django.forms import ModelForm

from paypal.standard.ipn.signals import (payment_was_successful,
                                            subscription_signup,
                        payment_was_flagged, payment_was_refunded,
                        payment_was_reversed)
from django.core.mail import send_mail


class AccountPaymentView(LoginRequiredMixin, TemplateView):
    template_name = "payment.html"
    context_object_name = 'context'
    base_url = "https://escolamupi.com.br"

    if settings.DEBUG:
        base_url = "http://localhost:8000"

    def get(self, request, **kwargs):
        # Se chegou aqui, o cara tem plano, entao, vamo renderizar o que ele
        # pode usar pra pagar:
        # ~~Get all possible providers~~ In context
        # Create forms for each provider
        # Render to response

        ## por enquanto, so paypal


        return super(AccountPaymentView,
                        self).get(request, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(AccountPaymentView, self).get_context_data(**kwargs)
        upd = UserPlanData.objects.get(user=self.request.user)
        pm_list = PaymentMethods.objects.filter(plans=upd.plan)

        ## For each payment method, creates payment form. For now, just paypal

        import ast
        for pm in pm_list:
            # TODO: test payment_method type. (for now, all paypal)
            # make the form creation a function for each payment type
            try:
                data = ast.literal_eval(pm.data)
            except:
                pass
            else:
                pm.data = data
                u = self.request.user

                pm.data['custom'] = "user_mail=" + u.email + "&user_id=" \
                                        + str(u.id)
                pm.data['notify_url'] = settings.SITE_URL + \
                                            reverse('paypal-ipn')
                pm.data['return_url'] = settings.SITE_URL + "/my-courses/"
                pm.data['cancel_return'] = settings.SITE_URL \
                                            + "/accounts/payment"
                pm.data['business'] = settings.PAYPAL_RECEIVER_EMAIL

                try:
                    pm.data['cmd'] == '_xclick-subscriptions'
                except:
                    form = PayPalPaymentsForm(initial=pm.data)
                else:
                    form = PayPalPaymentsForm(initial=pm.data,
                                                button_type="subscribe")

                if settings.PAYPAL_TEST:
                    context = { "form" : form.sandbox(), }
                    pm.form = form.sandbox()
                else:
                    context = { "form" : form, }
                    pm.form = form.render()

        context['payment_methods'] = pm_list
        return context

    def dispatch(self, *args, **kwargs):
        from django.shortcuts import redirect
        try:
            upd = UserPlanData.objects.get(user_id=self.request.user.id)
        except:
            return redirect('/accounts/plans', *args, **kwargs)
        else:
            if upd.user_status:
                return redirect('/my-courses', *args, **kwargs)
{'fraction_non_alphanumeric': 0.09049100500473421, 'fraction_numerical': 0.0013526308670363857, 'mean_word_length': 1.733456561922366, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4356134', 'n_tokens_mistral': 1989, 'n_tokens_neox': 1880, 'n_words': 473}