o
    zi}                     @   s   d dl Z d dlZd dlZd dlZd dlmZ dZdd Zdd Zdd	 Z	d
e
de
fddZdddZdd Zedkr?e  dS dS )    N)datetimez./home/cpsweather/theweathermonitor.com/auth.dbc                  C   s   t jtdd} t j| _| S )Ng      @)timeout)sqlite3connectDB_PATHRowrow_factorycx r   change_notification_settings.py
db_connect   s   r   c                  C   sR   t  } | d | d | d |   W d    d S 1 s"w   Y  d S )Na  
            CREATE TABLE IF NOT EXISTS recipient_preferences_requests (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                email TEXT NOT NULL,
                phone TEXT NOT NULL,
                status TEXT NOT NULL DEFAULT 'pending',
                requested_ts TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')),
                processed_ts TEXT,
                result_code TEXT,
                result_detail TEXT
            )
            z
            CREATE INDEX IF NOT EXISTS idx_recipient_preferences_requests_status
            ON recipient_preferences_requests(status)
            z
            CREATE INDEX IF NOT EXISTS idx_recipient_preferences_requests_requested_ts
            ON recipient_preferences_requests(requested_ts)
            )r   executecommitr	   r   r   r   ensure_table   s   
"r   c                  C   s   zt tjddp
d} W n ty   d} Y nw | dkr$tjj| nd}tj	j
|dd}|ddgd p8d }|ddgd pEd }||fS )	NCONTENT_LENGTH0r    T)keep_blank_valuesemailphone)intosenvironget
ValueErrorsysstdinreadurllibparseparse_qsstrip)content_lengthrawparsedr   r   r   r   r   parse_post_body0   s   r&   r   r   c                 C   sH   t  }|d|  |f |  W d    d S 1 sw   Y  d S )Nz
            INSERT INTO recipient_preferences_requests(email, phone, status)
            VALUES (?, ?, 'pending')
            )r   r   lowerr   )r   r   r
   r   r   r   insert_request>   s   

"r(   r   c           	   	   C   s   t j| dd}t j|dd}|rdt | dnd}|r&dt | dnd}d| d| d	| d
| d	}td td t  t| d S )NT)quotez<div class="error">z</div>r   z/<div class="muted" style="margin-bottom:16px;">aF  <!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Change Notification Settings - The Weather Monitor</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    body {
      font-family: Arial, sans-serif;
      margin: 0;
      padding: 0;
      background: #f5f7fa;
      color: #1f2937;
    }
    .wrap {
      max-width: 720px;
      margin: 40px auto;
      padding: 0 16px;
    }
    .card {
      background: #fff;
      border: 1px solid #d1d5db;
      border-radius: 12px;
      padding: 24px;
      box-shadow: 0 2px 8px rgba(0,0,0,0.04);
    }
    h1 {
      margin-top: 0;
    }
    .muted {
      color: #6b7280;
    }
    .error {
      background: #fee2e2;
      border: 1px solid #fecaca;
      color: #991b1b;
      padding: 12px;
      border-radius: 8px;
      margin-bottom: 16px;
    }
    label {
      display: block;
      margin: 12px 0 6px;
      font-weight: 600;
    }
    input[type="text"] {
      width: 100%;
      box-sizing: border-box;
      padding: 10px 12px;
      border: 1px solid #cbd5e1;
      border-radius: 8px;
    }
    button {
      margin-top: 16px;
      padding: 10px 16px;
      border: 0;
      border-radius: 8px;
      background: #2563eb;
      color: white;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="wrap">
    <section class="card">
      <h1>Change Notification Settings</h1>
      <div class="muted" style="margin-bottom:16px;">
        Enter your email address and phone number to request a secure link to manage your notification settings.
      </div>

      z
      z

      <form method="post" action="/change-notification-settings">
        <label for="email">Email Address</label>
        <input type="text" id="email" name="email" value="zw" required>

        <label for="phone">Phone Number</label>
        <input type="text" id="phone" name="phone" value="z~" required>

        <button type="submit">Request Secure Link</button>
      </form>
    </section>
  </div>
</body>
</html>
zStatus: 200 OKz&Content-Type: text/html; charset=utf-8)htmlescapeprint)	r   r   errormessageemail_hphone_h
error_htmlmessage_htmlbodyr   r   r   render_pageJ   s*   GHLOXr4   c                  C   sl   t   tjdp
d } | dkr1t \}}|r|s#t||dd d S t|| t||dd d S t  d S )NREQUEST_METHODGETPOSTz,Email address and phone number are required.)r   r   r-   z=If we found a matching recipient, a secure link will be sent.)r   r   r.   )r   r   r   r   upperr&   r4   r(   )methodr   r   r   r   r   main   s   


r:   __main__)r   r   r   r   )r*   r   r   urllib.parser   r   r   r   r   r&   strr(   r4   r:   __name__r   r   r   r   <module>   s   
i
