def queue_complaint_update_sms (mobile, complaint_message, complaint):
    if len (complaint_message.strip ()) != 0:
        complaint_message = prepare_sms_message (complaint_message, complaint)
        TextMessage.objects.queue_text_message (mobile, complaint_message)
        debug ("[%s]{%s}" % (complaint.complaintno, str (complaint.curstate))
               + ", Queued message [[%s]] for mobile [%s]" %
               (complaint_message, str (mobile)))
    else:
        debug ("Message empty - so not queueing")
def queue_complaint_ack_official_sms (complaint):
    if (complaint.assignto != None and
        complaint.assignto.user.cmhuser.phone != None and
        len (complaint.assignto.user.cmhuser.phone) != 0 and
        complaint.complainttype != None):
        official_message = "%s : %s : %s : %s/%s/%s : %s" % (complaint.complaintno,
                                                             complaint.filedby.name,
                                                             complaint.filedby.mobile,
                                                             complaint.location.name,
                                                             complaint.location.grampanchayat.name,
                                                             complaint.location.grampanchayat.block.name,
                                                             complaint.complainttype.summary)
        # made the official_message use smart_str since the message contains unicode
	# and encoding was not happening properly
        queue_sms (complaint.assignto.user.cmhuser.phone, smart_str(official_message))
    else:
        debug ("!! ERROR : Queuing message to official. Complaint no: %s, Assigned to: %s" %
               (complaint.complaintno, str (complaint.assignto)))
def close_resolved():
    now_day = datetime.today().date()
    seven_ago = now_day - timedelta(days=7)

    resolved = Complaint.objects.filter(latest=True, curstate=STATUS_RESOLVED, createdate__lte=seven_ago)

    for oldver in resolved:
        debug("Closing through time lapse : " + oldver.complaintno)
        newver = oldver.clone(AnonymousUser())
        newver.curstate = STATUS_CLOSED
        newver.save()

        if newver.complainttype.defsmsclo != None:
            queue_complaint_update_sms(newver.filedby.mobile, newver.complainttype.defsmsclo, newver)
        else:
            debug(
                "[%s]{%s}: " % (newver.complaintno, str(newver.curstate))
                + "Message is empty -- not queueing >> from forms.py: issuemgr"
            )
def department_selected (request):
    form = DepartmentSelected (request.GET)
    if form.is_valid ():
        debug (form)
        try:
            dept_id = form.cleaned_data ['department']
            supervisors = Official.objects.filter (department__id = dept_id,
                                                   supervisor = None)
            sups = [(supervisor.id, supervisor.user.cmhuser.get_desc_name ())
                    for supervisor in supervisors]
            sups.insert (0, ('-1', '----'))
            serialized = json.dumps (sups)
            request.session ['pos_supervisors'] = serialized
            return HttpResponse (serialized)
        except:
            import traceback
            traceback.print_exc ()
            return HttpResponse ([])
    else:
        return HttpResponse ([])
    def save (self, user):
        complaint = Complaint.objects.get (complaintno = self.cleaned_data ['complaintno'], latest = True)

        newver = complaint.clone (user)

        newver.curstate = ComplaintStatus.objects.get (id = self.cleaned_data ['newstatus'])
        newver.description = self.cleaned_data ['comment']

        if self.cleaned_data ['revlocationid'] != None:
            newver.location = Village.objects.get (id = self.cleaned_data ['revlocationid'])

        if self.cleaned_data ['revcategoryid'] != None:
            assignto = None
            complaint_base = ComplaintType.objects.get (id = self.cleaned_data ['revcategoryid'])
            department = complaint_base.department
            officials = department.official_set.all ()
            if officials.count () > 0:
                assignto = officials [0]
            else:
                assignto = None

            newver.complainttype = ComplaintType.objects.get (id = self.cleaned_data ['revcategoryid'])
            newver.department = newver.complainttype.department
            newver.assignto = assignto
            newver.gender = self.cleaned_data ['gender']
            newver.community = self.cleaned_data ['community']
        newver.save ()

        # Update the "original" issue if the new status is ACK state since the
        # issue has been made actionable
        if complaint.curstate == STATUS_NEW and newver.curstate == STATUS_ACK:
            original = newver.original
            original.complainttype = newver.complainttype
            original.location = newver.location
            original.department = newver.complainttype.department
            original.assignto = newver.assignto
            original.gender = newver.gender
            original.community = newver.community
            original.save ()

        if newver.curstate == STATUS_CLOSED:
            now_time = datetime.now ()
            ccms = ComplaintClosureMetric.objects.filter (complaintno = newver.complaintno)
            origs = Complaint.objects.filter (complaintno = newver.complaintno, curstate = STATUS_NEW).order_by ('created')
            if ccms.count () == 1:
                if origs.count () == 1:
                    orig = origs [0]
                    ccm = ccms [0]
                    ccm.closed = now_time
                    period = now_time - orig.created
                    ccm.period = period.days + ((period.seconds * 1.0) / (3600 * 24))
                    ccm.save ()
                else:
                    debug ("Multiple complaint objects with status_new for complaint : " + newver.complaintno)
                    debug ("... Doing nothing")
            else:
                debug ("Multiple complaint closure metrics exist for this complaint : " + newver.complaintno)
                debug ("... Doing nothing")


        if newver.curstate == STATUS_ACK:
            message = newver.complainttype.defsmsack
        elif newver.curstate == STATUS_OPEN:
            message = newver.complainttype.defsmsopen
        elif newver.curstate == STATUS_RESOLVED:
            message = newver.complainttype.defsmsres
        elif newver.curstate == STATUS_CLOSED:
            message = newver.complainttype.defsmsclo
        elif newver.curstate == STATUS_REOPEN:
            message = newver.complainttype.defsmsnew
        else:
            message = None

        if message != None:
            queue_complaint_update_sms (newver.filedby.mobile, message, newver)
        else:
            debug ("[%s]{%s}: " % (newver.complaintno, str (newver.curstate)) +
                   "Message is empty -- not queueing >> from forms.py: issuemgr")

        if (newver.curstate == STATUS_ACK):
            queue_complaint_ack_official_sms (newver)

        return newver
def process_dm (request):
    cmhuser_dm = CmhUser.objects.filter (user__approle__role = UserRoles.DM)
    if request.method == 'GET':
        if cmhuser_dm.count () == 0:
            return render_to_response ('create_dm.html',
                                       {'menus' : get_user_menus (request.user,process_dm),
                                        'user' : request.user,
                                        'form' : RegisterDM ()})
        elif cmhuser_dm.count () == 1:
            return render_to_response ('view_dm.html',
                                       {'menus' : get_user_menus (request.user,process_dm),
                                        'user' : request.user,
                                        'dm' : cmhuser_dm [0]})
        else:
            return render_to_response ('error_dm_count.html',
                                       {'menus' : get_user_menus (request.user,process_dm),
                                        'user' : request.user})
    elif request.method == 'POST':
        if 'register' in request.POST: # Registering a new DM
            form = RegisterDM (request.POST)
            if form.is_valid ():
                dm = form.save ()
                return render_to_response ('view_dm.html',
                                           {'menus' : get_user_menus (request.user,process_dm),
                                            'user' : request.user,
                                            'dm' : dm})
            else:
                return render_to_response ('create_dm.html',
                                           {'menus' : get_user_menus (request.user,process_dm),
                                            'user' : request.user,
                                            'form' : form})
        elif 'cancel' in request.POST: # Cancel DM registration
            return HttpResponseRedirect (reverse (masters))
        elif 'edit_dm' in request.POST: # Edit DM record
            form = DmId (request.POST)
            if form.is_valid ():
                dmid = form.cleaned_data ['dmid']
                try:
                    dm = CmhUser.objects.get (user__approle__role = UserRoles.DM,
                                              id = dmid)
                    formdata = {'dmid' : dm.id,
                                'username' : dm.user.username,
                                'name' : dm.user.get_full_name (),
                                'phone' : dm.phone}
                    form = EditDM (formdata)
                    return render_to_response ('edit_dm.html',
                                               {'menus':get_user_menus(request.user,process_dm),
                                                'user' : request.user,
                                                'form' : form})
                except CmhUser.DoesNotExist, CmhUser.MultipleObjectsReturned:
                    return HttpResponse (reverse (masters))
        elif 'edit_save' in request.POST:
            debug ("Saving a DM object")
            form = EditDM (request.POST)
            if form.is_valid ():
                form.save ()
                return HttpResponseRedirect (reverse (process_dm))
            else:
                debug ("Invalid form:" , form.errors)
                return render_to_response ('edit_dm.html',
                                           {'menus': get_user_menus(request.user,process_dm),
                                            'user' : request.user,
                                            'form' : form})
        elif 'edit_cancel' in request.POST:
            return HttpResponseRedirect (reverse (process_dm))
        else:
            return HttpResponseRedirect (reverse (masters))
Esempio n. 7
0
def gotomyprofile (request):
    if request.method=="GET":
        cmhuser = request.user.cmhuser
        return render_to_response('profile.html',
                                {'obj' : cmhuser,
                                 'menus' : get_user_menus (request.user,gotomyprofile),
                                 'user' : request.user})
    elif request.method=="POST":
        if 'edit' in request.POST:
            cmhuser = request.user.cmhuser
            return render_to_response('edit.html',
                                      {'menus'  : get_user_menus (request.user,gotomyprofile),
                                       'user'   : request.user,
                                       'form'   : ProfileEditForm (),
                                       'obj'    : cmhuser})
        elif 'savechanges' in request.POST:
            form = ProfileEditForm (request.POST)
            if form.is_valid ():
                user    = request.user
                cmhuser = user.cmhuser

                user.first_name = request.POST ['name']
                user.save ()

                cmhuser.phone = request.POST['phone']
                cmhuser.save()

                return HttpResponseRedirect (reverse (gotomyprofile))
            else:
                cmhuser = request.user.cmhuser
                return render_to_response('edit.html',
                                          {'obj'   : form,
                                           'menus' : get_user_menus (request.user,gotomyprofile),
                                           'user'  : request.user})

        elif 'reset' in request.POST:
            return render_to_response ('reset_password.html',
                                       {'form' : PasswordUpdateForm (request.user),
                                        'menus': get_user_menus (request.user,gotomyprofile),
                                        'user'  : request.user}
                                        )
        elif 'set_password' in request.POST:
            try:
                form = PasswordUpdateForm (request.user,
                                           request.POST)
                if form.is_valid ():
                    form.save ()
                    return render_to_response ('password_reset_success.html',
                                               {'menus': get_user_menus (request.user,gotomyprofile),
                                                'user'  : request.user})

                else:
                    debug ("Form is not valid" + str (form.errors))
                    return render_to_response ('reset_password.html',
                                               {'form' : form,
                                                'menus': get_user_menus (request.user,gotomyprofile),
                                                'user'  : request.user})
            except:
                import traceback
                traceback.print_exc ()
        elif 'cancel' in request.POST:
            return HttpResponseRedirect (reverse (gotomyprofile))