Google URL Shortner using Python requests
This code can convert a url to short url using google URL Shortener API https://developers.google.com/url-shortener/v1/getting_started
- json
- url
- api
- headers
- post
- requests
- short url
This code can convert a url to short url using google URL Shortener API https://developers.google.com/url-shortener/v1/getting_started
Requires [line_profiler](https://pypi.python.org/pypi/line_profiler) pip install line_profiler Will print profile info into console @line_profiler def my_view(request): context = some_quick_func() return some_heavy_func(context) It does not work good when nested, so don't wrap `some_heavy_func`. If you want to profile also some nested call - use `extra_view` parameter: @line_profiler(extra_view=[some_heavy_func]) def my_view(request): context = some_quick_func() return some_heavy_func(context) It will profile `my_view` and `some_heavy_func`
Update to https://djangosnippets.org/snippets/1907/ to be a bit more flexible, and code cleaned up a tiny bit. To use, add this snippet as a file in a templatetags folder in an app or in a project. Then include and call the tag with {% obfuscate_email 'email' %} or {% obfuscate_email 'email' 'link_text' %} if 'link_text' is provided, it will be used as the text in the body of the <a> tag. If not, then the email will be used.
I wanted to be able to restyle the inputs, which required having access to each of the select widgets. When used in a form, you can simply iterate over the field to access each element. Example: {% for form_field in form.date %} <div class="select-wrap"> {{ form_field }} </div> {% endfor %}
The `post_syncdb` (Django pre-1.7) and `post_migrate` (>=1.7 and South) signals are fired for every single app. What I really wanted was one signal fired after the migration or syncdb completed. There's no official one, and all the snippets were doing horribly hacky things (and wouldn't work for South, anyway). This one will work for Django syncdb, and South migrate.
**Provides pattern to organize and send your email messages.**
This class lets me have a model choice field that includes optgroups . Django has built-in support for optgroups if you explicitly set all the choices in a ChoiceField, but that doesn't help with ModelChoiceFields. Optgroups let you have nice-looking subscategories in huge dropdowns. They're even more useful if you're using something like selectize.js, because you have a ton of options. If you inherit from GroupedModelChoiceField and override the optgroup_from_instance function, as in SampleChoiceField, you'll get a dropdown with your models with the expected optgroup tags in the html. Be sure to have your queryset first order by whatever you're displaying in optgroup.
This is a quite simple snippet to integrate postgresql trgm search in django 1.6.10 This snippet is easy to adapt to other special operators by changing the trgm_search function. This example uses the operator `%%` but you could use `ts_vector(fieldname) @@ to_tsquery(%s)`
Instead of the default Django User the Auth Model is 'customer' from the usercp App. User's can use username as a display to the public, without disclosing their login information. This way they can use a forum with their username, which is seen by everyone. The login credentials, however are more privat, since it is the e-mail address. Allowing the user to not disclose any information.
PreviewMixin adds a preview page for Django's CBV (FormView, UpdateView, CreateView). After a form has been submitted, it is returned again, optionally in a different template to confirm. If the form is submitted with the same data, the default "form_valid" function is executed. Features: 1. `process_preview` - function executed after submitting the form for the first time (default is to do nothing) 2. `done` - function for the action if the confirm page is sent (defaults to whatever form_valid of the django cbv does) 3. `preview_template_name` - variable with the name of the template for the confirm page (defaults to taking the same template as for the initial form) 4. new function `security_hash` to calculate a hash which is added to the confirmation form. Works kind of like django-formtools, just as a Mixin for the default Django cbv.
The recipe uses deferred constraint validation to create circular references across database tables. The example requires Postgres (MySQL doesn't support deferred constraints). To achieve this, the following is required: * Insertions must be performed in a transaction. Foreign key constraints will be validated at the end of the transactions, allowing for insertion of rows with FKs pointing to rows that don't exist yet. * Primary keys need to be generated before insertion. That's what `prefetch_id` does by pulling the next value from the `*_id_seq` sequence.
You can use this as a model method.
Adds init and changes passed args to *args and **kwargs .
This snippets extends ModelAdmin in order to allow multi field sorting in admin tables. Usage example: class MyModelAdmin(MultiFieldSortableModelAdmin): list_display = ( ... 'user_full_name', ... ) def user_full_name(self, obj): return obj.user.get_full_name() user_full_name.admin_order_field = ['user__first_name', 'user__last_name']`
You can test how your django app behaves depending on what kind of response it gets from the API. It assumes were're using the python requests library.
2955 snippets posted so far.