def read_and_store_sms(sms_file):
    f = open(sms_file, mode='r')
    line_id = None
    line_number = 0
    for current_line in f:
        try:
            line_number += 1
            if len(current_line) <= 1:
                continue

            # this line is a new sms
            if current_line.strip().startswith("sms"):
                # split may contain more than we want because of the comma in content
                spited = current_line.split(",")
                direction, name, phone, date_time, content = spited[1], spited[
                    2], spited[3], spited[5], spited[7],
                date_time = deal_with_sms_date_time(date_time)

                # the position of name and phone may exchange due to the direction
                if direction == "submit":
                    pass
                else:
                    name, phone = phone, name
                sp_list = [direction, name, phone, date_time, content]
                tp_list = []
                for a in sp_list:
                    tp_list.append(a.strip())
                new_sms = Sms(sms_direction=tp_list[0],
                              sms_name=tp_list[1],
                              sms_phone=tp_list[2],
                              sms_date=tp_list[3][:10],
                              sms_time=tp_list[3],
                              sms_content=tp_list[4])
                new_sms.save()
                line_id = new_sms.get_id()
                continue

            # this line is a continue content
            if line_id is None:
                continue
            sms = Sms.get(id=line_id)
            sms.sms_content += current_line
            sms.save()
        except Exception, e:
            ErrorLog(error_line=line_number,
                     error_function="read_and_store_sms",
                     error_msg=e).save()
def read_and_store_sms(sms_file):
    f = open(sms_file, mode='r')
    line_id = None
    line_number = 0
    for current_line in f:
        try:
            line_number += 1
            if len(current_line) <= 1:
                continue

            # this line is a new sms
            if current_line.strip().startswith("sms"):
                # split may contain more than we want because of the comma in content
                spited = current_line.split(",")
                direction, name, phone, date_time, content = spited[1], spited[2], spited[3], spited[5], spited[7],
                date_time = deal_with_sms_date_time(date_time)

                # the position of name and phone may exchange due to the direction
                if direction == "submit":
                    pass
                else:
                    name, phone = phone, name
                sp_list = [direction, name, phone, date_time, content]
                tp_list = []
                for a in sp_list:
                    tp_list.append(a.strip())
                new_sms = Sms(sms_direction=tp_list[0], sms_name=tp_list[1], sms_phone=tp_list[2],
                              sms_date=tp_list[3][:10], sms_time=tp_list[3], sms_content=tp_list[4])
                new_sms.save()
                line_id = new_sms.get_id()
                continue

            # this line is a continue content
            if line_id is None:
                continue
            sms = Sms.get(id=line_id)
            sms.sms_content += current_line
            sms.save()
        except Exception, e:
            ErrorLog(error_line=line_number, error_function="read_and_store_sms", error_msg=e).save()