File size: 2,643 Bytes
bdb271a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

# initializes the SQLAlchemy object for the application
db = SQLAlchemy()

# defines the main data model for a scraped disaster post
class DisasterPost(db.Model):
    # sets the name of the database table
    __tablename__ = 'posts'

    # primary key for the table
    id = db.Column(db.Integer, primary_key=True)
    # unique ID from Reddit to prevent duplicates
    reddit_id = db.Column(db.String(50), unique=True, nullable=False)
    # title of the Reddit post
    title = db.Column(db.String(300), nullable=False)
    # full body content of the post
    content = db.Column(db.Text, nullable=True)
    # stores the contact person's name or Reddit username
    author = db.Column(db.String(50), nullable=True)
    # timestamp when the post was created (UTC)
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    
    # Intelligence Fields
    # extracted location or address of the incident
    location = db.Column(db.String(100), nullable=True) 
    # extracted mobile or contact number
    contact_number = db.Column(db.String(50), nullable=True) 
    # AI-classified type of disaster (e.g., Flood, Fire)
    disaster_type = db.Column(db.String(50), nullable=True)
    # AI-classified specific assistance needed (e.g., Medical, Rescue)
    assistance_type = db.Column(db.String(50), nullable=True)
    # dynamically assigned severity level (High, Medium, Low)
    urgency_level = db.Column(db.String(20), nullable=True)
    # boolean flag indicating if the post is a help request
    is_help_request = db.Column(db.Boolean, default=False)
    
    # Metadata
    # operational status of the post (New, Verified, Resolved)
    status = db.Column(db.String(20), default="New")

    # converts the model object into a dictionary format for API responses
    def to_dict(self):
        # converts the timestamp to ISO 8601 format
        iso_time = self.timestamp.isoformat()
        # ensures the time string ends with 'Z' for standardized UTC representation
        if not iso_time.endswith("Z"):
            iso_time += "Z"

        return {
            "id": self.id,
            "reddit_id": self.reddit_id,
            "title": self.title,
            "content": self.content,
            "author": self.author,
            "timestamp": iso_time, 
            "location": self.location,
            "contact_number": self.contact_number or "Check Post",
            "disaster_type": self.disaster_type,
            "assistance_type": self.assistance_type,
            "urgency_level": self.urgency_level,
            "status": self.status
        }