Ejemplo n.º 1
0
def _create_alerts(data, person, unit):
    print "Creating alerts!"
    try:
        alerts = data['alerts']
        if not isinstance(alerts, (list, tuple)):
            raise ValidationError("Problems not in list format")
    except KeyError:
        alerts = []

    errors = []
    
    for alert in alerts:
        comments = None
        try:
            emplid = alert['emplid']
            code = alert['code']
            description = alert['description']
            unique_id = alert['unique_id']
        except KeyError:
            raise ValidationError("Necessary fields not present in alert: " + str(alert) )
            
        try:
            if not isinstance(emplid, int):
                raise ValidationError("Alert emplid:'%s' must be integer" % str(emplid) )
            try:
                student = Person.objects.get(emplid=emplid)
            except Person.DoesNotExist:
                try:
                    p = coredata.queries.add_person( emplid )
                except SIMSProblem:
                    raise ValidationError("Person %s could not be found; SIMS not working." % str(emplid) )
                except IOError:
                    raise ValidationError("Person %s could not be found; SIMS not working." % str(emplid) )
            
            if not isinstance(code, basestring) or not isinstance(description, basestring):
                raise ValidationError("Alert code & description must be strings")
            if len(code) >= 30:
                raise ValidationError("Alert code must be less than or equal to 30 characters")

            try: 
                details = alert['details']
            except KeyError:
                details = {}
            
            try:
                alertType = AlertType.objects.get(code=code)
            except AlertType.DoesNotExist:
                # If the AlertType doesn't exist, try to create one. 
                # This requires that the User has provided a unit at the 
                # base level of the JSON
                alertType = AlertType()
                alertType.code = code
                alertType.unit = unit
                alertType.description = "Not set."
                alertType.save() 
            
            alert = Alert()
            alert.person = student
            alert.alerttype = alertType
            alert.description = description
            alert.details = details

            alert.safe_create(unique_id)

        except ValidationError as e:
            print e
            errors.append(str(e))
    return errors
Ejemplo n.º 2
0
def _create_alerts(data, person, unit):
    print "Creating alerts!"
    try:
        alerts = data['alerts']
        if not isinstance(alerts, (list, tuple)):
            raise ValidationError("Problems not in list format")
    except KeyError:
        alerts = []

    errors = []

    for alert in alerts:
        comments = None
        try:
            emplid = alert['emplid']
            code = alert['code']
            description = alert['description']
            unique_id = alert['unique_id']
        except KeyError:
            raise ValidationError("Necessary fields not present in alert: " +
                                  str(alert))

        try:
            if not isinstance(emplid, int):
                raise ValidationError("Alert emplid:'%s' must be integer" %
                                      str(emplid))
            try:
                student = Person.objects.get(emplid=emplid)
            except Person.DoesNotExist:
                try:
                    p = coredata.queries.add_person(emplid)
                except SIMSProblem:
                    raise ValidationError(
                        "Person %s could not be found; SIMS not working." %
                        str(emplid))
                except IOError:
                    raise ValidationError(
                        "Person %s could not be found; SIMS not working." %
                        str(emplid))

            if not isinstance(code, basestring) or not isinstance(
                    description, basestring):
                raise ValidationError(
                    "Alert code & description must be strings")
            if len(code) >= 30:
                raise ValidationError(
                    "Alert code must be less than or equal to 30 characters")

            try:
                details = alert['details']
            except KeyError:
                details = {}

            try:
                alertType = AlertType.objects.get(code=code)
            except AlertType.DoesNotExist:
                # If the AlertType doesn't exist, try to create one.
                # This requires that the User has provided a unit at the
                # base level of the JSON
                alertType = AlertType()
                alertType.code = code
                alertType.unit = unit
                alertType.description = "Not set."
                alertType.save()

            alert = Alert()
            alert.person = student
            alert.alerttype = alertType
            alert.description = description
            alert.details = details

            alert.safe_create(unique_id)

        except ValidationError as e:
            print e
            errors.append(str(e))
    return errors