| Conditions | 6 |
| Total Lines | 114 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | #!/usr/bin/env python3 |
||
| 154 | def send_new_user_notification(new_user_name, new_user_email): |
||
| 155 | """ |
||
| 156 | Send an email notification to the global admin when a new user registers. |
||
| 157 | |||
| 158 | Args: |
||
| 159 | new_user_name: Name of the newly registered user |
||
| 160 | new_user_email: Email of the newly registered user |
||
| 161 | |||
| 162 | Returns: |
||
| 163 | bool: True if email was sent successfully, False otherwise |
||
| 164 | """ |
||
| 165 | try: |
||
| 166 | # Get the global admin user |
||
| 167 | global_admin = User.query.filter_by(is_global_admin=True).first() |
||
| 168 | |||
| 169 | if not global_admin: |
||
| 170 | print("No global admin found, skipping notification") |
||
| 171 | return False |
||
| 172 | |||
| 173 | # Get email settings for the global admin |
||
| 174 | email_config = Email.query.filter_by(user_id=global_admin.id).first() |
||
| 175 | |||
| 176 | if not email_config: |
||
| 177 | print(f"No email settings configured for global admin {global_admin.id}, skipping notification") |
||
| 178 | return False |
||
| 179 | |||
| 180 | # Extract email credentials |
||
| 181 | from_email = email_config.email |
||
| 182 | password = email_config.password |
||
| 183 | imap_server = email_config.server |
||
| 184 | |||
| 185 | # Convert IMAP server to SMTP server |
||
| 186 | # Common patterns: imap.gmail.com -> smtp.gmail.com, imap.mail.yahoo.com -> smtp.mail.yahoo.com |
||
| 187 | smtp_server = imap_server.replace('imap', 'smtp') |
||
| 188 | |||
| 189 | # Create the email message |
||
| 190 | msg = MIMEMultipart('alternative') |
||
| 191 | msg['From'] = from_email |
||
| 192 | msg['To'] = from_email # Send to the global admin's own email |
||
| 193 | msg['Subject'] = 'New User Registration - PyCashFlow' |
||
| 194 | |||
| 195 | # Create plain text version |
||
| 196 | text_content = f""" |
||
| 197 | A new user has registered on PyCashFlow. |
||
| 198 | |||
| 199 | Name: {new_user_name} |
||
| 200 | Email: {new_user_email} |
||
| 201 | Registration Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} |
||
| 202 | |||
| 203 | This user account is currently inactive and requires approval before they can log in. |
||
| 204 | Please log in to your PyCashFlow account to activate this user. |
||
| 205 | """ |
||
| 206 | |||
| 207 | # Create HTML version |
||
| 208 | html_content = f""" |
||
| 209 | <html> |
||
| 210 | <body> |
||
| 211 | <h2>New User Registration</h2> |
||
| 212 | <p>A new user has registered on PyCashFlow.</p> |
||
| 213 | <table style="border-collapse: collapse; margin-top: 10px;"> |
||
| 214 | <tr> |
||
| 215 | <td style="padding: 5px; font-weight: bold;">Name:</td> |
||
| 216 | <td style="padding: 5px;">{new_user_name}</td> |
||
| 217 | </tr> |
||
| 218 | <tr> |
||
| 219 | <td style="padding: 5px; font-weight: bold;">Email:</td> |
||
| 220 | <td style="padding: 5px;">{new_user_email}</td> |
||
| 221 | </tr> |
||
| 222 | <tr> |
||
| 223 | <td style="padding: 5px; font-weight: bold;">Registration Date:</td> |
||
| 224 | <td style="padding: 5px;">{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}</td> |
||
| 225 | </tr> |
||
| 226 | </table> |
||
| 227 | <p style="margin-top: 15px;"> |
||
| 228 | <strong>Note:</strong> This user account is currently inactive and requires approval before they can log in. |
||
| 229 | Please log in to your PyCashFlow account to activate this user. |
||
| 230 | </p> |
||
| 231 | </body> |
||
| 232 | </html> |
||
| 233 | """ |
||
| 234 | |||
| 235 | # Attach both versions |
||
| 236 | part1 = MIMEText(text_content, 'plain') |
||
| 237 | part2 = MIMEText(html_content, 'html') |
||
| 238 | msg.attach(part1) |
||
| 239 | msg.attach(part2) |
||
| 240 | |||
| 241 | # Send the email using SMTP |
||
| 242 | # Try port 587 with STARTTLS first (most common) |
||
| 243 | try: |
||
| 244 | server = smtplib.SMTP(smtp_server, 587, timeout=10) |
||
| 245 | server.starttls() |
||
| 246 | server.login(from_email, password) |
||
| 247 | server.sendmail(from_email, from_email, msg.as_string()) |
||
| 248 | server.quit() |
||
| 249 | print(f"Successfully sent new user notification email to {from_email}") |
||
| 250 | return True |
||
| 251 | except Exception as e: |
||
| 252 | # If port 587 fails, try port 465 with SSL |
||
| 253 | print(f"Failed to send via port 587, trying SSL on port 465: {e}") |
||
| 254 | try: |
||
| 255 | server = smtplib.SMTP_SSL(smtp_server, 465, timeout=10) |
||
| 256 | server.login(from_email, password) |
||
| 257 | server.sendmail(from_email, from_email, msg.as_string()) |
||
| 258 | server.quit() |
||
| 259 | print(f"Successfully sent new user notification email to {from_email} via SSL") |
||
| 260 | return True |
||
| 261 | except Exception as e2: |
||
| 262 | print(f"Failed to send email via both ports: {e2}") |
||
| 263 | return False |
||
| 264 | |||
| 265 | except Exception as e: |
||
| 266 | print(f"Error sending new user notification: {e}") |
||
| 267 | return False |
||
| 268 | |||
| 284 |