Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -1551,124 +1551,6 @@ def add_data_ver_cur(): | |
| 1551 |  | 
| 1552 |  | 
| 1553 |  | 
| 1554 | 
            -
            @app.route('/add_data_ver_cur2', methods=['GET'])
         | 
| 1555 | 
            -
            def add_data_ver_cur2():
         | 
| 1556 | 
            -
                global current_curator_index
         | 
| 1557 | 
            -
             | 
| 1558 | 
            -
                veref_on_off = request.args.get('ver', '0')  # Включает "1" и выключает "0" верификацию номера вместо verifikation_start
         | 
| 1559 | 
            -
                curator_on_off = request.args.get('cur', '0')  # Включает "1" и выключает "0" назначение куратора
         | 
| 1560 | 
            -
                order = request.args.get('order', '')
         | 
| 1561 | 
            -
                status = request.args.get('status', '')
         | 
| 1562 | 
            -
                template_key = request.args.get('template_key', 'avp')
         | 
| 1563 | 
            -
                mapping_template_cur = mapp_templates.get(template_key, mt_avp)
         | 
| 1564 | 
            -
             | 
| 1565 | 
            -
                user_data = {mapping_template_cur[key]: request.args.get(key, "") for key in mapping_template_cur}
         | 
| 1566 | 
            -
             | 
| 1567 | 
            -
                logging.debug(f"Received data: {user_data}")
         | 
| 1568 | 
            -
             | 
| 1569 | 
            -
                if curator_on_off == "1":
         | 
| 1570 | 
            -
                    user_data['curator'] = curators[current_curator_index]
         | 
| 1571 | 
            -
             | 
| 1572 | 
            -
                if veref_on_off == "1":
         | 
| 1573 | 
            -
                    phone_number = user_data.get('phone', '')
         | 
| 1574 | 
            -
                    if not phone_number:
         | 
| 1575 | 
            -
                        logging.error("Phone number is empty")
         | 
| 1576 | 
            -
                        return jsonify({'status': 'error', 'message': 'Phone number is empty'}), 400
         | 
| 1577 | 
            -
                    
         | 
| 1578 | 
            -
                    phone_verification_response = verify_phone_number(phone_number)
         | 
| 1579 | 
            -
                    if phone_verification_response is not None:
         | 
| 1580 | 
            -
                        user_data['ws_st'] = phone_verification_response
         | 
| 1581 | 
            -
             | 
| 1582 | 
            -
                try:
         | 
| 1583 | 
            -
                    # Шаг 1: Сначала записываем пользователя
         | 
| 1584 | 
            -
                    add_or_update_contact(user_data, skip_defaults=True)
         | 
| 1585 | 
            -
                    if curator_on_off == "1":
         | 
| 1586 | 
            -
                        current_curator_index = (current_curator_index + 1) % len(curators)
         | 
| 1587 | 
            -
             | 
| 1588 | 
            -
                    # Шаг 2: Обновляем поле shop_st JSON-данными
         | 
| 1589 | 
            -
                    shop_st_data = {order: status}
         | 
| 1590 | 
            -
                    update_shop_st(user_data['email'], shop_st_data)
         | 
| 1591 | 
            -
             | 
| 1592 | 
            -
                    response_message = {
         | 
| 1593 | 
            -
                        'status': 'success',
         | 
| 1594 | 
            -
                        'message': f'User added with curator {user_data.get("curator", "not assigned")}'
         | 
| 1595 | 
            -
                    }
         | 
| 1596 | 
            -
                    if status:
         | 
| 1597 | 
            -
                        response_message['status'] = status
         | 
| 1598 | 
            -
                    return jsonify(response_message)
         | 
| 1599 | 
            -
                except Exception as e:
         | 
| 1600 | 
            -
                    logging.error(f"Error adding user: {e}")
         | 
| 1601 | 
            -
                    return jsonify({'status': 'error', 'message': str(e)}), 500
         | 
| 1602 | 
            -
             | 
| 1603 | 
            -
             | 
| 1604 | 
            -
            def add_or_update_contact(contact_data, skip_defaults=False):
         | 
| 1605 | 
            -
                conn = sqlite3.connect(DATABASE_NAME3)
         | 
| 1606 | 
            -
                cursor = conn.cursor()
         | 
| 1607 | 
            -
             | 
| 1608 | 
            -
                email = contact_data.get('email')
         | 
| 1609 | 
            -
                if not email:
         | 
| 1610 | 
            -
                    logging.error(f"Missing email in contact data: {contact_data}")
         | 
| 1611 | 
            -
                    return
         | 
| 1612 | 
            -
             | 
| 1613 | 
            -
                utc_now = datetime.utcnow()
         | 
| 1614 | 
            -
                msk_tz = pytz.timezone('Europe/Moscow')
         | 
| 1615 | 
            -
                msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz)
         | 
| 1616 | 
            -
                contact_data['data_t'] = msk_now.strftime('%Y-%m-%d %H:%M:%S')
         | 
| 1617 | 
            -
             | 
| 1618 | 
            -
                # Список всех возможных полей
         | 
| 1619 | 
            -
                fields = [
         | 
| 1620 | 
            -
                    'name', 'phone', 'email', 'vk_id', 'chat_id', 'ws_st', 'ws_stop', 'web_st', 'fin_prog',
         | 
| 1621 | 
            -
                    'b_city', 'b_fin', 'b_ban', 'b_ign', 'b_baners', 'b_butt', 'b_mess', 'shop_st', 'curator',
         | 
| 1622 | 
            -
                    'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'gc_url', 'key_pr', 'n_con', 'canal', 'data_on', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gcpc'
         | 
| 1623 | 
            -
                ]
         | 
| 1624 | 
            -
             | 
| 1625 | 
            -
                # Устанавливаем значения по умолчанию для отсутствующих полей, только если не указан флаг skip_defaults
         | 
| 1626 | 
            -
                if not skip_defaults:
         | 
| 1627 | 
            -
                    for field in fields:
         | 
| 1628 | 
            -
                        if field not in contact_data:
         | 
| 1629 | 
            -
                            contact_data[field] = ''
         | 
| 1630 | 
            -
             | 
| 1631 | 
            -
                placeholders = ", ".join([f"{field} = ?" for field in fields])
         | 
| 1632 | 
            -
             | 
| 1633 | 
            -
                cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,))
         | 
| 1634 | 
            -
                contact = cursor.fetchone()
         | 
| 1635 | 
            -
             | 
| 1636 | 
            -
                if contact:
         | 
| 1637 | 
            -
                    update_query = f"UPDATE contacts SET {placeholders} WHERE id = ?"
         | 
| 1638 | 
            -
                    cursor.execute(update_query, (*[contact_data.get(field, '') for field in fields], contact[0]))
         | 
| 1639 | 
            -
                else:
         | 
| 1640 | 
            -
                    insert_query = f"INSERT INTO contacts ({', '.join(fields)}) VALUES ({', '.join(['?' for _ in fields])})"
         | 
| 1641 | 
            -
                    cursor.execute(insert_query, tuple(contact_data.get(field, '') for field in fields))
         | 
| 1642 | 
            -
             | 
| 1643 | 
            -
                conn.commit()
         | 
| 1644 | 
            -
                conn.close()
         | 
| 1645 | 
            -
             | 
| 1646 | 
            -
             | 
| 1647 | 
            -
            def update_shop_st(email, shop_st_data):
         | 
| 1648 | 
            -
                conn = sqlite3.connect(DATABASE_NAME3)
         | 
| 1649 | 
            -
                cursor = conn.cursor()
         | 
| 1650 | 
            -
             | 
| 1651 | 
            -
                cursor.execute("SELECT shop_st FROM contacts WHERE email = ?", (email,))
         | 
| 1652 | 
            -
                contact = cursor.fetchone()
         | 
| 1653 | 
            -
             | 
| 1654 | 
            -
                if contact:
         | 
| 1655 | 
            -
                    current_shop_st = contact[0]
         | 
| 1656 | 
            -
                    if current_shop_st:
         | 
| 1657 | 
            -
                        try:
         | 
| 1658 | 
            -
                            current_shop_st = json.loads(current_shop_st)
         | 
| 1659 | 
            -
                        except json.JSONDecodeError:
         | 
| 1660 | 
            -
                            current_shop_st = {}
         | 
| 1661 | 
            -
                    else:
         | 
| 1662 | 
            -
                        current_shop_st = {}
         | 
| 1663 | 
            -
             | 
| 1664 | 
            -
                    current_shop_st.update(shop_st_data)
         | 
| 1665 | 
            -
                    new_shop_st = json.dumps(current_shop_st, ensure_ascii=False)
         | 
| 1666 | 
            -
             | 
| 1667 | 
            -
                    update_query = "UPDATE contacts SET shop_st = ? WHERE email = ?"
         | 
| 1668 | 
            -
                    cursor.execute(update_query, (new_shop_st, email))
         | 
| 1669 | 
            -
                    conn.commit()
         | 
| 1670 | 
            -
             | 
| 1671 | 
            -
                conn.close()
         | 
| 1672 |  | 
| 1673 |  | 
| 1674 |  | 
|  | |
| 1551 |  | 
| 1552 |  | 
| 1553 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 1554 |  | 
| 1555 |  | 
| 1556 |  |