Esempio n. 1
0
def keys_match(rec, keys, new_rec):
    try:
        keys = force_list(keys)
        if type(new_rec) is dict:
            new_values = force_list(itemgetter(*keys)(new_rec))
        else:
            new_values = force_list(new_rec)
        rec_values = force_list(itemgetter(*keys)(rec))
        return force_lower(rec_values) == force_lower(new_values)
    except Exception as inst:
        return None
Esempio n. 2
0
 def get_all_minutes(type=None, year=None):
     if not type:
         type = [t for t in MinutesType][1:]
     type = [Minutes.map_type_to_file(t)[:3] for t in force_list(type)]
     files = os.listdir(minutes_location)
     minutes = [
         Minutes(Minutes.map_file_to_type(f[:3]), parse_date(f[-14:-4], ' '), f[-3:])
         for f in files
         if not os.path.isdir(f) and f[:3] in type
     ]
     if year:
         minutes = [m for m in minutes if m.date.year in force_list(year)]
     return sorted(minutes, key=Minutes.take_date, reverse=True)
Esempio n. 3
0
def update_member_questions(member, qandas):
    all = []
    for qanda in qandas:
        (question, answer, other) = qanda
        if other == '':
            other = None
        if question == 0:
            continue
        answers = force_list(answer)
        items = [q for q in member.qandas if q.question_id == question]
        for answer in answers:
            if len(items) > 0:
                item = items.pop()
                item.question_id = question
                item.answer = answer
                item.other = other
            else:
                item = QandA(
                    member_id=member.id,
                    question_id=question,
                    answer=answer,
                    other=other,
                )
            all.append(item)
    member.qandas = all
    db.session.commit()
def get_elements_from_html(html, ids):
    result = {}
    for id in force_list(ids):
        i = html.find(id)
        if i >= 0:
            start = i + 1 + html[i:].find('>')
            length = html[start:].find('<')
            result[id] = html[start:start + length]
    return result
def line_break(text, line_break_characters=None):
    br = '<br/>'
    if type(text) is list:
        res = br.join([t for t in text if len(t) > 0])
    else:
        line_break_characters = force_list(line_break_characters)
        for r in line_break_characters[1:]:
            text = text.replace(r, line_break_characters[0])
        res = line_break(text.split(line_break_characters[0]))
    return res
Esempio n. 6
0
def update_records(file, key, key_value, header, new_values):
    if len(new_values) == 0: return
    delimiter = file_delimiter(file)
    keys = None
    ft, target_file_path = mkstemp()
    os.close(ft)
    found = [False] * len(new_values)
    key = force_list(key)
    key_value = force_list(key_value)
    rec_key = (dict(zip(key[:len(key_value)], key_value)))
    with my_open(target_file_path, 'w') as target_file:
        with my_open(file, 'r') as source_file:
            for line in source_file:
                last_line = line[-1] != '\n'
                values = line.rstrip('\n').split(delimiter)
                if keys is None:
                    keys = [k.lower() for k in values]
                    new_line = line
                else:
                    rec = OrderedDict(zip(keys, values))
                    new_line = line
                    if keys_match(rec, list(rec_key), rec_key):
                        update_rec = get_matching_update_rec(
                            rec, key, key_value, header, new_values, found)
                        if update_rec:
                            new_line = insert_rec_values(
                                rec, update_rec, delimiter, last_line)
                target_file.write(new_line)
            # add new records
            not_found = [i for i, f in enumerate(found) if not f]
            if len(not_found) > 0: target_file.write('\n')
            for i in not_found:
                last_line = i == not_found[-1]
                values = new_values[i]
                rec = OrderedDict(zip(keys, [''] * len(keys)))
                rec.update(dict(zip(key, key_value)))
                new_line = insert_rec_values(rec, dict(zip(header, values)),
                                             delimiter, last_line)
                target_file.write(new_line)
    os.remove(file)
    move(target_file_path, file)
Esempio n. 7
0
def save_object(objects):
    for object in force_list(objects):
        try:
            if not object.id:
                db_session.add(object)
            db_session.commit()
        except:
            # if any kind of exception occurs, rollback transaction
            db_session.rollback()
            raise
        finally:
            pass  # db_session.close()
Esempio n. 8
0
def send_mail(to, sender, cc=None, subject=None, message=None):
    #in order to get this to work, had to change the libraries as follows:
    #/home/admin/wagsv_python/venv/lib/python3.5/site-packages/flask_sendmail/connection.py[23]
    #   sm.stdin.write(str.encode(message.dump()))
    #/usr/lib/python3.5/email/mime/text.py[17]
    #   def __init__(self, _text, _subtype='plain', _charset='utf-8'):
    to = force_list(to)
    msg = Message(subject)
    msg.sender = sender
    msg.recipients = to
    msg.cc = force_list(cc)
    msg.body = '\n'.join(force_list(message))
    app = current_app._get_current_object()
    if config.get('send_mail'):
        #send_async_email(app, msg)
        mail.send(msg)
    else:
        out = [
            'Email:', 'from: ' + sender, 'to: ' + ', '.join(to),
            'subject: ' + subject, 'message:'
        ]
        print('\n'.join(out + force_list(message)))
Esempio n. 9
0
def add_keys(keys, key_values, new_data):
    keys = force_list(keys)
    for i in range(len(keys)):
        if keys[i] not in new_data:
            new_data[keys[i]] = key_values[i]
Esempio n. 10
0
 def __init__(self, date=None, message='', items=[]):
     self.date = coerce_fmt_date(date or datetime.date.today())
     self.message = message
     items = force_list(items)
     self.items = [NewsItem(*item) for item in items]