def _get_all_datasenders(self, manager, projects, user):
     all_data_senders, fields, labels = import_module.load_all_entities_of_type(manager)
     project_association = self._get_project_association(projects)
     remove_system_datasenders(all_data_senders)
     for datasender in all_data_senders:
         get_datasender_user_detail(datasender, user)
         datasender['projects'] = project_association.get(datasender['short_code'])
     return all_data_senders
 def _subject_choice_fields(self, entity_type, subject_field):
     subjects, fields, label = load_all_entities_of_type(self.dbm, type=entity_type)
     subjects = self._build_subject_choice_data(subjects, fields)
     language = get_text_language_by_instruction(subject_field.instruction)
     instruction_for_subject_field = translate("Choose Subject from this list.", language=language, func=ugettext)
     all_subject_choices = map(self.choice, subjects)
     choice_fields = self._get_choice_field(all_subject_choices, subject_field,
                                            help_text=instruction_for_subject_field)
     return choice_fields
Example #3
0
def broadcast_message(request, project_id):
    dbm = get_database_manager(request.user)
    project = Project.load(dbm.database, project_id)
    number_associated_ds = len(project.data_senders)
    number_of_ds = len(import_module.load_all_entities_of_type(dbm, type=REPORTER)[0]) - 1
    questionnaire = FormModel.get(dbm, project.qid)
    organization = utils.get_organization(request)
    if request.method == 'GET':
        form = BroadcastMessageForm(associated_ds=number_associated_ds, number_of_ds=number_of_ds)
        html = 'project/broadcast_message_trial.html' if organization.in_trial_mode else 'project/broadcast_message.html'
        return render_to_response(html, {'project': project,
                                         "project_links": make_project_links(project, questionnaire.form_code),
                                         'is_quota_reached': is_quota_reached(request, organization=organization),
                                         "form": form, "ong_country": organization.country,
                                         "success": None},
                                  context_instance=RequestContext(request))
    if request.method == 'POST':
        form = BroadcastMessageForm(associated_ds=number_associated_ds, number_of_ds=number_of_ds, data=request.POST)
        if form.is_valid():
            no_smsc = False
            data_senders = _get_data_senders(dbm, form, project)
            organization_setting = OrganizationSetting.objects.get(organization=organization)
            current_month = datetime.date(datetime.datetime.now().year, datetime.datetime.now().month, 1)
            message_tracker = organization._get_message_tracker(current_month)
            other_numbers = form.cleaned_data['others']
            failed_numbers = []
            try:
                failed_numbers = helper.broadcast_message(data_senders, form.cleaned_data['text'],
                                                          organization_setting.get_organisation_sms_number()[0],
                                                          other_numbers,
                                                          message_tracker,
                                                          country_code=organization.get_phone_country_code())
            except NoSMSCException as e:
                no_smsc = True
            success = not no_smsc and len(failed_numbers) == 0

            if success:
                form = BroadcastMessageForm(associated_ds=number_associated_ds, number_of_ds=number_of_ds)
            else:
                form = BroadcastMessageForm(associated_ds=number_associated_ds, number_of_ds=number_of_ds,
                                            data=request.POST)
            return render_to_response('project/broadcast_message.html',
                                      {'project': project,
                                       "project_links": make_project_links(project, questionnaire.form_code),
                                       'is_quota_reached': is_quota_reached(request, organization=organization),
                                       "form": form,
                                       "ong_country": organization.country, "no_smsc": no_smsc,
                                       'failed_numbers': ",".join(failed_numbers), "success": success},
                                      context_instance=RequestContext(request))

        return render_to_response('project/broadcast_message.html',
                                  {'project': project,
                                   "project_links": make_project_links(project, questionnaire.form_code), "form": form,
                                   'is_quota_reached': is_quota_reached(request, organization=organization),
                                   'success': None, "ong_country": organization.country},
                                  context_instance=RequestContext(request))
Example #4
0
def _get_all_data_senders(dbm):
    data_senders, fields, labels = load_all_entities_of_type(dbm)
    return [dict(zip(fields, data["cols"])) for data in data_senders]