def import_file_contacts(request, form_class=ImportContactForm, template_name='friends/upload_contacts.html', redirect_to="invite_imported"): redirect_to=request.REQUEST.get(REDIRECT_FIELD_NAME, redirect_to) if redirect_to and '/' not in redirect_to: redirect_to=reverse(redirect_to) if request.method == 'POST': contacts_file_form=form_class(request.POST,request.FILES) if contacts_file_form.is_valid(): friends_file=request.FILES['contacts_file'] if friends_file.multiple_chunks(): messages.add_message(request, messages.ERROR,"The file you uploaded is too large.") else: start = True format = None contact_file_content = "" for chunk in friends_file.chunks(): if start == True: if 'VCARD' in chunk: format = 'VCARD' else: ARBITRARY_FIELD_MINIMUM=5 first_line = chunk.split('\n')[0] if len(first_line.split(',')) > ARBITRARY_FIELD_MINIMUM or len(first_line.split('\t')) > ARBITRARY_FIELD_MINIMUM: format = 'OUTLOOK' contact_file_content += chunk start = False if format == 'VCARD': imported, total = import_vcards(contact_file_content, request.user) elif format == 'OUTLOOK': imported, total = import_outlook(contact_file_content, request.user) if format: if imported < total: if imported: messages.add_message(request, messages.SUCCESS,'A total of %d emails imported. %d records were already imported.' % (imported, total-imported)) else: messages.add_message(request, messages.SUCCESS,'A total of %d emails were found, but they were already imported.' % (total)) else: messages.add_message(request, messages.SUCCESS,'A total of %d emails imported vs %d.' % (imported,total)) return {'imported':imported, 'total':total}, {'url': redirect_to} else: messages.add_message(request, messages.ERROR,'The file format you uploaded wasn\'t valid.') else: contacts_file_form=form_class() return locals(), template_name
def save(self, user): #imported, total = import_vcards(self.cleaned_data["vcard_file"].content, user) imported, total = import_vcards(self.cleaned_data["vcard_file"].read(), user) return imported, total
def save(self, user): imported, total = import_vcards( self.cleaned_data["vcard_file"].content, user) return imported, total