Пример #1
0
def membership_import_confirm(request, id):
    """
    Confirm the membership import and continue with the process.
    This can only be accessed via a hidden post form from the preview page.
    That will hold the original mappings selected by the user.
    """
    if not is_admin(request.user):
        raise Http403
    
    memport = get_object_or_404(MembershipImport, pk=id)
    
    if request.method == "POST":
        form = ImportMapForm(request.POST, memport=memport)
        
        if form.is_valid():
            cleaned_data = form.cleaned_data
            app = memport.app
            file_path = os.path.join(settings.MEDIA_ROOT, memport.get_file().file.name)
            
            if not settings.CELERY_IS_ACTIVE:
                # if celery server is not present 
                # evaluate the result and render the results page
                result = ImportMembershipsTask()
                memberships, stats = result.run(app, file_path, cleaned_data)
                return render_to_response('memberships/import-confirm.html', {
                    'memberships': memberships,
                    'stats': stats,
                    'datetime': datetime,
                }, context_instance=RequestContext(request))
            else:
                result = ImportMembershipsTask.delay(app, file_path, cleaned_data)
            
            return redirect('membership_import_status', result.task_id)
    else:
        return redirect('membership_import_preview', memport.id)
Пример #2
0
def membership_import_preview(request, id):
    """
    This will generate a form based on the uploaded CSV for field mapping.
    A preview will be generated based on the mapping given.
    """
    if not is_admin(request.user):
        raise Http403
    
    memport = get_object_or_404(MembershipImport, pk=id)
    
    if request.method == 'POST':
        form = ImportMapForm(request.POST, memport=memport)
        
        if form.is_valid():
            #show the user a preview based on the mapping
            cleaned_data = form.cleaned_data
            app = memport.app

            file_path = os.path.join(settings.MEDIA_ROOT, memport.get_file().file.name)
            memberships, stats = parse_mems_from_csv(file_path, cleaned_data)
            
            # return the form to use it for the confirm view
            template_name = 'memberships/import-preview.html'
            return render_to_response(template_name, {
                'memberships':memberships,
                'stats':stats,
                'memport':memport,
                'form':form,
                'datetime': datetime,
            }, context_instance=RequestContext(request))

    else:
        form = ImportMapForm(memport=memport)
    
    template_name = 'memberships/import-map-fields.html'
    return render_to_response(template_name, {
        'form':form,
        'memport':memport,
        'datetime':datetime,
        }, context_instance=RequestContext(request))