Showing posts with label file. Show all posts
Showing posts with label file. Show all posts
Wednesday, July 19, 2017
diff rcs file
diff rcs file
Code:
rcsdiff -r1.1 -r1.2 -u file
here we are diffing rev 1.2 agains 1.1
If you want to colour the output you can use the idiff script
Code:
rcsdiff -r1.1 -r1.2 -u file | ./idiff
Thursday, July 13, 2017
Display File Content with Colors and Line Number Alternative to cat Ubuntu
Display File Content with Colors and Line Number Alternative to cat Ubuntu
Sometimes you may want to quickly display the contents of a file directly on the command line with the `cat` command. And sometimes the contents of that file is the source code of a program.
If you want to display the source code markup with colors and line numbers, then this trick is what you are looking for.
What you need to do first is install Pygments, a python syntax highlighter.
sudo apt-get install python-pygments python3-pygmentsAfter installing pygments, you can now view source code with colored markups. just run the ff:
pygmentize testfile.rbNow for the line numbers, you will have to pipe the `nl` command
pygmentize testfile.rb | nl --body-numbering=aThe option
--body-numberingmeans it will accept a style for line numbering and it is any of the ff:
a - number all lines
t - number only nonempty lines
n - number no lines
I used a so that everything will be numbered including empty lines.
We can actually add nl options in bash alias so that we do not have to type it everytime.
alias nl=nl --body-number=aWhile at it, lets also shorten the pygmentize command.
alias ccat=pygmentizeNow the source code syntax will be highlighted everytime we ran this command:
ccat testfile.rb | nl
Thursday, June 29, 2017
Django Reminder Delete File when deleting object
Django Reminder Delete File when deleting object
For Django FileField objects and object of its subclasses, when using the objects delete() method, it will only delete the object record from the database, the file will remain in the appointed location in your servers filesystem.
After a few research, it is a deliberate design in Django to prevent unwanted mess in database when rolling back an record (not sure how this is doable though, leave comments if you know how :D).
So, we have to explicitly delete the file from the filesystem ourselves using
FileFieldObject.file_field.storage.delete(filepath)
A simple implementation to delete the file when delete the file object from db would be like this:
f = FileFieldObject #replace with your models .objects.get() here
storage = f.file_field.storage
#field_field is the property in your model which is an object of class model.FileField
path = f.field_field.path
storage.delete(path) #delete file from disk
f.delete() #delete record from db
Sunday, June 25, 2017
Django Remind Image File upload handling and retrieving
Django Remind Image File upload handling and retrieving
The handling of image/file in Django is a bit complicated than PHP, since PHP basically uses HTML syntax to display a file.
Assuming we are building a blog, and want to to user to add photo to their posts
Here are the steps required - I write this to serve as a reminder and quick reference for myself, so I am going to do this quick.
1. Specify the MEDIA_ROOT and MEDIA_URL in the settings.py that you want to use, if not specified, the uploaded files will be stored at the projects BASE_DIR
2. Start serving files in MEDIA_ROOT by adding settings in myproject/settings.py, use shortcut method provided in the documentation in development
3. Create a model in models.py to help store the image, added an attribute to store the location that the image file will be uploaded, such as imageFile = models.ImageField(upload_to = someLocation)
3.1 (optional) Since we are adding photos for a blog post, we can make accessing the photos of a blog post easier by adding a ForeignKey field in the photo model, and point it to an blog post entry.
4. build a form in a template with enctype = multipart/data and method = post attributes
5. write a view function that process the POSTed data, point the form in step 1 to this view function by adding an ACTION attribute.
6. In the view function, the files submitted via the POSTed form is available in the variable request.FILES dictionary, use the name of the type=file input in your form to access the file in the dictionary, say request.FILES[profile-pic]
7. handle said file, and save() it, if you have added a ForeignKey field in the photo model class, remember to save the blog post entry object before saving the photo(s), if the blog post isnt saved first, an error would occur since the photo object creation requires a ForeignKey field which is bonded to an BlogPost object
8. To retrieve the photo in a blog post template, pass the photo of a blog post like this
photos = BlogObject.photo_set.all(),
then
return render(request, template.html, {photo: photos })
9. In the blog post template, display the photo like this:
{% if photo %}
{% for image in photo %}
<img src = {{ image.ImageFile.url }}
{% endfor %}
{% endif %}
Note that the ImageFile variable here is the attribute we set earlier in the models.Photo class, which is an ImageField() object.
ImageField objects has a .url attribute that store the relative location of the image file. Therefore, we can use this to be the src attribute of our img tags.
for FileField, the usage is quite similar, but instead we would have to provide it through an <a> tag to render a file download link.
Thats it, its quite complicated to set up properly for the first time, but it would get better later :D
Thursday, April 13, 2017
Django ImageField rename file on upload
Django ImageField rename file on upload
By default Django keeps the original name of the uploaded file but more than likely you will want to rename it to something else (like the objects id). Luckily, with ImageField or FileField of Django forms, you can assign a callable function to the upload_to parameter to do the renaming. For example:
from django.db import models
from django.utils import timezone
import os
from uuid import uuid4
def path_and_rename(instance, filename):
upload_to = photos
ext = filename.split(.)[-1]
# get filename
if instance.pk:
filename = {}.{}.format(instance.pk, ext)
else:
# set filename as random string
filename = {}.{}.format(uuid4().hex, ext)
# return the whole path to the file
return os.path.join(upload_to, filename)
class CardInfo(models.Model):
id_number = models.CharField(max_length=6, primary_key=True)
name = models.CharField(max_length=255)
photo = models.ImageField(upload_to=path_and_rename, max_length=255, null=True, blank=True)
In this example, every image that is uploaded will be rename to the CardInfo objects primary key which is id_number.
Reference: http://stackoverflow.com/questions/15140942/django-imagefield-change-file-name-on-upload
Subscribe to:
Posts (Atom)