Esempio n. 1
0
def copy_snapshot(request, domain):
    user = request.couch_user
    if not user.is_eula_signed():
        messages.error(request, 'You must agree to our eula to download an app')
        return project_info(request, domain)

    dom = Domain.get_by_name(domain)
    if request.method == "POST" and dom.is_snapshot:
        args = {'domain_name': request.POST['new_project_name'], 'eula_confirmed': True}
        form = DomainRegistrationForm(args)

        if request.POST.get('new_project_name', ""):
            if not dom.published:
                messages.error(request, "This project is not published and can't be downloaded")
                return project_info(request, domain)

            if form.is_valid():
                new_domain = dom.save_copy(form.clean_domain_name(), user=user)
            else:
                messages.error(request, form.errors)
                return project_info(request, domain)

            if new_domain is None:
                messages.error(request, _("A project by that name already exists"))
                return project_info(request, domain)
            dom.downloads += 1
            dom.save()
            messages.success(request, render_to_string("appstore/partials/view_wiki.html", {"pre": _("Project copied successfully!")}), extra_tags="html")
            return HttpResponseRedirect(reverse('view_app',
                args=[new_domain.name, new_domain.full_applications()[0].get_id]))
        else:
            messages.error(request, _("You must specify a name for the new project"))
            return project_info(request, domain)
    else:
        return HttpResponseRedirect(reverse('project_info', args=[domain]))
Esempio n. 2
0
def copy_snapshot(request, domain):
    user = request.couch_user
    if not user.is_eula_signed():
        messages.error(request,
                       'You must agree to our eula to download an app')
        return project_info(request, domain)

    dom = Domain.get(domain)
    if request.method == "POST" and dom.is_snapshot:
        from corehq.apps.registration.forms import DomainRegistrationForm
        args = {
            'domain_name': request.POST['new_project_name'],
            'eula_confirmed': True
        }
        form = DomainRegistrationForm(args)

        if request.POST.get('new_project_name', ""):
            if not dom.published:
                messages.error(
                    request,
                    "This project is not published and can't be downloaded")
                return project_info(request, domain)

            if form.is_valid():
                new_domain = dom.save_copy(form.cleaned_data['domain_name'],
                                           user=user)
            else:
                messages.error(request, form.errors)
                return project_info(request, domain)

            if new_domain is None:
                messages.error(request,
                               _("A project by that name already exists"))
                return project_info(request, domain)

            def inc_downloads(d):
                d.downloads += 1

            apply_update(dom, inc_downloads)

            # sign project up for trial
            create_30_day_trial(new_domain)

            messages.success(request,
                             render_to_string(
                                 "appstore/partials/view_wiki.html",
                                 {"pre": _("Project copied successfully!")}),
                             extra_tags="html")
            return HttpResponseRedirect(
                reverse('view_app',
                        args=[
                            new_domain.name,
                            new_domain.full_applications()[0].get_id
                        ]))
        else:
            messages.error(request,
                           _("You must specify a name for the new project"))
            return project_info(request, domain)
    else:
        return HttpResponseRedirect(reverse('project_info', args=[domain]))
Esempio n. 3
0
def register_domain(request, domain_type=None):
    domain_type = domain_type or 'commcare'
    assert domain_type in DOMAIN_TYPES
    _render = partial(render_registration_view, domain_type=domain_type)

    is_new = False
    referer_url = request.GET.get('referer', '')

    active_domains_for_user = Domain.active_for_user(request.user)
    if len(active_domains_for_user) <= 0 and not request.user.is_superuser:
        is_new = True
        domains_for_user = Domain.active_for_user(request.user, is_active=False)
        if len(domains_for_user) > 0:
            vals = dict(requested_domain=domains_for_user[0])
            return _render(request, 'registration/confirmation_waiting.html', {
                'requested_domain': domains_for_user[0]
            })

    if request.method == 'POST':
        nextpage = request.POST.get('next')
        org = request.POST.get('org')
        form = DomainRegistrationForm(request.POST)
        if form.is_valid():
            reqs_today = RegistrationRequest.get_requests_today()
            max_req = settings.DOMAIN_MAX_REGISTRATION_REQUESTS_PER_DAY
            if reqs_today >= max_req:
                vals = {'error_msg':'Number of domains requested today exceeds limit ('+str(max_req)+') - contact Dimagi',
                        'show_homepage_link': 1 }
                return _render(request, 'error.html', vals)

            request_new_domain(
                request, form, org, new_user=is_new, domain_type=domain_type)

            requested_domain = form.cleaned_data['domain_name']
            if is_new:
                vals = dict(alert_message="An email has been sent to %s." % request.user.username, requested_domain=requested_domain)
                return _render(request, 'registration/confirmation_sent.html', vals)
            else:
                messages.success(request, '<strong>The project {project_name} was successfully created!</strong> An email has been sent to {username} for your records.'.format(
                    username=request.user.username,
                    project_name=requested_domain
                ), extra_tags="html")

                if nextpage:
                    return HttpResponseRedirect(nextpage)
                if referer_url:
                    return redirect(referer_url)
                return HttpResponseRedirect(reverse("domain_homepage", args=[requested_domain]))
        else:
            if nextpage:
#                messages.error(request, "The new project could not be created! Please make sure to fill out all fields of the form.")
                return orgs_landing(request, org, form=form)
    else:
        form = DomainRegistrationForm(initial={'domain_type': domain_type})

    return _render(request, 'registration/domain_request.html', {
        'form': form,
        'is_new': is_new,
    })
Esempio n. 4
0
def copy_snapshot(request, snapshot):
    user = request.couch_user
    if not user.is_eula_signed():
        messages.error(request, _('You must agree to our terms of service to download an app'))
        return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))

    domain_obj = Domain.get(snapshot)
    if request.method == "POST" and domain_obj.is_snapshot:
        assert domain_obj.full_applications(include_builds=False), 'Bad attempt to copy project without any apps!'

        from corehq.apps.registration.forms import DomainRegistrationForm

        args = {
            'domain_name': request.POST['new_project_name'],
            'hr_name': request.POST['new_project_name'],
            'eula_confirmed': True,
        }
        form = DomainRegistrationForm(args)

        if request.POST.get('new_project_name', ""):
            if not domain_obj.published:
                messages.error(request, _("This project is not published and can't be downloaded"))
                return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))

            if not form.is_valid():
                messages.error(request, form.errors)
                return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))

            new_domain_name = name_to_url(form.cleaned_data['hr_name'], "project")
            with CriticalSection(['copy_domain_snapshot_{}_to_{}'.format(domain_obj.name, new_domain_name)]):
                try:
                    new_domain = domain_obj.save_copy(new_domain_name,
                                                      new_hr_name=form.cleaned_data['hr_name'],
                                                      user=user)
                    if new_domain.commtrack_enabled:
                        new_domain.convert_to_commtrack()
                    ensure_explicit_community_subscription(
                        new_domain.name, date.today(), SubscriptionAdjustmentMethod.USER,
                        web_user=user.username,
                    )
                except NameUnavailableException:
                    messages.error(request, _("A project by that name already exists"))
                    return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))

            def inc_downloads(d):
                d.downloads += 1

            apply_update(domain_obj, inc_downloads)
            messages.success(request, render_to_string("appstore/partials/view_wiki.html",
                                                       {"pre": _("Project copied successfully!")}),
                             extra_tags="html")
            return HttpResponseRedirect(reverse('view_app',
                                                args=[new_domain.name, new_domain.full_applications()[0].get_id]))
        else:
            messages.error(request, _("You must specify a name for the new project"))
            return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))
    else:
        return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))
Esempio n. 5
0
def copy_snapshot(request, snapshot):
    user = request.couch_user
    if not user.is_eula_signed():
        messages.error(request, _('You must agree to our terms of service to download an app'))
        return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))

    domain_obj = Domain.get(snapshot)
    if request.method == "POST" and domain_obj.is_snapshot:
        assert domain_obj.full_applications(include_builds=False), 'Bad attempt to copy project without any apps!'

        from corehq.apps.registration.forms import DomainRegistrationForm

        args = {
            'domain_name': request.POST['new_project_name'],
            'hr_name': request.POST['new_project_name'],
            'eula_confirmed': True,
        }
        form = DomainRegistrationForm(args)

        if request.POST.get('new_project_name', ""):
            if not domain_obj.published:
                messages.error(request, _("This project is not published and can't be downloaded"))
                return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))

            if not form.is_valid():
                messages.error(request, form.errors)
                return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))

            new_domain_name = name_to_url(form.cleaned_data['hr_name'], "project")
            with CriticalSection(['copy_domain_snapshot_{}_to_{}'.format(domain_obj.name, new_domain_name)]):
                try:
                    new_domain = domain_obj.save_copy(new_domain_name,
                                                      new_hr_name=form.cleaned_data['hr_name'],
                                                      user=user)
                    if new_domain.commtrack_enabled:
                        new_domain.convert_to_commtrack()
                    ensure_explicit_community_subscription(
                        new_domain.name, date.today(), SubscriptionAdjustmentMethod.USER,
                        web_user=user.username,
                    )
                except NameUnavailableException:
                    messages.error(request, _("A project by that name already exists"))
                    return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))

            def inc_downloads(d):
                d.downloads += 1

            apply_update(domain_obj, inc_downloads)
            messages.success(request, render_to_string("appstore/partials/view_wiki.html",
                                                       {"pre": _("Project copied successfully!")}),
                             extra_tags="html")
            return HttpResponseRedirect(reverse('view_app',
                                                args=[new_domain.name, new_domain.full_applications()[0].get_id]))
        else:
            messages.error(request, _("You must specify a name for the new project"))
            return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))
    else:
        return HttpResponseRedirect(reverse(ProjectInformationView.urlname, args=[snapshot]))
Esempio n. 6
0
    def post(self, request, *args, **kwargs):
        referer_url = request.GET.get('referer', '')
        nextpage = request.POST.get('next')
        form = DomainRegistrationForm(request.POST)
        context = self.get_context_data(form=form)
        if form.is_valid():
            reqs_today = RegistrationRequest.get_requests_today()
            max_req = settings.DOMAIN_MAX_REGISTRATION_REQUESTS_PER_DAY
            if reqs_today >= max_req:
                context.update({
                    'current_page': {
                        'page_name': _('Oops!')
                    },
                    'error_msg':
                    _('Number of domains requested today exceeds limit (%d) - contact Dimagi'
                      ) % max_req,
                    'show_homepage_link':
                    1
                })
                return render(request, 'error.html', context)

            try:
                domain_name = request_new_domain(request,
                                                 form,
                                                 is_new_user=self.is_new_user)
            except NameUnavailableException:
                context.update({
                    'current_page': {
                        'page_name': _('Oops!')
                    },
                    'error_msg':
                    _('Project name already taken - please try another'),
                    'show_homepage_link':
                    1
                })
                return render(request, 'error.html', context)

            if self.is_new_user:
                context.update({
                    'requested_domain': domain_name,
                    'current_page': {
                        'page_name': _('Confirm Account')
                    },
                })
                track_workflow(self.request.user.email, "Created new project")
                return render(request, 'registration/confirmation_sent.html',
                              context)
            else:
                if nextpage:
                    return HttpResponseRedirect(nextpage)
                if referer_url:
                    return redirect(referer_url)
                return HttpResponseRedirect(
                    reverse("domain_homepage", args=[domain_name]))

        return self.render_to_response(context)
Esempio n. 7
0
def copy_snapshot(request, domain):
    user = request.couch_user
    if not user.is_eula_signed():
        messages.error(request, 'You must agree to our eula to download an app')
        return project_info(request, domain)

    dom = Domain.get(domain)
    if request.method == "POST" and dom.is_snapshot:
        assert dom.full_applications(include_builds=False), 'Bad attempt to copy project without any apps!'

        from corehq.apps.registration.forms import DomainRegistrationForm

        args = {
            'domain_name': request.POST['new_project_name'],
            'hr_name': request.POST['new_project_name'],
            'eula_confirmed': True,
        }
        form = DomainRegistrationForm(args)

        if request.POST.get('new_project_name', ""):
            if not dom.published:
                messages.error(request, _("This project is not published and can't be downloaded"))
                return project_info(request, domain)

            if not form.is_valid():
                messages.error(request, form.errors)
                return project_info(request, domain)

            new_domain_name = name_to_url(form.cleaned_data['hr_name'], "project")
            with CriticalSection(['copy_domain_snapshot_{}_to_{}'.format(dom.name, new_domain_name)]):
                try:
                    new_domain = dom.save_copy(new_domain_name,
                                               new_hr_name=form.cleaned_data['hr_name'],
                                               user=user)
                except NameUnavailableException:
                    messages.error(request, _("A project by that name already exists"))
                    return project_info(request, domain)

                # sign new project up for trial
                create_30_day_trial(new_domain)

            def inc_downloads(d):
                d.downloads += 1

            apply_update(dom, inc_downloads)
            messages.success(request, render_to_string("appstore/partials/view_wiki.html",
                                                       {"pre": _("Project copied successfully!")}),
                             extra_tags="html")
            return HttpResponseRedirect(reverse('view_app',
                                                args=[new_domain.name, new_domain.full_applications()[0].get_id]))
        else:
            messages.error(request, _("You must specify a name for the new project"))
            return project_info(request, domain)
    else:
        return HttpResponseRedirect(reverse('project_info', args=[domain]))
Esempio n. 8
0
    def post(self, request, *args, **kwargs):
        referer_url = request.GET.get('referer', '')
        nextpage = request.POST.get('next')
        form = DomainRegistrationForm(request.POST)
        context = self.get_context_data(form=form)
        if not form.is_valid():
            return self.render_to_response(context)

        if settings.RESTRICT_DOMAIN_CREATION and not request.user.is_superuser:
            context.update({
                'current_page': {'page_name': _('Oops!')},
                'error_msg': _('Your organization has requested that project creation be restricted. '
                               'For more information, please speak to your administrator.'),
            })
            return render(request, 'error.html', context)

        reqs_today = RegistrationRequest.get_requests_today()
        max_req = settings.DOMAIN_MAX_REGISTRATION_REQUESTS_PER_DAY
        if reqs_today >= max_req:
            context.update({
                'current_page': {'page_name': _('Oops!')},
                'error_msg': _(
                    'Number of projects requested today exceeds limit (%d) - contact Dimagi'
                ) % max_req,
                'show_homepage_link': 1
            })
            return render(request, 'error.html', context)

        try:
            domain_name = request_new_domain(request, form, is_new_user=self.is_new_user)
        except NameUnavailableException:
            context.update({
                'current_page': {'page_name': _('Oops!')},
                'error_msg': _('Project name already taken - please try another'),
                'show_homepage_link': 1
            })
            return render(request, 'error.html', context)

        if self.is_new_user:
            context.update({
                'requested_domain': domain_name,
                'current_page': {'page_name': _('Confirm Account')},
            })
            track_workflow(self.request.user.email, "Created new project")
            return render(request, 'registration/confirmation_sent.html', context)

        if nextpage:
            return HttpResponseRedirect(nextpage)
        if referer_url:
            return redirect(referer_url)
        return HttpResponseRedirect(reverse("domain_homepage", args=[domain_name]))
Esempio n. 9
0
def copy_snapshot(request, domain):
    dom = Domain.get_by_name(domain)
    if request.method == "POST" and dom.is_snapshot:
        args = {'domain_name': request.POST['new_project_name'], 'tos_confirmed': True}
        form = DomainRegistrationForm(args)

        if form.is_valid():
            new_domain = dom.save_copy(form.clean_domain_name(), user=request.couch_user)
        else:
            messages.error(request, form.errors)
            return project_info(request, domain)

        if new_domain is None:
            messages.error(request, "A project by that name already exists")
            return project_info(request, domain)

        messages.success(request, "Project copied successfully!")
        return redirect("domain_project_settings", new_domain.name)
Esempio n. 10
0
    def post(self, request, *args, **kwargs):
        referer_url = request.GET.get('referer', '')
        nextpage = request.POST.get('next')
        form = DomainRegistrationForm(request.POST, current_user=request.couch_user)
        context = self.get_context_data(form=form)
        if form.is_valid():
            reqs_today = RegistrationRequest.get_requests_today()
            max_req = settings.DOMAIN_MAX_REGISTRATION_REQUESTS_PER_DAY
            if reqs_today >= max_req:
                context.update({
                    'current_page': {'page_name': _('Oops!')},
                    'error_msg': _(
                        'Number of domains requested today exceeds limit (%d) - contact Dimagi'
                    ) % max_req,
                    'show_homepage_link': 1
                })
                return render(request, 'error.html', context)

            try:
                domain_name = request_new_domain(
                    request, form, is_new_user=self.is_new_user)
            except NameUnavailableException:
                context.update({
                    'current_page': {'page_name': _('Oops!')},
                    'error_msg': _('Project name already taken - please try another'),
                    'show_homepage_link': 1
                })
                return render(request, 'error.html', context)

            if self.is_new_user:
                context.update({
                    'requested_domain': domain_name,
                    'track_domain_registration': True,
                    'current_page': {'page_name': _('Confirm Account')},
                })
                return render(request, 'registration/confirmation_sent.html', context)
            else:
                if nextpage:
                    return HttpResponseRedirect(nextpage)
                if referer_url:
                    return redirect(referer_url)
                return HttpResponseRedirect(reverse("domain_homepage", args=[domain_name]))

        return self.render_to_response(context)
Esempio n. 11
0
def orgs_landing(request, org, template="orgs/orgs_landing.html", form=None, add_form=None, invite_member_form=None,
                 add_team_form=None, update_form=None, tab=None):
    organization = request.organization

    class LandingNotification(Notification):
        doc_type = 'OrgLandingNotification'

        def template(self):
            return 'orgs/partials/landing_notification.html'

    MainNotification.display_if_needed(messages, request, ctxt={"org": organization})
    LandingNotification.display_if_needed(messages, request)

    reg_form_empty = not form
    add_form_empty = not add_form
    invite_member_form_empty = not invite_member_form
    add_team_form_empty = not add_team_form

    reg_form = form or DomainRegistrationForm(initial={'org': organization.name})
    add_form = add_form or AddProjectForm(org)
    invite_member_form = invite_member_form or InviteMemberForm(org)
    add_team_form = add_team_form or AddTeamForm(org)

    ctxt = base_context(request, organization, update_form=update_form)
    user_domains = []
    req_domains = []
    # display a notification for each org request that hasn't previously been seen
    if request.couch_user.is_org_admin(org):
        requests = OrgRequest.get_requests(org)
        for req in requests:
            if req.seen or req.domain in [d.name for d in ctxt["domains"]]:
                continue
            messages.info(request, render_to_string("orgs/partials/org_request_notification.html",
                {"requesting_user": WebUser.get(req.requested_by).username, "org_req": req, "org": organization}),
                extra_tags="html")

        def format_domains(dom_list, extra=None):
            extra = extra or []
            dom_list = list(set(filter(lambda d: d not in ctxt["domains"] + extra, dom_list)))
            return [Domain.get_by_name(d) for d in dom_list]

        # get the existing domains that an org admin would add to the organization
        user_domains = request.couch_user.domains or []
        req_domains = [req.domain for req in requests]
        user_domains = format_domains(user_domains)
        req_domains = format_domains(req_domains, [d.name for d in user_domains if d])

    ctxt.update(dict(reg_form=reg_form, add_form=add_form, reg_form_empty=reg_form_empty, add_form_empty=add_form_empty,
                invite_member_form=invite_member_form, invite_member_form_empty=invite_member_form_empty,
                add_team_form=add_team_form, add_team_form_empty=add_team_form_empty, tab="projects",
                user_domains=user_domains, req_domains=req_domains))
    return render(request, template, ctxt)
Esempio n. 12
0
    def get_context_data(self, **kwargs):
        request = self.request
        if (not request.couch_user) or request.couch_user.is_commcare_user():
            raise Http404()

        context = super(RegisterDomainView, self).get_context_data(**kwargs)
        context.update(get_domain_context())

        context.update({
            'form': kwargs.get('form') or DomainRegistrationForm(),
            'is_new_user': self.is_new_user,
        })
        return context
Esempio n. 13
0
def register_domain(request, domain_type=None):
    domain_type = domain_type or 'commcare'
    assert domain_type in DOMAIN_TYPES
    context = get_domain_context(domain_type)

    is_new = False
    referer_url = request.GET.get('referer', '')

    active_domains_for_user = Domain.active_for_user(request.user)
    if len(active_domains_for_user) <= 0 and not request.user.is_superuser:
        is_new = True
        domains_for_user = Domain.active_for_user(request.user,
                                                  is_active=False)
        if len(domains_for_user) > 0:
            context['requested_domain'] = domains_for_user[0]
            return render(request, 'registration/confirmation_waiting.html',
                          context)

    if request.method == 'POST':
        nextpage = request.POST.get('next')
        org = request.POST.get('org')
        form = DomainRegistrationForm(request.POST)
        if form.is_valid():
            reqs_today = RegistrationRequest.get_requests_today()
            max_req = settings.DOMAIN_MAX_REGISTRATION_REQUESTS_PER_DAY
            if reqs_today >= max_req:
                context.update({
                    'error_msg':
                    _('Number of domains requested today exceeds limit (%d) - contact Dimagi'
                      ) % max_req,
                    'show_homepage_link':
                    1
                })
                return render(request, 'error.html', context)

            request_new_domain(request,
                               form,
                               org,
                               new_user=is_new,
                               domain_type=domain_type)

            requested_domain = form.cleaned_data['domain_name']
            if is_new:
                context.update({
                    'alert_message':
                    _("An email has been sent to %s.") % request.user.username,
                    'requested_domain':
                    requested_domain
                })
                return render(request, 'registration/confirmation_sent.html',
                              context)
            else:
                messages.success(
                    request,
                    _('<strong>The project {project} was successfully created!</strong> '
                      'An email has been sent to {user} for your records.'
                      ).format(project=requested_domain,
                               user=request.user.username),
                    extra_tags="html")

                if nextpage:
                    return HttpResponseRedirect(nextpage)
                if referer_url:
                    return redirect(referer_url)
                return HttpResponseRedirect(
                    reverse("domain_homepage", args=[requested_domain]))
        else:
            if nextpage:
                return orgs_landing(request, org, form=form)
    else:
        form = DomainRegistrationForm(initial={'domain_type': domain_type})

    context.update({
        'form': form,
        'is_new': is_new,
    })
    return render(request, 'registration/domain_request.html', context)
Esempio n. 14
0
def register_domain(request, domain_type=None):
    domain_type = domain_type or 'commcare'
    if domain_type not in DOMAIN_TYPES or request.couch_user.is_commcare_user():
        raise Http404()

    context = get_domain_context(domain_type)

    is_new = False
    referer_url = request.GET.get('referer', '')

    active_domains_for_user = Domain.active_for_user(request.user)
    if len(active_domains_for_user) <= 0 and not request.user.is_superuser:
        is_new = True
        domains_for_user = Domain.active_for_user(request.user, is_active=False)
        if len(domains_for_user) > 0:
            context['requested_domain'] = domains_for_user[0]
            return render(request, 'registration/confirmation_waiting.html',
                    context)

    if request.method == 'POST':
        nextpage = request.POST.get('next')
        org = request.POST.get('org')
        form = DomainRegistrationForm(request.POST)
        if form.is_valid():
            reqs_today = RegistrationRequest.get_requests_today()
            max_req = settings.DOMAIN_MAX_REGISTRATION_REQUESTS_PER_DAY
            if reqs_today >= max_req:
                context.update({
                    'error_msg': _(
                        'Number of domains requested today exceeds limit (%d) - contact Dimagi'
                    ) % max_req,
                    'show_homepage_link': 1
                })
                return render(request, 'error.html', context)

            request_new_domain(
                request, form, org, new_user=is_new, domain_type=domain_type)

            requested_domain = form.cleaned_data['domain_name']
            if is_new:
                context.update({
                    'alert_message': _("An email has been sent to %s.") % request.user.username,
                    'requested_domain': requested_domain
                })
                return render(request, 'registration/confirmation_sent.html',
                        context)
            else:
                messages.success(request, _(
                    '<strong>The project {project} was successfully created!</strong> '
                    'An email has been sent to {user} for your records.').format(
                    project=requested_domain, user=request.user.username),
                    extra_tags="html")

                if nextpage:
                    return HttpResponseRedirect(nextpage)
                if referer_url:
                    return redirect(referer_url)
                return HttpResponseRedirect(reverse("domain_homepage", args=[requested_domain]))
        else:
            if nextpage:
                return orgs_landing(request, org, form=form)
    else:
        form = DomainRegistrationForm(initial={'domain_type': domain_type})

    context.update({
        'form': form,
        'is_new': is_new,
    })
    return render(request, 'registration/domain_request.html', context)
Esempio n. 15
0
def register_domain(request, domain_type=None):
    domain_type = domain_type or 'commcare'
    if domain_type not in DOMAIN_TYPES or (not request.couch_user) or request.couch_user.is_commcare_user():
        raise Http404()

    context = get_domain_context(domain_type)

    is_new = False
    referer_url = request.GET.get('referer', '')

    active_domains_for_user = Domain.active_for_user(request.user)
    if len(active_domains_for_user) <= 0 and not request.user.is_superuser:
        is_new = True
        domains_for_user = Domain.active_for_user(request.user, is_active=False)
        if len(domains_for_user) > 0:
            context['requested_domain'] = domains_for_user[0]
            return render(request, 'registration/confirmation_waiting.html',
                    context)

    if request.method == 'POST':
        nextpage = request.POST.get('next')
        org = request.POST.get('org')
        form = DomainRegistrationForm(request.POST)
        if form.is_valid():
            reqs_today = RegistrationRequest.get_requests_today()
            max_req = settings.DOMAIN_MAX_REGISTRATION_REQUESTS_PER_DAY
            if reqs_today >= max_req:
                context.update({
                    'error_msg': _(
                        'Number of domains requested today exceeds limit (%d) - contact Dimagi'
                    ) % max_req,
                    'show_homepage_link': 1
                })
                return render(request, 'error.html', context)

            try:
                domain_name = request_new_domain(
                    request, form, org, new_user=is_new, domain_type=domain_type)
            except NameUnavailableException:
                context.update({
                    'error_msg': _('Project name already taken - please try another'),
                    'show_homepage_link': 1
                })
                return render(request, 'error.html', context)

            if is_new:
                context.update({
                    'alert_message': _("An email has been sent to %s.") % request.user.username,
                    'requested_domain': domain_name,
                    'track_domain_registration': True,
                })
                return render(request, 'registration/confirmation_sent.html',
                        context)
            else:
                if nextpage:
                    return HttpResponseRedirect(nextpage)
                if referer_url:
                    return redirect(referer_url)
                return HttpResponseRedirect(reverse("domain_homepage", args=[domain_name]))
        else:
            if nextpage:
                return orgs_landing(request, org, form=form)
    else:
        form = DomainRegistrationForm(initial={'domain_type': domain_type})

    context.update({
        'form': form,
        'is_new': is_new,
    })
    return render(request, 'registration/domain_request.html', context)
Esempio n. 16
0
def register_domain(request, domain_type=None):
    domain_type = domain_type or "commcare"
    assert domain_type in DOMAIN_TYPES
    context = get_domain_context(domain_type)

    is_new = False
    referer_url = request.GET.get("referer", "")

    active_domains_for_user = Domain.active_for_user(request.user)
    if len(active_domains_for_user) <= 0 and not request.user.is_superuser:
        is_new = True
        domains_for_user = Domain.active_for_user(request.user, is_active=False)
        if len(domains_for_user) > 0:
            context["requested_domain"] = domains_for_user[0]
            return render(request, "registration/confirmation_waiting.html", context)

    if request.method == "POST":
        nextpage = request.POST.get("next")
        org = request.POST.get("org")
        form = DomainRegistrationForm(request.POST)
        if form.is_valid():
            reqs_today = RegistrationRequest.get_requests_today()
            max_req = settings.DOMAIN_MAX_REGISTRATION_REQUESTS_PER_DAY
            if reqs_today >= max_req:
                context.update(
                    {
                        "error_msg": _("Number of domains requested today exceeds limit (%d) - contact Dimagi")
                        % max_req,
                        "show_homepage_link": 1,
                    }
                )
                return render(request, "error.html", context)

            request_new_domain(request, form, org, new_user=is_new, domain_type=domain_type)

            requested_domain = form.cleaned_data["domain_name"]
            if is_new:
                context.update(
                    {
                        "alert_message": _("An email has been sent to %s.") % request.user.username,
                        "requested_domain": requested_domain,
                    }
                )
                return render(request, "registration/confirmation_sent.html", context)
            else:
                messages.success(
                    request,
                    _(
                        "<strong>The project {project} was successfully created!</strong> "
                        "An email has been sent to {user} for your records."
                    ).format(project=requested_domain, user=request.user.username),
                    extra_tags="html",
                )

                if nextpage:
                    return HttpResponseRedirect(nextpage)
                if referer_url:
                    return redirect(referer_url)
                return HttpResponseRedirect(reverse("domain_homepage", args=[requested_domain]))
        else:
            if nextpage:
                return orgs_landing(request, org, form=form)
    else:
        form = DomainRegistrationForm(initial={"domain_type": domain_type})

    context.update({"form": form, "is_new": is_new})
    return render(request, "registration/domain_request.html", context)
Esempio n. 17
0
def register_domain(request, domain_type=None):
    domain_type = domain_type or 'commcare'
    if domain_type not in DOMAIN_TYPES or request.couch_user.is_commcare_user(
    ):
        raise Http404()

    context = get_domain_context(domain_type)

    is_new = False
    referer_url = request.GET.get('referer', '')

    active_domains_for_user = Domain.active_for_user(request.user)
    if len(active_domains_for_user) <= 0 and not request.user.is_superuser:
        is_new = True
        domains_for_user = Domain.active_for_user(request.user,
                                                  is_active=False)
        if len(domains_for_user) > 0:
            context['requested_domain'] = domains_for_user[0]
            return render(request, 'registration/confirmation_waiting.html',
                          context)

    if request.method == 'POST':
        nextpage = request.POST.get('next')
        org = request.POST.get('org')
        form = DomainRegistrationForm(request.POST)
        if form.is_valid():
            reqs_today = RegistrationRequest.get_requests_today()
            max_req = settings.DOMAIN_MAX_REGISTRATION_REQUESTS_PER_DAY
            if reqs_today >= max_req:
                context.update({
                    'error_msg':
                    _('Number of domains requested today exceeds limit (%d) - contact Dimagi'
                      ) % max_req,
                    'show_homepage_link':
                    1
                })
                return render(request, 'error.html', context)

            try:
                domain_name = request_new_domain(request,
                                                 form,
                                                 org,
                                                 new_user=is_new,
                                                 domain_type=domain_type)
            except NameUnavailableException:
                context.update({
                    'error_msg':
                    _('Project name already taken - please try another'),
                    'show_homepage_link':
                    1
                })
                return render(request, 'error.html', context)

            if is_new:
                context.update({
                    'alert_message':
                    _("An email has been sent to %s.") % request.user.username,
                    'requested_domain':
                    domain_name,
                    'track_domain_registration':
                    True,
                })
                return render(request, 'registration/confirmation_sent.html',
                              context)
            else:
                if nextpage:
                    return HttpResponseRedirect(nextpage)
                if referer_url:
                    return redirect(referer_url)
                return HttpResponseRedirect(
                    reverse("domain_homepage", args=[domain_name]))
        else:
            if nextpage:
                return orgs_landing(request, org, form=form)
    else:
        form = DomainRegistrationForm(initial={'domain_type': domain_type})

    context.update({
        'form': form,
        'is_new': is_new,
    })
    return render(request, 'registration/domain_request.html', context)
Esempio n. 18
0
def copy_snapshot(request, domain):
    user = request.couch_user
    if not user.is_eula_signed():
        messages.error(request,
                       'You must agree to our eula to download an app')
        return project_info(request, domain)

    dom = Domain.get(domain)
    if request.method == "POST" and dom.is_snapshot:
        assert dom.full_applications(
            include_builds=False
        ), 'Bad attempt to copy project without any apps!'

        from corehq.apps.registration.forms import DomainRegistrationForm

        args = {
            'domain_name': request.POST['new_project_name'],
            'hr_name': request.POST['new_project_name'],
            'eula_confirmed': True,
        }
        form = DomainRegistrationForm(args)

        if request.POST.get('new_project_name', ""):
            if not dom.published:
                messages.error(
                    request,
                    _("This project is not published and can't be downloaded"))
                return project_info(request, domain)

            if not form.is_valid():
                messages.error(request, form.errors)
                return project_info(request, domain)

            new_domain_name = form.cleaned_data['hr_name']
            with CriticalSection([
                    'copy_domain_snapshot_{}_to_{}'.format(
                        dom.name, new_domain_name)
            ]):
                try:
                    new_domain = dom.save_copy(
                        new_domain_name,
                        new_hr_name=form.cleaned_data['hr_name'],
                        user=user)
                except NameUnavailableException:
                    messages.error(request,
                                   _("A project by that name already exists"))
                    return project_info(request, domain)

                # sign new project up for trial
                create_30_day_trial(new_domain)

            def inc_downloads(d):
                d.downloads += 1

            apply_update(dom, inc_downloads)
            messages.success(request,
                             render_to_string(
                                 "appstore/partials/view_wiki.html",
                                 {"pre": _("Project copied successfully!")}),
                             extra_tags="html")
            return HttpResponseRedirect(
                reverse('view_app',
                        args=[
                            new_domain.name,
                            new_domain.full_applications()[0].get_id
                        ]))
        else:
            messages.error(request,
                           _("You must specify a name for the new project"))
            return project_info(request, domain)
    else:
        return HttpResponseRedirect(reverse('project_info', args=[domain]))