Thank you

Opw has come to an end and looking back I feeel so proud and happy.I become more confident .I figured out what I want to do next.I also had fun, met a lot of amazing people .All good things come to an end. This is a small thank you note to thank all those wonder full people who have helped me in making this internship the best opportunity I ever had. My parents friends for their constant support and motivation.There are some persons I specifically want to thank

Rebecca Billings: My mentor,I am so privileged to have her as my mentor.I am really inspired by her the way she organizes her time.I have learned to organize my time.My commmunication skill has improved over the time,it is because of her.

Bob Silverberg:  I have improved a lot pair programming with him.I learned not just scripting in python but modularizing code so that its make the code more readable.He gives  exceptionally good PR reviewer that hardly any bug goes unnoticed. He has been patient when i took time more than required.

Stephend mbrand and mozwebqa team.I owe you a lot.Thanking for the constant support and words of motivation.I like to explore more

Git pre-commit hook

When I got repeated reviews about code not passing flake8 checking bob mentioned about using hooks. It was then a new term to me. After checking a couple of webpages I found it very interesting. I wrote a git pre-commit-hook using shell scripting.My script checks two case

1: Check if the current branch is a master branch.
2: Check for python file for checking python syntax errors

What are git hooks?
git can trigger some import actions before you perform an important function like commit, rebase etc. A pre-commit hook is triggered before a commit occurs. Git hooks are stored in hooks directory of .git

Step 1

cd .git/hooks
vi pre- commit

Step 2: Copy the code

(git diff --cached --name-only

git diff–cached is used to find the difference between latest commit and files added for staging.
–name-only list only names of the that have changed.

(git diff --cached --name-only --diff-filter=ACM | grep -e '\.py$'

This list all the python files.

flake8 $file --ignore=E501

This ignores E501(line too long).

Step3: Make the file executable

chmod +x pre-commit-hook

And that’s it. Now every time when you commit your code, git will run flake8 for you. Also you may not fear about accidentally committing your master branch.

Data Migration in Django

Changing the database is one side of the equation, but often a migration involves changing data as well;

Consider this case

Bug 1096431 Able to create tasks with duplicate names.Task names should be unique

class Task:

     name = models.CharField(max_length=255, verbose_name=title)

    start_date = models.DateTimeField(blank=True, null=True)

Schema migration of course. ie Add unique = True. If you apply this migration to production it will cause an IntergrityError because you make a database column unique, while it’s contents are not unique. To solve this you need to find all tasks with duplicate names and rename them to something unique. This will be a datamigration instead of a schemamigration. Thanks to Giorgos Logiotatidis for the guidance,

Step1 : Create a few task with same names say ‘task’

Step2 : This is datamigration part

python datamigration Tasks

Step 3: When you Open up the newly created file you can see the skeleton  of forwards and backwards function.I wrote a code to rename the duplicate taskname.

Step4 : Now add the unique keyword to the field and apply a schemamigration

Step5 :Finally migrate the model tasks.Now you can the the duplicate tasks gets renamed as  task 2 task 3 etc

First week Tasks

It took pretty long time to get into my blog. The code is yet to be merged .But I must say it was challenging and interesting.

Task1: Display users who have completed a given task on the task detail page

How i did?

I wrote  a simple method to the model Task which returns the list of users who have completed the given task.

def users_who_completed_this_task(self):
return User.objects.filter(
taskattempt__in=TaskAttempt.objects.filter(, state=TaskAttempt.FINISHED)).distinct()

The property decorator lets you to access the method as an attribute of the class. So that if task1 is an instance of the model Task, task1.users_who_completed_task returns all users who completed the specific task.

My first unit test case

  • This was most challenging and exciting part.I wrote unit test cases for the code i wrote.Thanks to Bob silverberg for the helping me with  constant feedbacks. If you are new to unit testing this check this link.🙂

Task2: Add date completed in the Task Activity page

This was quite easy . 🙂

modified=attempt.modified.strftime(‘%Y-%m-%d: %I:%M:%S %p’).lower())

What i learned

    • Some simple filters used in jinja2 templates like strfttime. Here is a simple code snippet for a comma separated list of users in the template

 {% for each_user in users %}
{{ }}{% if not loop.last %},{% endif %}
{% endfor %}

  • Writing unit testcase