def _smart_group(request, contact_store, group): if request.method == 'POST': if '_save_group' in request.POST: smart_group_form = SmartGroupForm(request.POST) if smart_group_form.is_valid(): group.name = smart_group_form.cleaned_data['name'] group.query = smart_group_form.cleaned_data['query'] group.save() return redirect(_group_url(group.key)) elif '_export' in request.POST: tasks.export_group_contacts.delay( request.user_api.user_account_key, group.key, True) messages.info( request, 'The export is scheduled and should ' 'complete within a few minutes.') return redirect(_group_url(group.key)) elif '_delete_group_contacts' in request.POST: tasks.delete_group_contacts.delay( request.user_api.user_account_key, group.key) messages.info(request, "The group's contacts will be deleted shortly.") return redirect(_group_url(group.key)) elif '_delete_group' in request.POST: tasks.delete_group.delay(request.user_api.user_account_key, group.key) messages.info(request, 'The group will be deleted shortly.') return redirect(reverse('contacts:index')) else: # We didn't get any useful POST variables, so just redirect back to # the group page without doing anything. return redirect(_group_url(group.key)) else: smart_group_form = SmartGroupForm({ 'name': group.name, 'query': group.query, }) keys = contact_store.get_contacts_for_group(group) limit = min(int(request.GET.get('limit', 100)), len(keys)) if keys: messages.info( request, "Showing %s of the group's %s contact(s)" % (limit, len(keys))) contacts = utils.contacts_by_key(contact_store, *keys[:limit]) return render( request, 'contacts/smart_group_detail.html', { 'group': group, 'selected_contacts': contacts, 'group_form': smart_group_form, })
def _smart_group(request, contact_store, group): if request.method == 'POST': if '_save_group' in request.POST: smart_group_form = SmartGroupForm(request.POST) if smart_group_form.is_valid(): group.name = smart_group_form.cleaned_data['name'] group.query = smart_group_form.cleaned_data['query'] group.save() return redirect(_group_url(group.key)) elif '_export' in request.POST: tasks.export_group_contacts.delay( request.user_api.user_account_key, group.key, True) messages.info(request, 'The export is scheduled and should ' 'complete within a few minutes.') return redirect(_group_url(group.key)) elif '_delete_group_contacts' in request.POST: tasks.delete_group_contacts.delay( request.user_api.user_account_key, group.key) messages.info( request, "The group's contacts will be deleted shortly.") return redirect(_group_url(group.key)) elif '_delete_group' in request.POST: tasks.delete_group.delay(request.user_api.user_account_key, group.key) messages.info(request, 'The group will be deleted shortly.') return redirect(reverse('contacts:index')) else: # We didn't get any useful POST variables, so just redirect back to # the group page without doing anything. return redirect(_group_url(group.key)) else: smart_group_form = SmartGroupForm({ 'name': group.name, 'query': group.query, }) keys = contact_store.get_contacts_for_group(group) limit = min(int(request.GET.get('limit', 100)), len(keys)) if keys: messages.info( request, "Showing %s of the group's %s contact(s)" % (limit, len(keys))) contacts = utils.contacts_by_key(contact_store, *keys[:limit]) return render(request, 'contacts/smart_group_detail.html', { 'group': group, 'selected_contacts': contacts, 'group_form': smart_group_form, })
def groups(request, type=None): contact_store = request.user_api.contact_store if request.POST: contact_group_form = ContactGroupForm(request.POST) smart_group_form = SmartGroupForm(request.POST) if '_new_group' in request.POST: if contact_group_form.is_valid(): group = contact_store.new_group( contact_group_form.cleaned_data['name']) messages.add_message( request, messages.INFO, 'New group created') return redirect(_group_url(group.key)) elif '_new_smart_group' in request.POST: if smart_group_form.is_valid(): name = smart_group_form.cleaned_data['name'] query = smart_group_form.cleaned_data['query'] smart_group = contact_store.new_smart_group( name, query) return redirect(_group_url(smart_group.key)) elif '_delete' in request.POST: groups = request.POST.getlist('group') for group_key in groups: group = contact_store.get_group(group_key) tasks.delete_group.delay(request.user_api.user_account_key, group.key) messages.info(request, '%d groups will be deleted ' 'shortly.' % len(groups)) return redirect(reverse('contacts:groups')) elif '_export' in request.POST: tasks.export_many_group_contacts.delay( request.user_api.user_account_key, request.POST.getlist('group')) messages.info(request, 'The export is scheduled and should ' 'complete within a few minutes.') return redirect(reverse('contacts:groups')) else: contact_group_form = ContactGroupForm() smart_group_form = SmartGroupForm() user_query = request.GET.get('q', '') query = user_query if query: if ':' not in query: query = 'name:%s' % (query,) keys = contact_store.groups.raw_search(query).get_keys() groups = utils.groups_by_key(contact_store, *keys) else: if type == 'static': groups = contact_store.list_static_groups() elif type == 'smart': groups = contact_store.list_smart_groups() else: groups = contact_store.list_groups() groups = sorted(groups, key=lambda group: group.created_at, reverse=True) paginator = Paginator(groups, 15) try: page = paginator.page(request.GET.get('p', 1)) except PageNotAnInteger: page = paginator.page(1) except EmptyPage: page = paginator.page(paginator.num_pages) pagination_params = urlencode({ 'query': query, }) return render(request, 'contacts/group_list.html', { 'paginator': paginator, 'pagination_params': pagination_params, 'page': page, 'query': user_query, 'contact_group_form': contact_group_form, })
def groups(request, type=None): contact_store = request.user_api.contact_store if request.POST: contact_group_form = ContactGroupForm(request.POST) smart_group_form = SmartGroupForm(request.POST) if '_new_group' in request.POST: if contact_group_form.is_valid(): group = contact_store.new_group( contact_group_form.cleaned_data['name']) messages.add_message(request, messages.INFO, 'New group created') return redirect(_group_url(group.key)) elif '_new_smart_group' in request.POST: if smart_group_form.is_valid(): name = smart_group_form.cleaned_data['name'] query = smart_group_form.cleaned_data['query'] smart_group = contact_store.new_smart_group(name, query) return redirect(_group_url(smart_group.key)) elif '_delete' in request.POST: groups = request.POST.getlist('group') for group_key in groups: group = contact_store.get_group(group_key) tasks.delete_group.delay(request.user_api.user_account_key, group.key) messages.info( request, '%d groups will be deleted ' 'shortly.' % len(groups)) return redirect(reverse('contacts:groups')) elif '_export' in request.POST: tasks.export_many_group_contacts.delay( request.user_api.user_account_key, request.POST.getlist('group')) messages.info( request, 'The export is scheduled and should ' 'complete within a few minutes.') return redirect(reverse('contacts:groups')) else: contact_group_form = ContactGroupForm() smart_group_form = SmartGroupForm() user_query = request.GET.get('q', '') query = user_query if query: if ':' not in query: query = 'name:%s' % (query, ) keys = contact_store.groups.raw_search(query).get_keys() groups = utils.groups_by_key(contact_store, *keys) else: if type == 'static': groups = contact_store.list_static_groups() elif type == 'smart': groups = contact_store.list_smart_groups() else: groups = contact_store.list_groups() groups = sorted(groups, key=lambda group: group.created_at, reverse=True) paginator = Paginator(groups, 15) try: page = paginator.page(request.GET.get('p', 1)) except PageNotAnInteger: page = paginator.page(1) except EmptyPage: page = paginator.page(paginator.num_pages) pagination_params = urlencode({ 'query': query, }) return render( request, 'contacts/group_list.html', { 'paginator': paginator, 'pagination_params': pagination_params, 'page': page, 'query': user_query, 'contact_group_form': contact_group_form, })
def _people(request): contact_store = request.user_api.contact_store upload_contacts_form = None group = None if request.method == 'POST': if '_delete' in request.POST: contacts = request.POST.getlist('contact') for person_key in contacts: contact = contact_store.get_contact_by_key(person_key) contact.delete() messages.info(request, '%d Contacts deleted' % len(contacts)) elif '_export' in request.POST: tasks.export_contacts.delay(request.user_api.user_account_key, request.POST.getlist('contact')) messages.info( request, 'The export is scheduled and should ' 'complete within a few minutes.') elif '_export_all' in request.POST: tasks.export_all_contacts.delay(request.user_api.user_account_key) messages.info( request, 'The export is scheduled and should ' 'complete within a few minutes.') else: # first parse the CSV file and create Contact instances # from them for attaching to a group later upload_contacts_form = UploadContactsForm(request.POST, request.FILES) if upload_contacts_form.is_valid(): # We could be creating a new contact group. if request.POST.get('name'): new_group_form = ContactGroupForm(request.POST) if new_group_form.is_valid(): group = contact_store.new_group( new_group_form.cleaned_data['name']) # We could be using an existing contact group. elif request.POST.get('contact_group'): select_group_form = SelectContactGroupForm( request.POST, groups=contact_store.list_groups()) if select_group_form.is_valid(): group = contact_store.get_group( select_group_form.cleaned_data['contact_group']) if group is None: messages.error( request, 'Please select a group or provide ' 'a new group name.') else: file_object = upload_contacts_form.cleaned_data['file'] file_name, file_path = utils.store_temporarily(file_object) utils.store_file_hints_in_session(request, file_name, file_path) return redirect(_group_url(group.key)) else: messages.error(request, 'Something went wrong with the upload.') select_contact_group_form = SelectContactGroupForm( groups=contact_store.list_groups()) # TODO: A lot of this stuff is duplicated from the similar group search # in the groups() view. We need a function that does that to avoid # the duplication. user_query = request.GET.get('q', '') query = user_query if query: if not ':' in query: query = 'name:%s' % (query, ) keys = contact_store.contacts.raw_search(query).get_keys() else: keys = contact_store.list_contacts() limit = min(int(request.GET.get('limit', 100)), len(keys)) messages.info(request, "Showing %s of %s contact(s)" % (limit, len(keys))) smart_group_form = SmartGroupForm(initial={'query': query}) contacts = utils.contacts_by_key(contact_store, *keys[:limit]) return render( request, 'contacts/contact_list.html', { 'query': user_query, 'selected_contacts': contacts, 'smart_group_form': smart_group_form, 'upload_contacts_form': upload_contacts_form or UploadContactsForm(), 'select_contact_group_form': select_contact_group_form, })