Пример #1
0
def create_edit_location(request, location_id=None):
    """ 
    This is an example of how to use the create_edit_location helper in your
    own view.  Substitute your own template and action to perform once
    the object is saved.
    """
    if location_id:
        location = get_object_or_404(contactinfo.Location, pk=location_id)
    else:
        location = None
    location, saved, context = helpers.create_edit_location(
        request,
        location,
        True,
    )
    if saved:
        if 'next' in request.GET:
            return HttpResponseRedirect(request.GET['next'])
        else:
            edit_url = reverse('edit_location', args=(location.id, ))
            return HttpResponseRedirect(edit_url)
    else:
        return render_to_response('contactinfo/create_edit_location.html',
                                  context,
                                  context_instance=RequestContext(request))
Пример #2
0
def create_edit_business(request, business=None):
    location = None
    if business:
        try:
            location = business.locations.get()
        except contactinfo.Location.DoesNotExist:
            pass
    new_location = not location
    if request.POST:
        business_form = crm_forms.BusinessForm(
            request.POST,
            instance=business,
        )
        location, location_saved, location_context = create_edit_location(
            request,
            location,
            business_form.is_valid(),
        )
        if location_saved:
            new_business = not business
            business = business_form.save()
            if new_location:
                business.locations.add(location)
            if new_business:
                return HttpResponseRedirect(
                    reverse(
                        'view_business',
                        kwargs={
                            'business_id': business.id,
                        },
                    ), )
            else:
                return HttpResponseRedirect(reverse('list_businesses'))
    else:
        business_form = crm_forms.BusinessForm(instance=business)
        location, location_saved, location_context = create_edit_location(
            request,
            location,
            False,
        )

    context = {
        'business': business,
        'business_form': business_form,
    }
    context.update(location_context)
    return context
Пример #3
0
def create_edit_business(request, business=None):
    location = None
    if business:
        try:
            location = business.locations.get()
        except contactinfo.Location.DoesNotExist:
            pass
    new_location = not location
    if request.POST:
        business_form = crm_forms.BusinessForm(
            request.POST,
            instance=business,
        )
        location, location_saved, location_context = create_edit_location(
            request, 
            location,
            business_form.is_valid(),
        )
        if location_saved:
            new_business = not business
            business = business_form.save()
            if new_location:
                business.locations.add(location)
            if new_business:
                return HttpResponseRedirect(
                    reverse(
                        'view_business',
                        kwargs={'business_id': business.id,},
                    ),
                )
            else:
                return HttpResponseRedirect(reverse('list_businesses'))
    else:
        business_form = crm_forms.BusinessForm(instance=business)
        location, location_saved, location_context = create_edit_location(
            request, 
            location,
            False,
        )
    
    context = {
        'business': business,
        'business_form': business_form,
    }
    context.update(location_context)
    return context
Пример #4
0
def create_edit_location(request, location_id=None):
    """ 
    This is an example of how to use the create_edit_location helper in your
    own view.  Substitute your own template and action to perform once
    the object is saved.
    """
    if location_id:
        location = get_object_or_404(contactinfo.Location, pk=location_id)
    else:
        location = None
    location, saved, context = helpers.create_edit_location(request, location, True)
    if saved:
        if "next" in request.GET:
            return HttpResponseRedirect(request.GET["next"])
        else:
            edit_url = reverse("edit_location", args=(location.id,))
            return HttpResponseRedirect(edit_url)
    else:
        return render_to_response(
            "contactinfo/create_edit_location.html", context, context_instance=RequestContext(request)
        )
Пример #5
0
def create_edit_person(request, person_id=None):
    if person_id:
        profile = get_object_or_404(crm.Contact, pk=person_id)
        try:
            location = profile.locations.all()[0]
        except IndexError:
            location = None
    else:
        profile = None
        location = None
    new_location = not location

    if profile and not profile.is_editable_by(request.user):
        return HttpResponseRedirect(reverse('auth_login'))

    if request.POST:
        pre_save = ''
        if profile:
            pre_save = profile.as_text_block()
        profile_form = crm_forms.ProfileForm(
            request.POST,
            instance=profile,
            request=request,
        )
        location, location_saved, location_context = create_edit_location(
            request,
            location,
            profile_form.is_valid(),
        )

        if location_saved:
            # no e-mail will be sent if dict is empty or None
            email = {
                #                'template': 'path/to/email/template.txt',
                #                'subject': 'Welcome!',
                #                'extra_context': { 'somekey': 'someval' },
            }
            saved_profile = profile_form.save()

            if new_location:
                saved_profile.locations.add(location)
            if saved_profile:
                message = 'Person updated successfully'
            else:
                message = 'New person created successfully'
            request.notifications.add(message)
            post_save = saved_profile.as_text_block()

            try:
                group = Group.objects.get(name='Contact Notifications')
            except Group.DoesNotExist:
                group = None
            if group and post_save != pre_save:
                body = "At %s, %s %s changed the profile of %s:\n\n%s" % (
                    datetime.datetime.now(),
                    request.user.first_name,
                    request.user.last_name,
                    saved_profile,
                    ''.join(list(difflib.ndiff(pre_save, post_save))),
                )
                send_mail(
                    'CRM Contact Update: %s' % saved_profile,
                    body,
                    settings.DEFAULT_FROM_EMAIL,
                    [u.email for u in group.user_set.all()],
                )

            if 'associate' in request.REQUEST:
                return HttpResponseRedirect(
                    '%s&associate=true&search_selection=user-%d' % (
                        request.REQUEST['associate'],
                        saved_user.id,
                    ))

            return HttpResponseRedirect(
                reverse(
                    'view_person',
                    kwargs={
                        'person_id': saved_profile.id,
                    },
                ))
    else:
        profile_form = crm_forms.ProfileForm(
            instance=profile,
            request=request,
        )
        location, location_saved, location_context = create_edit_location(
            request,
            location,
            False,
        )
    context = {
        'forms': [profile_form],
        'contact': profile,
    }
    context.update(location_context)
    return context
Пример #6
0
def create_edit_person(request, person_id=None):
    if person_id:
        profile = get_object_or_404(crm.Contact, pk=person_id)
        try:
            location = profile.locations.all()[0]
        except IndexError:
            location = None
    else:
        profile = None
        location = None
    new_location = not location
    
    if profile and not profile.is_editable_by(request.user):
        return HttpResponseRedirect(reverse('auth_login'))
    
    if request.POST:
        pre_save = ''
        if profile:
            pre_save = profile.as_text_block()
        profile_form = crm_forms.ProfileForm(
            request.POST,
            instance=profile,
            request=request,
        )
        location, location_saved, location_context = create_edit_location(
            request, 
            location,
            profile_form.is_valid(),
        )
        
        if location_saved:
            # no e-mail will be sent if dict is empty or None
            email = {
#                'template': 'path/to/email/template.txt',
#                'subject': 'Welcome!',
#                'extra_context': { 'somekey': 'someval' },
            }
            saved_profile = profile_form.save()
            
            if new_location:
                saved_profile.locations.add(location)
            if saved_profile:
                message = 'Person updated successfully'
            else:
                message = 'New person created successfully'
            request.notifications.add(message)
            post_save = saved_profile.as_text_block()
            
            try:
                group = Group.objects.get(name='Contact Notifications')
            except Group.DoesNotExist:
                group = None
            if group and post_save != pre_save:
                body = "At %s, %s %s changed the profile of %s:\n\n%s" % (
                    datetime.datetime.now(),
                    request.user.first_name,
                    request.user.last_name,
                    saved_profile,
                    ''.join(list(difflib.ndiff(pre_save, post_save))),
                )
                send_mail(
                    'CRM Contact Update: %s' % saved_profile,
                    body,
                    settings.DEFAULT_FROM_EMAIL,
                    [u.email for u in group.user_set.all()],
                )
            
            if 'associate' in request.REQUEST:
                return HttpResponseRedirect(
                    '%s&associate=true&search_selection=user-%d' % (
                            request.REQUEST['associate'],
                            saved_user.id,
                        )
                )
            
            return HttpResponseRedirect(
                reverse(
                    'view_person',
                    kwargs={'person_id': saved_profile.id,},
                )
            )
    else:
        profile_form = crm_forms.ProfileForm(
            instance=profile,
            request=request,
        )
        location, location_saved, location_context = create_edit_location(
            request, 
            location,
            False,
        )
    context = {
        'forms': [profile_form],
        'contact': profile,
    }
    context.update(location_context)
    return context