MAD2 Viva - Preparations: Tips, Resources, and Strategies
"A complete guide to acing your Modern Applications Development 2 Viva for IITM BS Degree."
a. Preparation Checklist
- Content Example:
- ✅ Revise all assignments and projects
- ✅ Understand Flask basics
- ✅ Practice REST API concepts
- ✅ Be ready to explain your project in detail
- ✅ Prepare for common Viva questions
Key Topics to Focus On
- Example:
- Flask Basics: Understand routing, request handling, and templates.
- SQLAlchemy: Be clear on models and relationships.
- API Development: Know how REST APIs work and how you implemented them in your project.
- Frontend Basics: Be ready to explain any Vue.js/HTML/CSS integration
Definition
A primary key is:
- Unique: No two rows in a table can have the same value for the primary key.
- Non-Nullable: The primary key field(s) must always have a value (i.e., it cannot contain
NULL). - Immutable: Ideally, the value of a primary key should not change once it is assigned to a record.
Purpose
- To ensure uniqueness in the table.
- To provide a way to reference records in relationships (e.g., foreign keys in other tables).
- To allow for efficient indexing and searching.
Characteristics
- Single-Column or Composite:
- A primary key can be a single column (e.g.,
id) or a combination of columns (composite key).
- A primary key can be a single column (e.g.,
- Automatically Indexed:
- Most database systems automatically create an index on the primary key for faster lookups.
Example in SQL
Single-Column Primary Key:
- Here,
idis the primary key, uniquely identifying each user.
Composite Primary Key:
- Here, the combination of
order_idandproduct_iduniquely identifies each row.
Why Use a Primary Key?
- Ensures data integrity by preventing duplicate rows.
- Makes relationships between tables possible via foreign keys.
- Improves query performance with indexing.
Primary Key vs Unique Key
- Both enforce uniqueness, but a table can only have one primary key, while it can have multiple unique keys.
- A primary key does not allow
NULLvalues, but a unique key can allow null values.
No, you cannot fully replace a primary key with just NOT NULL and AUTO_INCREMENT though they might achieve some overlapping functionality. Here’s why:
What NOT NULL and AUTO_INCREMENT Do
NOT NULL:
- Ensures that a column cannot have
NULLvalues. - Does not guarantee uniqueness across rows.
- Ensures that a column cannot have
AUTO_INCREMENT :
- Automatically generates sequential values for a column when new rows are inserted.
- Often combined with
NOT NULLto ensure no gaps or duplicates. - Does not enforce uniqueness by itself.
When NOT NULL + AUTO_INCREMENT Is Used Without Primary Key
You can use NOT NULL and AUTO_INCREMENT alone if:
- You don’t need to enforce uniqueness across rows.
- You’re not creating relationships between tables.
- Example: You want to track insert order but don’t require the column to be the unique identifier.
Example:
- Here,
order_idwill increment automatically but is not a primary key. Duplicates are possible if noPRIMARY KEYorUNIQUEconstraint is applied.
Best Practice
- Always use a primary key to ensure uniqueness and proper indexing if the column is meant to uniquely identify rows.
- If the column should auto-generate values, you can combine
AUTO_INCREMENTorSERIALwith thePRIMARY KEYconstraint.
Example:
- Here:
idisNOT NULLandAUTO_INCREMENTto generate sequential values.PRIMARY KEYensures uniqueness and allows relationships with other tables.
Key Takeaway
- NOT NULL + AUTO_INCREMENT ≠ PRIMARY KEY.
- To fully replicate a primary key’s functionality (uniqueness, indexing, referential integrity), you must explicitly define it or use a UNIQUE constraint along with
NOT NULLandAUTO_INCREMENT.
In your Application , Flask Security likely facilitates:
- Authentication for login/logout functionality.
- Role Management for admins, professionals, and customers.
- Password Hashing for secure user registration and management.
- Route Protection using
@roles_required,@login_required, and@auth_required.
How to Identify Where It’s Used
To pinpoint exactly where Flask Security is used in your app:
- Look for imports like
- Check your user model:
- Ensure it includes
RoleandUserRolestables if Flask Security's role-based features are used.
- Ensure it includes
- Inspect routes with decorators like
@roles_requiredor@login_required. - Configuration Settings
1. User Authentication-verification of the user is allowed to access the resources,
where all the routes required user login
Flask Security provides an easy way to handle user login/logout and password management.
You likely used it to:
- Allow users and admins to log in securely.
- Provide session management .
2. Role Management
Admin,users), Flask Security is likely used to manage access control for these roles: @roles_required to enforce role-based access on certain routes.3. Password Hashing and Security
Flask Security includes built-in password hashing using libraries like werkzeug to secure user passwords in the database
Use Case in Your App:
- User registration (
/registerroutes) would hash passwords before saving them to the database. - Password reset functionality (if implemented) to securely manage forgotten passwords.
4. Route Protection
Flask Security provides decorators like:
@login_required: Ensures only logged-in users can access specific routes.@roles_required: Restricts access to users with specific roles.@auth_required: Checks for authentication tokens in APIs.
You may have used these to restrict access to routes:
- Protect the admin dashboard with
@roles_required('admin').
5. Token-Based Authentication (Optional for API)
If your application includes an API, Flask Security could have been used to:
- Issue authentication tokens for API requests.
- Use
@auth_requiredto validate tokens.
Both Authenticated and Authorized:
- This is the case for an admin. They are first authenticated (logged in) and then authorized (granted access to admin-only actions)
What is the DOM?
The DOM (Document Object Model) is a programming interface for web documents. It represents the structure of a webpage as a tree of objects that can be manipulated with programming languages like JavaScript.
When a web page is loaded in a browser:
- The browser parses the HTML and CSS.
- It creates a DOM that represents the page’s structure and content.
@app.route('/api/search',methods=['GET'])
def search():
query = args.get.query('username',' ')
if not query:
return josinify({"message":"query is username required"}),400
users = User.query.filter(User.username.ilike(f'%{query}%')).all
results = [{'id': user.id, 'username': user.username, 'email': user.email} for user in users]
return jsonify(results)
Key Points:
request.args.get('username'): Retrieves the username parameter from the query string.
ilike: Case-insensitive matching using SQL'sLIKEkeyword.%{query}%: Enables partial matching (finds usernames containing the search query).2. Frontend: Vue.js Implementation
Create a search input and display results dynamically.
Search Component:
- <template>
- <div>
- <!-- Search Input -->
- <form class="d-flex" role="search">
- <input
- type="text"
- class="form-control me-2"
- v-model="searchQuery"
- placeholder="Search by username"
- aria-label="Search"
- style="width: 400px;"
- />
- <button
- type="button"
- class="btn btn-outline-primary"
- @click="searchUsers"
- >
- Search
- </button>
- </form>
- <!-- Results -->
- <ul v-if="results.length > 0">
- <li v-for="user in results" :key="user.id">
- {{ user.username }} - {{ user.email }}
- </li>
- </ul>
- <p v-else-if="searched">No users found.</p>
- </div>
- </template>
- <script>
- export default {
- data() {
- return {
- searchQuery: '', // Input value
- results: [], // Search results
- searched: false // Flag to show "No users found" message
- };
- },
- methods: {
- async searchUsers() {
- try {
- const response = await fetch(
- `/search?username=${encodeURIComponent(this.searchQuery)}`
- );
- if (!response.ok) throw new Error('Failed to fetch');
- const data = await response.json();
- this.results = data;
- this.searched = true; // Mark as searched to show no-results message if applicable
- } catch (error) {
- console.error('Error searching users:', error);
- }
- }
- }
- };
- </script>
No comments:
Post a Comment