Add UUID generation for candidates during scraping #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR implements automatic UUID generation for all candidates when they are scraped and added to the database, addressing the need for unique identifiers as requested in the issue.
Problem
Previously, candidates did not have proper unique identifiers in the JSON database. The system relied on name-based matching for candidate lookups, which was fragile and made it difficult to:
Solution
Added UUID generation at the point where candidates are created during the scraping process:
1. Candidate Models (
app/models/candidate.py)Added optional
idfield to all candidate models to store the UUID:Candidate: Base model withid: Optional[str] = NoneLokSabhaCandidate: Lok Sabha specific withid: Optional[str] = NoneAssemblyCandidate: Assembly specific withid: Optional[str] = Field(alias="ID", default=None)2. LokSabha Scraper (
app/scrapers/lok_sabha.py)Modified candidate creation in
_scrape_parties()to generate UUID:3. VidhanSabha Scraper (
app/scrapers/vidhan_sabha.py)Modified candidate creation in
_extract_candidates_from_page()to generate UUID:4. Data Service (
app/services/json_data_service.py)Enhanced
get_candidate_by_id()method to support UUID-based lookups while maintaining backward compatibility:idorIDfield)Benefits
✅ Unique Identification: Each candidate now has a globally unique identifier (UUID v4)
✅ Database Ready: Makes future migration to SQL/NoSQL databases seamless
✅ Better APIs: Enables reliable candidate lookup by ID in REST endpoints
✅ Backward Compatible: Existing name-based lookups continue to work
✅ Minimal Impact: Only touched necessary files with surgical changes
Example
When new candidates are scraped, they will automatically include a UUID:
{ "id": "a1b2c3d4-e5f6-7890-abcd-ef1234567890", "party_id": 1, "constituency": "New Delhi", "candidate_name": "John Doe", "votes": "100000", "margin": "5000" }This UUID can then be used in API calls:
Testing
Breaking Changes
None. This is a backward-compatible addition that enhances the system without breaking existing functionality.
Original prompt
Fixes #4
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.