Skip to content

Instantly share code, notes, and snippets.

@xleon
xleon / models.py
Last active December 26, 2015 07:18
When using easy_thumbnails on django you want to delete created thumbnails in two cases: 1) you update or "clean" the image file of the model 2) you delete the model instance. Using Signals, this is pretty easy This code is tested and working when updating one item, removing it and it also works when bulk deleting
from easy_thumbnails.models import Source, Thumbnail
from django.db import models
from django.db.models.signals import pre_delete, pre_save
from project_package import settings
class Project(models.Model):
name = models.CharField("Name", max_length=255)
main_image = models.ImageField("Main Image", blank=True, null=True, upload_to='uploads')
def __unicode__(self):
@xleon
xleon / models.py
Created October 23, 2013 07:38
Next and prev models when using django-admin-sortable. This is useful for "next" and "prev" buttons of a project detail for example
from adminsortable.models import Sortable
from django.template.defaultfilters import slugify
class Project(Sortable):
name = models.CharField("Name", max_length=255)
slug = models.SlugField(max_length=255, editable=False)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Project, self).save(*args, **kwargs)
@xleon
xleon / EFNestedCollections.cs
Last active August 29, 2015 14:15
Entity framework nested collection filtering
public async Task<IEnumerable<Conversation>> GetUserConversations(string userId)
{
var temp = await conversationRepo.GetDbSet()
.Where(x => x.DeletedBy != userId)
.Where(x => x.SellerId == userId || x.BuyerId == userId)
.Select(x => new
{
conversation = x,
buyer = x.Buyer,
seller = x.Seller,
@xleon
xleon / SignalR_IntegrationTest.cs
Last active July 26, 2018 20:18
Attempt to test a SignalR (authorized) method (chat "SendMessage") in a one-to-one chat
[TestMethod]
public async Task SendMessage_Should_SaveData_AndReachPeer()
{
IHubProxy proxy1 = await CreateConnectionAndGetProxy(AdminToken);
IHubProxy proxy2 = await CreateConnectionAndGetProxy(UserToken);
ChatMessageDTO pushedMessage = new ChatMessageDTO();
proxy1.On<ChatMessageDTO>("AddNewMessage", m => pushedMessage = m);
ChatMessageEntryDTO message = new ChatMessageEntryDTO
@xleon
xleon / ExtendedSearchBarRenderer.cs
Created February 25, 2015 01:44
Xamarin.Forms renderer to hide the "Cancel" button of a SearchBar on iOS
using System;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using UIKit;
using System.Diagnostics;
[assembly: ExportRenderer(typeof(SearchBar), typeof(Namespace.iOS.Renderers.ExtendedSearchBarRenderer))]
namespace Namespace.iOS.Renderers
{
@xleon
xleon / CustomAndroidApplication.cs
Last active January 21, 2016 17:07
Custom Android Application class to know when the app is in the background
using System;
using Acr.UserDialogs;
using Android.App;
using Android.Content;
using Android.Content.Res;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Plugin.CurrentActivity;
@xleon
xleon / ExifRotation.cs
Last active November 13, 2020 19:40
Exif rotation fix (Android Xamarin) borrowed from mvvmcross
using Android.Graphics;
using Android.Media;
private static Bitmap ExifRotateBitmap(string filePath, Bitmap bitmap)
{
if (bitmap == null)
return null;
var exif = new ExifInterface(filePath);
var rotation = exif.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Normal);
@xleon
xleon / Pre-build event (Visual Studio)
Last active February 20, 2016 13:33
PowerShell script to update iOS bundle (Info.pslist) depending on the build configuration. This way you can target different identifiers/display names and have them all installed in the same device
PowerShell -File "$(ProjectDir)Update-Bundle.ps1" $(ProjectDir) $(ConfigurationName)
@xleon
xleon / AutoScrollFromKeyboard.cs
Created April 1, 2016 03:00
Scroll Content when soft keyboard appears/disappears in iOS UIViewController
using System;
using CoreGraphics;
using Foundation;
using HakiOS.Ui.Extensions;
using UIKit;
// adapted from http://forums.xamarin.com/discussion/comment/23235/#Comment_23235
namespace HakiOS.Ui.Utils
{
public class AutoScrollFromKeyboard
@xleon
xleon / ILocator.as
Last active August 10, 2016 10:42
Service Locator for ActionScript applications
package Core.Utils.ServiceLocation
{
public interface ILocator
{
function RegisterConstant(value:Object, type:Class):void;
function Register(type:Class, contract:Class = null):void;
function RegisterSingleton(type:Class, contract:Class = null):void;