def clean(self):
        """ make sure that both lat and lon are provided. if lat is given then lon is also required and vice versa.  """
        lat=self.cleaned_data['lat']
        lon=self.cleaned_data['lon']
        code=self.cleaned_data['code']
        if not lat and lon:
            msg=u"Please provide the latitude"
            self._errors["lat"]=ErrorList([msg])
            return ''
        elif lat and not lon:
            msg=u"Please provide the longitude"
            self._errors["lon"]=ErrorList([msg])
            return ''
        if lat and lon:
            if not -90 <= lat <= 90 :
                msg=u'Invalid latitude must be between 90 and -90'
                self._errors["lat"]=ErrorList([msg])
                return ''
            if not -180 <= lon <=180 :
                msg=u'Invalid latitude must be between 180 and -180'
                self._errors["lon"]=ErrorList([msg])
                return ''
        if not code:
            try:
                self.cleaned_data['code']=generate_tracking_tag(Area.objects.order_by('-pk')[0].code)
            except:
                self.cleaned_data['code']=generate_tracking_tag()

        else:
            self.cleaned_data['code']='2'+code

        return self.cleaned_data
Example #2
0
 def process_response(self, message):
     db_message = None
     if hasattr(message, 'db_message'):
         db_message = message.db_message
     resp = Response.objects.create(poll=self, message=db_message, contact=db_message.connection.contact, date=db_message.date)
     outgoing_message = self.default_response
     if (self.type == Poll.TYPE_LOCATION):
         location_template = STARTSWITH_PATTERN_TEMPLATE % '[a-zA-Z]*'
         regex = re.compile(location_template)
         if regex.search(message.text):
             spn = regex.search(message.text).span()
             location_str = message.text[spn[0]:spn[1]]
             area = None
             area_names = Area.objects.all().values_list('name', flat=True)
             area_names_lower = [ai.lower() for ai in area_names]
             area_names_matches = difflib.get_close_matches(location_str.lower(), area_names_lower)
             if area_names_matches:
                 area = Area.objects.get(name__iexact=area_names_matches[0])
             else:
                 area = Area.objects.create(name=location_str, code=generate_tracking_tag())
             resp.eav.poll_location_value = area
             
     elif (self.type == Poll.TYPE_NUMERIC):
         try:
             resp.eav.poll_number_value = float(message.text)
         except ValueError:        
             pass
         
     elif ((self.type == Poll.TYPE_TEXT) or (self.type == Poll.TYPE_REGISTRATION)):
         resp.eav.poll_text_value = message.text
         if self.categories:
             for category in self.categories.all():
                 for rule in category.rules.all():
                     regex = re.compile(rule.regex)
                     if regex.search(message.text.lower()):
                         rc = ResponseCategory.objects.create(response = resp, category=category)
                         resp.categories.add(rc)
                         break
     if not resp.categories.all().count() and self.categories.filter(default=True).count():
         resp.categories.add(ResponseCategory.objects.create(response = resp, category=self.categories.get(default=True)))
         
     for respcategory in resp.categories.order_by('category__priority'):
         if respcategory.category.response:
             outgoing_message = respcategory.category.response
             break
     resp.save()
     if not outgoing_message:
         return "We have received your response, thank you!"
     else:
         return outgoing_message