<s> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import Group as AuthGroup , Permission <EOL> from django . core . cache import cache <EOL> from django . core . exceptions import ValidationError <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import models <EOL> from django . db . models import Q <EOL> from django . db . models . signals import m2m_changed <EOL> from django . utils . safestring import mark_safe <EOL> from taggit . managers import TaggableManager <EOL> from taggit . models import Tag <EOL> import autocomplete_light <EOL> from open_connect . media . models import Image , ShortenedURL <EOL> from open_connect . connectmessages . models import Thread , Message <EOL> from open_connect . connect_core . utils . location import get_coordinates , STATES <EOL> from open_connect . connect_core . utils . models import TimestampModel <EOL> from open_connect . groups . tasks import remove_user_from_group <EOL> autocomplete_light . register ( Tag ) <EOL> GROUP_STATUSES = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) <EOL> class Category ( TimestampModel ) : <EOL> """<STR_LIT>""" <EOL> slug = models . SlugField ( unique = True ) <EOL> name = models . CharField ( max_length = <NUM_LIT> ) <EOL> color = models . CharField ( <EOL> verbose_name = '<STR_LIT>' , max_length = <NUM_LIT:7> , default = '<STR_LIT>' ) <EOL> class Meta ( object ) : <EOL> """<STR_LIT>""" <EOL> verbose_name = '<STR_LIT>' <EOL> verbose_name_plural = '<STR_LIT>' <EOL> def __unicode__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . name <EOL> class GroupManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> def get_queryset ( self ) : <EOL> """<STR_LIT>""" <EOL> return super ( GroupManager , self ) . get_queryset ( ) . select_related ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) . exclude ( status = '<STR_LIT>' ) <EOL> def with_deleted ( self ) : <EOL> """<STR_LIT>""" <EOL> return super ( GroupManager , self ) . get_queryset ( ) . select_related ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def create ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> name = kwargs . pop ( '<STR_LIT:name>' , None ) <EOL> if '<STR_LIT>' not in kwargs and name : <EOL> kwargs [ '<STR_LIT>' ] = AuthGroup . objects . create ( name = name ) <EOL> return super ( GroupManager , self ) . create ( ** kwargs ) <EOL> def published ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . get_queryset ( ) . filter ( published = True , ** kwargs ) <EOL> def search ( self , search = None , location = None ) : <EOL> """<STR_LIT>""" <EOL> groups = Group . objects . published ( ) . select_related ( '<STR_LIT:image>' , '<STR_LIT>' ) <EOL> if search : <EOL> groups = groups . filter ( <EOL> Q ( group__name__icontains = search ) <EOL> | Q ( category__name__icontains = search ) <EOL> | Q ( description__icontains = search ) <EOL> | Q ( tags__slug__icontains = search ) <EOL> ) . distinct ( ) <EOL> if location : <EOL> groups = self . location_search ( location , queryset = groups ) <EOL> return groups <EOL> def location_search ( self , location , queryset = None ) : <EOL> """<STR_LIT>""" <EOL> coords = get_coordinates ( location ) <EOL> if not coords : <EOL> return Group . objects . none ( ) <EOL> if queryset is None : <EOL> queryset = Group . objects . published ( ) <EOL> sql = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> result = queryset . extra ( <EOL> select = { '<STR_LIT>' : sql } , <EOL> select_params = ( coords [ <NUM_LIT:0> ] , coords [ <NUM_LIT:0> ] , coords [ <NUM_LIT:1> ] ) , <EOL> where = [ '<STR_LIT:(>' + sql + '<STR_LIT>' ] , <EOL> params = ( coords [ <NUM_LIT:0> ] , coords [ <NUM_LIT:0> ] , coords [ <NUM_LIT:1> ] ) , <EOL> order_by = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ) . distinct ( ) <EOL> return result <EOL> class Group ( TimestampModel ) : <EOL> """<STR_LIT>""" <EOL> group = models . OneToOneField ( AuthGroup ) <EOL> private = models . BooleanField ( <EOL> default = False , <EOL> help_text = '<STR_LIT>' <EOL> ) <EOL> published = models . BooleanField ( <EOL> default = True , <EOL> verbose_name = u'<STR_LIT>' , <EOL> help_text = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> moderated = models . BooleanField ( <EOL> default = False , <EOL> verbose_name = u'<STR_LIT>' , <EOL> help_text = '<STR_LIT>' <EOL> ) <EOL> featured = models . BooleanField ( <EOL> default = False , <EOL> verbose_name = u'<STR_LIT>' , <EOL> help_text = '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> db_index = True <EOL> ) <EOL> member_list_published = models . BooleanField ( <EOL> default = True , <EOL> help_text = '<STR_LIT>' <EOL> ) <EOL> category = models . ForeignKey ( <EOL> '<STR_LIT>' , verbose_name = u'<STR_LIT>' , default = <NUM_LIT:1> ) <EOL> display_location = models . CharField ( blank = True , max_length = <NUM_LIT:100> ) <EOL> latitude = models . FloatField ( blank = True , null = True ) <EOL> longitude = models . FloatField ( blank = True , null = True ) <EOL> radius = models . IntegerField ( blank = True , null = True ) <EOL> is_national = models . BooleanField ( default = True , db_index = True ) <EOL> owners = models . ManyToManyField ( <EOL> settings . AUTH_USER_MODEL , <EOL> blank = True , <EOL> related_name = '<STR_LIT>' ) <EOL> whitelist_users = models . ManyToManyField ( <EOL> settings . AUTH_USER_MODEL , <EOL> blank = True , <EOL> related_name = '<STR_LIT>' <EOL> ) <EOL> description = models . TextField ( blank = True ) <EOL> tags = TaggableManager ( blank = True ) <EOL> image = models . ForeignKey ( Image , blank = True , null = True ) <EOL> state = models . CharField ( <EOL> max_length = <NUM_LIT:3> , <EOL> choices = [ ( s , s ) for s in STATES ] , <EOL> blank = True , <EOL> db_index = True <EOL> ) <EOL> tos_accepted_at = models . DateTimeField ( blank = True , null = True ) <EOL> created_by = models . ForeignKey ( settings . AUTH_USER_MODEL , null = True ) <EOL> status = models . CharField ( <EOL> choices = GROUP_STATUSES , <EOL> default = '<STR_LIT>' , <EOL> db_index = True , <EOL> max_length = <NUM_LIT:50> <EOL> ) <EOL> objects = GroupManager ( ) <EOL> class Meta ( object ) : <EOL> """<STR_LIT>""" <EOL> ordering = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> permissions = ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) , <EOL> ) <EOL> def __unicode__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return u'<STR_LIT:%s>' % self . group . name <EOL> def save ( self , force_insert = False , force_update = False , using = None , <EOL> update_fields = None ) : <EOL> """<STR_LIT>""" <EOL> if not all ( [ self . longitude , self . latitude , self . radius ] ) : <EOL> self . is_national = True <EOL> else : <EOL> self . is_national = False <EOL> return super ( Group , self ) . save ( <EOL> force_insert , force_update , using , update_fields ) <EOL> def delete ( self , using = None ) : <EOL> """<STR_LIT>""" <EOL> self . status = '<STR_LIT>' <EOL> self . save ( ) <EOL> for user in self . group . user_set . all ( ) . iterator ( ) : <EOL> remove_user_from_group . delay ( user = user , group = self ) <EOL> def get_absolute_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return reverse ( '<STR_LIT>' , args = ( self . pk , ) ) <EOL> @ property <EOL> def full_url ( self ) : <EOL> """<STR_LIT>""" <EOL> return settings . ORIGIN + self . get_absolute_url ( ) <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> required_together = [ <EOL> self . latitude , self . longitude , self . radius ] <EOL> if any ( required_together ) and not all ( required_together ) : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def get_members ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . group . user_set . all ( ) <EOL> def get_members_avatar_prioritized ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_members ( ) . extra ( <EOL> select = { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) . select_related ( '<STR_LIT:image>' ) . order_by ( '<STR_LIT>' ) <EOL> def public_threads_by_user ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return Thread . public . by_user ( user ) . filter ( group = self ) <EOL> def public_threads ( self ) : <EOL> """<STR_LIT>""" <EOL> return Thread . public . by_group ( group = self ) <EOL> @ property <EOL> def unmoderated_messages ( self ) : <EOL> """<STR_LIT>""" <EOL> return Message . objects . filter ( <EOL> thread__group = self , status = '<STR_LIT>' ) <EOL> @ property <EOL> def total_unmoderated_messages ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . unmoderated_messages . count ( ) <EOL> def images ( self , user ) : <EOL> """<STR_LIT>""" <EOL> return Image . popular . with_user ( <EOL> user = user <EOL> ) . filter ( <EOL> message__thread__group = self <EOL> ) <EOL> def links ( self ) : <EOL> """<STR_LIT>""" <EOL> return ShortenedURL . popular . filter ( <EOL> message__thread__group = self , <EOL> message__status = '<STR_LIT>' ) <EOL> def group_owners_changed ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs [ '<STR_LIT:action>' ] in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> users = get_user_model ( ) . objects . filter ( pk__in = kwargs [ '<STR_LIT>' ] ) <EOL> for user in users : <EOL> cache . delete ( user . cache_key + '<STR_LIT>' ) <EOL> if kwargs [ '<STR_LIT:action>' ] == '<STR_LIT>' : <EOL> direct_message_permission = Permission . objects . get ( <EOL> codename = '<STR_LIT>' , <EOL> content_type__app_label = '<STR_LIT>' ) <EOL> for user in users : <EOL> user . user_permissions . add ( direct_message_permission ) <EOL> m2m_changed . connect ( group_owners_changed , Group . owners . through ) <EOL> class GroupRequestManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> def unapproved ( self ) : <EOL> """<STR_LIT>""" <EOL> return super ( <EOL> GroupRequestManager , self <EOL> ) . get_queryset ( ) . filter ( moderated_by__isnull = True ) <EOL> class GroupRequest ( TimestampModel ) : <EOL> """<STR_LIT>""" <EOL> user = models . ForeignKey ( settings . AUTH_USER_MODEL ) <EOL> group = models . ForeignKey ( Group ) <EOL> moderated_by = models . ForeignKey ( <EOL> settings . AUTH_USER_MODEL , <EOL> blank = True , <EOL> null = True , <EOL> related_name = '<STR_LIT>' <EOL> ) <EOL> moderated_at = models . DateTimeField ( blank = True , null = True ) <EOL> approved = models . NullBooleanField ( blank = True ) <EOL> objects = GroupRequestManager ( ) <EOL> def __unicode__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return mark_safe ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' . format ( <EOL> url = self . user . get_absolute_url ( ) , <EOL> email = self . user . email , <EOL> state = self . user . state , <EOL> zip_code = self . user . zip_code , <EOL> name = self . user . get_real_name ( ) , <EOL> group = self . group <EOL> ) <EOL> ) </s>
