# Path: sendinel/backend/authhelper.py
# def format_and_validate_phonenumber(number):
#     """
#     Replaces all number specific characters like
#     "+", "-" and "/" and checks that there are no
#     letters included. Checks also if the number is 
#     the number of a mobile phone.
#     
#     @param  number:     The phone number that will be checked
#     @type   number:     string
#     
#     @raise  ValueError
#     """
#    
#     if number.startswith('+'):
#         number = number.replace('+', '00', 1)
#     
#     regex = re.compile('(\/|\+|-| )')
#     number = regex.sub('', number)
#     
#     if number.startswith(COUNTRY_CODE_PHONE):
#         number = number.replace(COUNTRY_CODE_PHONE, '0', 1)
# 
#     # if the conversion to int does not fail then
#     # there are only numbers included in the string
#     try:
#         int(number)
#     except ValueError:
#         raise ValidationError(_('Please enter numbers only.'))
#     
#     if number.startswith(START_MOBILE_PHONE):
#         return number
#     else:
#         raise ValidationError(_('Please enter a cell phone number.'))
# 
# Path: sendinel/backend/models.py
# def get_enabled_wocs():
#     """
#     Return all Ways of Communication that are enabled
#     """
#     return WayOfCommunication.objects.all().filter(enabled = True)
# 
# Path: sendinel/infoservices/models.py
# class InfoMessage(Sendable):
#     """
#     Define a InfoMessage.
#     """
# 
#     template = Template("$text")
#     text = models.TextField()
#     
#     def __unicode__(self):
#         return "InfoMessage to %s: '%s' via %s" % \
#                                         (self.recipient.phone_number,
#                                          self.text,
#                                          self.way_of_communication)
#     
#     def get_data_for_sms(self):
#         """
#         Prepare OutputData for sms.
#         Generate the message for an InfoMessage.
#         Return SMSOutputData for sending.
#         """
#         
#         data = SMSOutputData()           
#         data.data = texthelper.generate_text({'text': self.text},
#                                              InfoMessage.template)
#         data.phone_number = self.recipient.phone_number
#                 
#         return data
#         
#     def get_data_for_voice(self):
#         """
#         Prepare OutputData for voicecall.
#         Generate the message for an InfoMessage.
#         Return VoiceOutputData for sending.
#         """
#         
#         data = VoiceOutputData()
#         data.data = self.text
#         data.phone_number = self.recipient.phone_number
#             
#         return data
# 
# class InfoService(models.Model):
#     """
#     Represent a user group.
#     Existing types are 'information' and 'medicine' 
#     whereas information-groups send regular messages to their 
#     subscribers, medicine deletes the group after sending a message
#     """
#     
#     TYPES = (
#         ('information', ugettext_lazy("Information Group")),
#         ('medicine', ugettext_lazy('Waiting List for Medicine'))
#     )
#     TYPE_TEXTS = {
#         'information': {
#             'title': ugettext_lazy('Information Groups'),
#             'name': ugettext_lazy('information group'),
#             'members_button': ugettext_lazy('Group members'),
#             'remove_button': ugettext_lazy('Remove group'),
#             'remove_patient_button': ugettext_lazy('Remove patient from group'),
#             'form_field_description': ugettext_lazy('Inform patients about:'),
#             'table_head': ugettext_lazy('Information')
#         },
#         'medicine': {
#             'title': ugettext_lazy('Waiting Lists for Medicine'),
#             'name': ugettext_lazy('waiting list for medicine'),
#             'members_button': ugettext_lazy('List members'),
#             'remove_button': ugettext_lazy('Remove list'),
#             'remove_patient_button': ugettext_lazy('Remove patient from list'),
#             'form_field_description':
#             ugettext_lazy('The patients are waiting for the following medicine:'),
#             'table_head': ugettext_lazy('Medicine')
#         },
#     }
#     
#     
#     members = models.ManyToManyField(Patient, through = "Subscription")
#     name = models.CharField(max_length = 255,
#                             default = None,
#                             unique = True,
#                             blank = False,
#                             null = False,
#                             verbose_name = ugettext_lazy("name"))
#     type = models.CharField(max_length = 255, 
#                             choices = TYPES,
#                             default = None,
#                             blank = False, 
#                             null = False)
#     
#     class Meta:
#         verbose_name = ugettext_lazy("Information service")
#                             
#     def __unicode__(self):
#         return self.name
#     
#     def member_count(self):
#         return self.members.all().count()
#     
#     def texts(self):
#         """
#         Returns the text blocks associated with the group's type.
#         """
#         return self.TYPE_TEXTS[self.type]


from django.forms import CharField, Form, ModelChoiceField
from django.utils.translation import ugettext_lazy as _
from sendinel.backend.authhelper import format_and_validate_phonenumber
from sendinel.backend.models import get_enabled_wocs
from sendinel.infoservices.models import InfoMessage, InfoService



class RegisterPatientForMedicineForm(Form):  
    phone_number = CharField(validators = [format_and_validate_phonenumber],
            error_messages={'required':_('Please enter a phone number')})
    way_of_communication = ModelChoiceField(
                        queryset = get_enabled_wocs(),
                        error_messages={'required': \
                                _('Please choose a way of communication')})     
    medicine = ModelChoiceField(
            queryset=InfoService.objects.filter(type='medicine'),
            error_messages={'required': \
                                _('Please choose a medicine'),
                            'invalid_choice':
                                _('Please choose a medicine')})

class MedicineMessageValidationForm(Form):
    medicine = ModelChoiceField(

# Path: sendinel/backend/authhelper.py
# def format_and_validate_phonenumber(number):
#     """
#     Replaces all number specific characters like
#     "+", "-" and "/" and checks that there are no
#     letters included. Checks also if the number is 
#     the number of a mobile phone.
#     
#     @param  number:     The phone number that will be checked
#     @type   number:     string
#     
#     @raise  ValueError
#     """
#    
#     if number.startswith('+'):
#         number = number.replace('+', '00', 1)
#     
#     regex = re.compile('(\/|\+|-| )')
#     number = regex.sub('', number)
#     
#     if number.startswith(COUNTRY_CODE_PHONE):
#         number = number.replace(COUNTRY_CODE_PHONE, '0', 1)
# 
#     # if the conversion to int does not fail then
#     # there are only numbers included in the string
#     try:
#         int(number)
#     except ValueError:
#         raise ValidationError(_('Please enter numbers only.'))
#     
#     if number.startswith(START_MOBILE_PHONE):
#         return number
#     else:
#         raise ValidationError(_('Please enter a cell phone number.'))
# 
# Path: sendinel/backend/models.py
# def get_enabled_wocs():
#     """
#     Return all Ways of Communication that are enabled
#     """
#     return WayOfCommunication.objects.all().filter(enabled = True)
# 
# Path: sendinel/infoservices/models.py
# class InfoMessage(Sendable):
#     """
#     Define a InfoMessage.
#     """
# 
#     template = Template("$text")
#     text = models.TextField()
#     
#     def __unicode__(self):
#         return "InfoMessage to %s: '%s' via %s" % \
#                                         (self.recipient.phone_number,
#                                          self.text,
#                                          self.way_of_communication)
#     
#     def get_data_for_sms(self):
#         """
#         Prepare OutputData for sms.
#         Generate the message for an InfoMessage.
#         Return SMSOutputData for sending.
#         """
#         
#         data = SMSOutputData()           
#         data.data = texthelper.generate_text({'text': self.text},
#                                              InfoMessage.template)
#         data.phone_number = self.recipient.phone_number
#                 
#         return data
#         
#     def get_data_for_voice(self):
#         """
#         Prepare OutputData for voicecall.
#         Generate the message for an InfoMessage.
#         Return VoiceOutputData for sending.
#         """
#         
#         data = VoiceOutputData()
#         data.data = self.text
#         data.phone_number = self.recipient.phone_number
#             
#         return data
# 
# class InfoService(models.Model):
#     """
#     Represent a user group.
#     Existing types are 'information' and 'medicine' 
#     whereas information-groups send regular messages to their 
#     subscribers, medicine deletes the group after sending a message
#     """
#     
#     TYPES = (
#         ('information', ugettext_lazy("Information Group")),
#         ('medicine', ugettext_lazy('Waiting List for Medicine'))
#     )
#     TYPE_TEXTS = {
#         'information': {
#             'title': ugettext_lazy('Information Groups'),
#             'name': ugettext_lazy('information group'),
#             'members_button': ugettext_lazy('Group members'),
#             'remove_button': ugettext_lazy('Remove group'),
#             'remove_patient_button': ugettext_lazy('Remove patient from group'),
#             'form_field_description': ugettext_lazy('Inform patients about:'),
#             'table_head': ugettext_lazy('Information')
#         },
#         'medicine': {
#             'title': ugettext_lazy('Waiting Lists for Medicine'),
#             'name': ugettext_lazy('waiting list for medicine'),
#             'members_button': ugettext_lazy('List members'),
#             'remove_button': ugettext_lazy('Remove list'),
#             'remove_patient_button': ugettext_lazy('Remove patient from list'),
#             'form_field_description':
#             ugettext_lazy('The patients are waiting for the following medicine:'),
#             'table_head': ugettext_lazy('Medicine')
#         },
#     }
#     
#     
#     members = models.ManyToManyField(Patient, through = "Subscription")
#     name = models.CharField(max_length = 255,
#                             default = None,
#                             unique = True,
#                             blank = False,
#                             null = False,
#                             verbose_name = ugettext_lazy("name"))
#     type = models.CharField(max_length = 255, 
#                             choices = TYPES,
#                             default = None,
#                             blank = False, 
#                             null = False)
#     
#     class Meta:
#         verbose_name = ugettext_lazy("Information service")
#                             
#     def __unicode__(self):
#         return self.name
#     
#     def member_count(self):
#         return self.members.all().count()
#     
#     def texts(self):
#         """
#         Returns the text blocks associated with the group's type.
#         """
#         return self.TYPE_TEXTS[self.type]


# Path: sendinel/medicines/forms.py
from django.forms import CharField, Form, ModelChoiceField
from django.utils.translation import ugettext_lazy as _
from sendinel.backend.authhelper import format_and_validate_phonenumber
from sendinel.backend.models import get_enabled_wocs
from sendinel.infoservices.models import InfoMessage, InfoService




class RegisterPatientForMedicineForm(Form):  
    phone_number = CharField(validators = [format_and_validate_phonenumber],
            error_messages={'required':_('Please enter a phone number')})
    way_of_communication = ModelChoiceField(
                        queryset = get_enabled_wocs(),
                        error_messages={'required': \
                                _('Please choose a way of communication')})     
    medicine = ModelChoiceField(
            queryset=InfoService.objects.filter(type='medicine'),
            error_messages={'required': \
                                _('Please choose a medicine'),
                            'invalid_choice':
                                _('Please choose a medicine')})

class MedicineMessageValidationForm(Form):
    medicine = ModelChoiceField(

                queryset=InfoService.objects.filter(type='medicine'),