예제 #1
0
def request_new_domain(request, form, org, domain_type=None, new_user=True):
    now = datetime.utcnow()
    current_user = CouchUser.from_django_user(request.user)

    commtrack_enabled = domain_type == 'commtrack'

    dom_req = RegistrationRequest()
    if new_user:
        dom_req.request_time = now
        dom_req.request_ip = get_ip(request)
        dom_req.activation_guid = uuid.uuid1().hex

    new_domain = Domain(
        name=form.cleaned_data['domain_name'],
        is_active=False,
        date_created=datetime.utcnow(),
        commtrack_enabled=commtrack_enabled,
        locations_enabled=commtrack_enabled,
        creating_user=current_user.username,
        secure_submissions=True,
    )

    if commtrack_enabled:
        enable_commtrack_previews(new_domain)

    if form.cleaned_data.get('domain_timezone'):
        new_domain.default_timezone = form.cleaned_data['domain_timezone']

    if org:
        new_domain.organization = org
        new_domain.hr_name = request.POST.get('domain_hrname', None) or new_domain.name

    if not new_user:
        new_domain.is_active = True

    # ensure no duplicate domain documents get created on cloudant
    new_domain.save(**get_safe_write_kwargs())

    if not new_domain.name:
        new_domain.name = new_domain._id
        new_domain.save() # we need to get the name from the _id

    create_30_day_trial(new_domain)

    dom_req.domain = new_domain.name

    if request.user.is_authenticated():
        if not current_user:
            current_user = WebUser()
            current_user.sync_from_django_user(request.user)
            current_user.save()
        current_user.add_domain_membership(new_domain.name, is_admin=True)
        current_user.save()
        dom_req.requesting_user_username = request.user.username
        dom_req.new_user_username = request.user.username

    if new_user:
        dom_req.save()
        send_domain_registration_email(request.user.email,
                                       dom_req.domain,
                                       dom_req.activation_guid)
    else:
        send_global_domain_registration_email(request.user, new_domain.name)
    send_new_request_update_email(request.user, get_ip(request), new_domain.name, is_new_user=new_user)
예제 #2
0
def bootstrap_commtrack_settings_if_necessary(domain, requisitions_enabled=False):
    """
    Create a new CommtrackConfig object for a domain
    if it does not already exist.

    This adds some collection of default products, programs,
    SMS keywords, etc.
    """
    def _needs_commtrack_config(domain):
        return (domain and
                domain.commtrack_enabled and
                not CommtrackConfig.for_domain(domain.name))

    if not _needs_commtrack_config(domain):
        return

    config = CommtrackConfig(
        domain=domain.name,
        multiaction_enabled=True,
        multiaction_keyword='report',
        actions=[
            CommtrackActionConfig(
                action='receipts',
                keyword='r',
                caption='Received',
            ),
            CommtrackActionConfig(
                action='consumption',
                keyword='c',
                caption='Consumed',
            ),
            CommtrackActionConfig(
                action='consumption',
                subaction='loss',
                keyword='l',
                caption='Losses',
            ),
            CommtrackActionConfig(
                action='stockonhand',
                keyword='soh',
                caption='Stock on hand',
            ),
            CommtrackActionConfig(
                action='stockout',
                keyword='so',
                caption='Stock-out',
            ),
        ],
    )

    if requisitions_enabled:
        config.requisition_config = get_default_requisition_config()

    config.save()

    program = get_or_create_default_program(domain.name)

    # Enable feature flags if necessary - this is required by exchange
    # and should have no effect on changing the project settings directly
    enable_commtrack_previews(domain)

    return config
예제 #3
0
파일: util.py 프로젝트: jmaina/commcare-hq
def bootstrap_commtrack_settings_if_necessary(domain, requisitions_enabled=False):
    """
    Create a new CommtrackConfig object for a domain
    if it does not already exist.


    This adds some collection of default products, programs,
    SMS keywords, etc.
    """
    def _needs_commtrack_config(domain):
        return (domain and
                domain.commtrack_enabled and
                not CommtrackConfig.for_domain(domain.name))

    if not _needs_commtrack_config(domain):
        return

    config = CommtrackConfig(
        domain=domain.name,
        multiaction_enabled=True,
        multiaction_keyword='report',
        actions=[
            CommtrackActionConfig(
                action='receipts',
                keyword='r',
                caption='Received',
            ),
            CommtrackActionConfig(
                action='consumption',
                keyword='c',
                caption='Consumed',
            ),
            CommtrackActionConfig(
                action='consumption',
                subaction='loss',
                keyword='l',
                caption='Losses',
            ),
            CommtrackActionConfig(
                action='stockonhand',
                keyword='soh',
                caption='Stock on hand',
            ),
            CommtrackActionConfig(
                action='stockout',
                keyword='so',
                caption='Stock-out',
            ),
        ],
    )

    if requisitions_enabled:
        config.requisition_config = get_default_requisition_config()

    config.save()

    program = get_or_create_default_program(domain.name)
    make_product(domain.name, 'Sample Product 1', 'pp', program.get_id)
    make_product(domain.name, 'Sample Product 2', 'pq', program.get_id)
    make_product(domain.name, 'Sample Product 3', 'pr', program.get_id)

    # this method is called during domain's post save, so this
    # is a little tricky, but it happens after the config is
    # created so should not cause problems
    domain.save()

    # Enable feature flags if necessary - this is required by exchange
    # and should have no effect on changing the project settings directly
    enable_commtrack_previews(domain)

    return config