File size: 2,196 Bytes
bb98c0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import streamlit as st
import pandas as pd
import requests

# Function to create a custom field in Maropost
def create_custom_field(data, auth_token):
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
    }
    response = requests.post(
        "https://api.maropost.com/accounts/1783/custom_fields.json",
        json=data,
        headers=headers,
        params={'auth_token': auth_token}
    )
    return response

# Function to read CSV and create custom fields
def create_custom_fields_from_csv(csv_file, auth_token):
    df = pd.read_csv(csv_file)
    success_fields = []
    failed_fields = []

    for _, row in df.iterrows():
        data = {
            "custom_field": {
                "name": row['name'],
                "field_type": row['field_type'],
                "add_to_profile_page": row.get('add_to_profile_page', False),
                "default_value": row.get('default_value', "")
            }
        }
        response = create_custom_field(data, auth_token)
        if response.status_code in [200, 201]:
            success_fields.append(row['name'])
        else:
            failed_fields.append((row['name'], response.status_code, response.text))
    
    return success_fields, failed_fields

# Streamlit UI
def main():
    st.title("Maropost Custom Fields Creator")

    auth_token = st.text_input("Enter your Maropost API auth token:", type="password")
    csv_file = st.file_uploader("Upload CSV file for custom fields", type=['csv'])

    if st.button("Create Custom Fields"):
        if not auth_token or csv_file is None:
            st.error("Please enter your auth token and upload a CSV file.")
        else:
            success_fields, failed_fields = create_custom_fields_from_csv(csv_file, auth_token)
            if success_fields:
                st.success(f"Successfully created custom fields: {', '.join(success_fields)}.")
            if failed_fields:
                for failed_field, status_code, message in failed_fields:
                    st.error(f"Failed to create custom field {failed_field}. Response code: {status_code}, Message: {message}")

if __name__ == "__main__":
    main()